blob: 89a68369b3b12d9cef9f47bb8cb8fb553f46bcc8 [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
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149 // The lock that must be acquired before referencing any static bg data structures. Unlike
150 // other locks, this one can generally be held long-term because we never expect any of these
151 // static data structures to be referenced outside of the worker thread except on the first
152 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700153 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
160 // created by LauncherModel that are directly on the home screen (however, no widgets or
161 // shortcuts within folders).
162 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
165 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 new ArrayList<LauncherAppWidgetInfo>();
167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700169 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700170
Adam Cohendcd297f2013-06-18 13:13:40 -0700171 // sBgWorkspaceScreens is the ordered set of workspace screens.
172 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
175 // times it is pinned.
176 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
177
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700178 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700179 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
180 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700181
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 // </ only access in worker thread >
183
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700184 private IconCache mIconCache;
185 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 private final LauncherAppsCompat mLauncherApps;
188 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700191 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700193 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700195 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
196 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700197 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700198 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800199 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700202 public void bindAppsAdded(ArrayList<Long> newScreens,
203 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700204 ArrayList<ItemInfo> addAnimated,
205 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200206 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700207 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
208 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
209 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700210 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700211 public void bindWorkspaceComponentsRemoved(
212 HashSet<String> packageNames, HashSet<ComponentName> components,
213 UserHandleCompat user);
214 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800215 public void notifyWidgetProvidersChanged();
216 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700217 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700218 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700219 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Winson Chung64359a52013-07-08 17:17:08 -0700222 public interface ItemInfoFilter {
223 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
224 }
225
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700226 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
227 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800228 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400229 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100230 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700231 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700233 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234
Kenny Guyed131872014-04-30 03:02:21 +0100235 mLauncherApps = LauncherAppsCompat.getInstance(context);
236 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 }
238
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
240 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700241 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700242 if (sWorkerThread.getThreadId() == Process.myTid()) {
243 // If we are on the worker thread, post onto the main handler
244 mHandler.post(r);
245 } else {
246 r.run();
247 }
248 }
249
250 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
251 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700252 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 r.run();
255 } else {
256 // If we are not on the worker thread, then post to the worker handler
257 sWorker.post(r);
258 }
259 }
260
Sunny Goyal756adbc2015-04-16 15:20:51 -0700261 public void setPackageState(final PackageInstallInfo installInfo) {
262 Runnable updateRunnable = new Runnable() {
263
264 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500265 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700266 synchronized (sBgLock) {
267 final HashSet<ItemInfo> updates = new HashSet<>();
268
269 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
270 // Ignore install success events as they are handled by Package add events.
271 return;
272 }
273
Sunny Goyale2df0622015-04-24 11:27:00 -0700274 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 if (info instanceof ShortcutInfo) {
276 ShortcutInfo si = (ShortcutInfo) info;
277 ComponentName cn = si.getTargetComponent();
278 if (si.isPromise() && (cn != null)
279 && installInfo.packageName.equals(cn.getPackageName())) {
280 si.setInstallProgress(installInfo.progress);
281
282 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
283 // Mark this info as broken.
284 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
285 }
286 updates.add(si);
287 }
288 }
289 }
290
291 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
292 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
293 widget.installProgress = installInfo.progress;
294 updates.add(widget);
295 }
296 }
297
298 if (!updates.isEmpty()) {
299 // Push changes to the callback.
300 Runnable r = new Runnable() {
301 public void run() {
302 Callbacks callbacks = getCallback();
303 if (callbacks != null) {
304 callbacks.bindRestoreItemsChange(updates);
305 }
306 }
307 };
308 mHandler.post(r);
309 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500310 }
311 }
312 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700313 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500314 }
315
Sunny Goyal756adbc2015-04-16 15:20:51 -0700316 /**
317 * Updates the icons and label of all pending icons for the provided package name.
318 */
319 public void updateSessionDisplayInfo(final String packageName) {
320 Runnable updateRunnable = new Runnable() {
321
322 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700323 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700324 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700325 ArrayList<ShortcutInfo> updates = new ArrayList<>();
326 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327
Sunny Goyale2df0622015-04-24 11:27:00 -0700328 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 if (info instanceof ShortcutInfo) {
330 ShortcutInfo si = (ShortcutInfo) info;
331 ComponentName cn = si.getTargetComponent();
332 if (si.isPromise() && (cn != null)
333 && packageName.equals(cn.getPackageName())) {
334 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
335 // For auto install apps update the icon as well as label.
336 mIconCache.getTitleAndIcon(si,
337 si.promisedIntent, user,
338 si.shouldUseLowResIcon());
339 } else {
340 // Only update the icon for restored apps.
341 si.updateIcon(mIconCache);
342 }
343 updates.add(si);
344 }
345 }
346 }
347
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700348 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700349 }
350 }
351 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700352 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700353 }
354
Adam Cohen76a47a12014-02-05 11:47:43 -0800355 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800356 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800357
358 if (allAppsApps == null) {
359 throw new RuntimeException("allAppsApps must not be null");
360 }
361 if (allAppsApps.isEmpty()) {
362 return;
363 }
364
365 // Process the newly added applications and add them to the database first
366 Runnable r = new Runnable() {
367 public void run() {
368 runOnMainThread(new Runnable() {
369 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800370 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800371 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500372 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800373 }
374 }
375 });
376 }
377 };
378 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700379 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800380
Sunny Goyala9116722015-04-29 13:55:58 -0700381 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800382 int[] xy, int spanX, int spanY) {
383 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700384 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700385
386 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800387 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700388 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700389 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800390 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800391 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700392 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 }
394
395 /**
396 * Find a position on the screen for the given size or adds a new screen.
397 * @return screenId and the coordinates for the item.
398 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700399 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700400 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 ArrayList<Long> workspaceScreens,
402 ArrayList<Long> addedWorkspaceScreensFinal,
403 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700404 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800405
Sunny Goyala9116722015-04-29 13:55:58 -0700406 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700407 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700408 synchronized (sBgLock) {
409 for (ItemInfo info : sBgItemsIdMap) {
410 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
411 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
412 if (items == null) {
413 items = new ArrayList<>();
414 screenItems.put(info.screenId, items);
415 }
416 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 }
420
421 // Find appropriate space for the item.
422 long screenId = 0;
423 int[] cordinates = new int[2];
424 boolean found = false;
425
426 int screenCount = workspaceScreens.size();
427 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700428 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 if (preferredScreenIndex < screenCount) {
430 screenId = workspaceScreens.get(preferredScreenIndex);
431 found = findNextAvailableIconSpaceInScreen(
432 screenItems.get(screenId), cordinates, spanX, spanY);
433 }
434
435 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700436 // Search on any of the screens starting from the first screen.
437 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 screenId = workspaceScreens.get(screen);
439 if (findNextAvailableIconSpaceInScreen(
440 screenItems.get(screenId), cordinates, spanX, spanY)) {
441 // We found a space for it
442 found = true;
443 break;
444 }
445 }
446 }
447
448 if (!found) {
449 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700450 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
451 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
452 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453
454 // Save the screen id for binding in the workspace
455 workspaceScreens.add(screenId);
456 addedWorkspaceScreensFinal.add(screenId);
457
458 // If we still can't find an empty space, then God help us all!!!
459 if (!findNextAvailableIconSpaceInScreen(
460 screenItems.get(screenId), cordinates, spanX, spanY)) {
461 throw new RuntimeException("Can't find space to add the item");
462 }
463 }
464 return Pair.create(screenId, cordinates);
465 }
466
467 /**
468 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700470 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700471 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800473 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700474 return;
Winson Chung997a9232013-07-24 15:33:46 -0700475 }
Winson Chung64359a52013-07-08 17:17:08 -0700476 // Process the newly added applications and add them to the database first
477 Runnable r = new Runnable() {
478 public void run() {
479 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
480 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
481
Winson Chung76828c82013-08-19 15:43:29 -0700482 // Get the list of workspace screens. We need to append to this list and
483 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
484 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800485 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700486 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700488 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800489 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700490 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800491 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700492 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800493 }
Winson Chung76828c82013-08-19 15:43:29 -0700494
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800495 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700496 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700497 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 long screenId = coords.first;
499 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700500
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700501 ItemInfo itemInfo;
502 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
503 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700505 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700506 } else {
507 throw new RuntimeException("Unexpected info type");
508 }
Winson Chung94d67682013-09-25 16:29:40 -0700509
Winson Chung64359a52013-07-08 17:17:08 -0700510 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700511 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700512 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700513 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700514 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700516 }
517 }
518
Winson Chung76828c82013-08-19 15:43:29 -0700519 // Update the workspace screens
520 updateWorkspaceScreenOrder(context, workspaceScreens);
521
Adam Cohen76a47a12014-02-05 11:47:43 -0800522 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700523 runOnMainThread(new Runnable() {
524 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800525 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700526 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700527 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
528 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700529 if (!addedShortcutsFinal.isEmpty()) {
530 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
531 long lastScreenId = info.screenId;
532 for (ItemInfo i : addedShortcutsFinal) {
533 if (i.screenId == lastScreenId) {
534 addAnimated.add(i);
535 } else {
536 addNotAnimated.add(i);
537 }
Winson Chung997a9232013-07-24 15:33:46 -0700538 }
539 }
Winson Chungd64d1762013-08-20 14:37:16 -0700540 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800541 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700542 }
Winson Chung64359a52013-07-08 17:17:08 -0700543 }
Winson Chung997a9232013-07-24 15:33:46 -0700544 });
545 }
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
547 };
548 runOnWorkerThread(r);
549 }
550
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 /**
552 * Adds an item to the DB if it was not created previously, or move it to a new
553 * <container, screen, cellX, cellY>
554 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800555 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700556 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 if (item.container == ItemInfo.NO_ID) {
558 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700559 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 } else {
561 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700562 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564 }
565
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700566 static void checkItemInfoLocked(
567 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
568 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
569 if (modelItem != null && item != modelItem) {
570 // check all the data is consistent
571 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
572 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
573 ShortcutInfo shortcut = (ShortcutInfo) item;
574 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
575 modelShortcut.intent.filterEquals(shortcut.intent) &&
576 modelShortcut.id == shortcut.id &&
577 modelShortcut.itemType == shortcut.itemType &&
578 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700579 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 modelShortcut.cellX == shortcut.cellX &&
581 modelShortcut.cellY == shortcut.cellY &&
582 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700583 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700584 // For all intents and purposes, this is the same object
585 return;
586 }
587 }
588
589 // the modelItem needs to match up perfectly with item if our model is
590 // to be consistent with the database-- for now, just require
591 // modelItem == item or the equality check above
592 String msg = "item: " + ((item != null) ? item.toString() : "null") +
593 "modelItem: " +
594 ((modelItem != null) ? modelItem.toString() : "null") +
595 "Error: ItemInfo passed to checkItemInfo doesn't match original";
596 RuntimeException e = new RuntimeException(msg);
597 if (stackTrace != null) {
598 e.setStackTrace(stackTrace);
599 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800600 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700601 }
602 }
603
Michael Jurka816474f2012-06-25 14:49:02 -0700604 static void checkItemInfo(final ItemInfo item) {
605 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
606 final long itemId = item.id;
607 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700608 public void run() {
609 synchronized (sBgLock) {
610 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700611 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700612 }
613 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700615 }
616
Michael Jurkac9d95c52011-08-29 14:03:34 -0700617 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
618 final ItemInfo item, final String callingFunction) {
619 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700620 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700621 final ContentResolver cr = context.getContentResolver();
622
Adam Cohen487f7dd2012-06-28 18:12:10 -0700623 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 Runnable r = new Runnable() {
625 public void run() {
626 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700627 updateItemArrays(item, itemId, stackTrace);
628 }
629 };
630 runOnWorkerThread(r);
631 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700633 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
634 final ArrayList<ItemInfo> items, final String callingFunction) {
635 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700636
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
638 Runnable r = new Runnable() {
639 public void run() {
640 ArrayList<ContentProviderOperation> ops =
641 new ArrayList<ContentProviderOperation>();
642 int count = items.size();
643 for (int i = 0; i < count; i++) {
644 ItemInfo item = items.get(i);
645 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700646 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700648
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
650 updateItemArrays(item, itemId, stackTrace);
651
652 }
653 try {
654 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
655 } catch (Exception e) {
656 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700657 }
658 }
659 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700660 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700661 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700662
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
664 // Lock on mBgLock *after* the db operation
665 synchronized (sBgLock) {
666 checkItemInfoLocked(itemId, item, stackTrace);
667
668 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
669 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
670 // Item is in a folder, make sure this folder exists
671 if (!sBgFolders.containsKey(item.container)) {
672 // An items container is being set to a that of an item which is not in
673 // the list of Folders.
674 String msg = "item: " + item + " container being set to: " +
675 item.container + ", not in the list of folders";
676 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 }
678 }
679
680 // Items are added/removed from the corresponding FolderInfo elsewhere, such
681 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
682 // that are on the desktop, as appropriate
683 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800684 if (modelItem != null &&
685 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
686 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 switch (modelItem.itemType) {
688 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
689 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700690 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
692 if (!sBgWorkspaceItems.contains(modelItem)) {
693 sBgWorkspaceItems.add(modelItem);
694 }
695 break;
696 default:
697 break;
698 }
699 } else {
700 sBgWorkspaceItems.remove(modelItem);
701 }
702 }
703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700707 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700708 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 item.cellX = cellX;
712 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713
Winson Chung3d503fb2011-07-13 17:25:49 -0700714 // We store hotseat items in canonical form which is this orientation invariant position
715 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722
723 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
726 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800727 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700731 }
732
733 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
735 * cellX, cellY have already been updated on the ItemInfos.
736 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800737 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 final long container, final int screen) {
739
740 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
741 int count = items.size();
742
743 for (int i = 0; i < count; i++) {
744 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 item.container = container;
746
747 // We store hotseat items in canonical form which is this orientation invariant position
748 // in the hotseat
749 if (context instanceof Launcher && screen < 0 &&
750 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700751 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 item.cellY);
753 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700754 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755 }
756
757 final ContentValues values = new ContentValues();
758 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
759 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
760 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800761 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763
764 contentValues.add(values);
765 }
766 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
767 }
768
769 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700770 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800771 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700772 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700774 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800775 item.cellX = cellX;
776 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 item.spanX = spanX;
778 item.spanY = spanY;
779
780 // We store hotseat items in canonical form which is this orientation invariant position
781 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 }
Adam Cohend4844c32011-02-18 19:25:06 -0800788
Adam Cohend4844c32011-02-18 19:25:06 -0800789 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800790 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
792 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800793 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
795 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800797
Michael Jurka816474f2012-06-25 14:49:02 -0700798 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800
801 /**
802 * Update an item to the database in a specified container.
803 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700804 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700805 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100806 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700807 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800808 }
809
Sunny Goyal756a28a2015-04-23 17:07:55 -0700810 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700811 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700812 synchronized (mLock) {
813 if (!mHasLoaderCompletedOnce ||
814 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
815 throw new RuntimeException("Trying to add shortcut while loader is running");
816 }
817 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700818 }
819 }
820
Adam Cohend4844c32011-02-18 19:25:06 -0800821 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700822 * Returns true if the shortcuts already exists on the workspace. This must be called after
823 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700825 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
826 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700827 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700828 if (intent.getComponent() != null) {
829 // If component is not null, an intent with null package will produce
830 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700831 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700832 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700833 intentWithPkg = intent.toUri(0);
834 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700835 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700836 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
837 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700838 }
839 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700840 intentWithPkg = intent.toUri(0);
841 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700843
844 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700845 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 if (item instanceof ShortcutInfo) {
847 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700848 Intent targetIntent = info.promisedIntent == null
849 ? info.intent : info.promisedIntent;
850 if (targetIntent != null && info.user.equals(user)) {
851 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700852 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
853 return true;
854 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700855 }
856 }
857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700859 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700860 }
861
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 * Add an item to the database in a specified container. Sets the container, screen, cellX and
864 * cellY fields of the item. Also assigns an ID to the item.
865 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700866 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700867 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 item.cellX = cellX;
870 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 // We store hotseat items in canonical form which is this orientation invariant position
872 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700874 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879
880 final ContentValues values = new ContentValues();
881 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100882 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Sunny Goyald2497482015-09-22 18:24:19 -0700884 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
885 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
886
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700887 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700888
Jason Monk8e19cf22014-03-20 15:06:57 -0400889 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700890 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700891 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700892 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700895 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400896 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700897 sBgItemsIdMap.put(item.id, item);
898 switch (item.itemType) {
899 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
900 sBgFolders.put(item.id, (FolderInfo) item);
901 // Fall through
902 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
903 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700904 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
906 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
907 sBgWorkspaceItems.add(item);
908 } else {
909 if (!sBgFolders.containsKey(item.container)) {
910 // Adding an item to a folder that doesn't exist.
911 String msg = "adding item: " + item + " to a folder that " +
912 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700913 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700915 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700916 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700917 incrementPinnedShortcutCount(
918 ShortcutKey.fromItemInfo(item), true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700919 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920 break;
921 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
922 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
923 break;
924 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 }
926 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700927 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700929 }
930
Sunny Goyal34942622014-08-29 17:20:55 -0700931 private static ArrayList<ItemInfo> getItemsByPackageName(
932 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700933 ItemInfoFilter filter = new ItemInfoFilter() {
934 @Override
935 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
936 return cn.getPackageName().equals(pn) && info.user.equals(user);
937 }
938 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700939 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700940 }
941
942 /**
943 * Removes all the items from the database corresponding to the specified package.
944 */
945 static void deletePackageFromDatabase(Context context, final String pn,
946 final UserHandleCompat user) {
947 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700948 }
949
950 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700951 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700953 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700954 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
955 items.add(item);
956 deleteItemsFromDatabase(context, items);
957 }
958
959 /**
960 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700962 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700964 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700965 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700966 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700967 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700969
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 // Lock on mBgLock *after* the db operation
971 synchronized (sBgLock) {
972 switch (item.itemType) {
973 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
974 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700975 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700976 if (info.container == item.id) {
977 // We are deleting a folder which still contains items that
978 // think they are contained by that folder.
979 String msg = "deleting a folder (" + item + ") which still " +
980 "contains items (" + info + ")";
981 Log.e(TAG, msg);
982 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 sBgWorkspaceItems.remove(item);
985 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700986 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700987 decrementPinnedShortcutCount(ShortcutKey.fromItemInfo(item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700988 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
990 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
991 sBgWorkspaceItems.remove(item);
992 break;
993 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
994 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
995 break;
996 }
997 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 }
1000 }
Michael Jurka83df1882011-08-31 20:59:26 -07001001 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 }
1004
1005 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001006 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1007 */
1008 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1009 synchronized (sBgLock) {
1010 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1011 if (count == null || --count.value == 0) {
1012 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1013 }
1014 }
1015 }
1016
1017 /**
1018 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1019 *
1020 * As an optimization, the caller can pass shouldPin == false to avoid
1021 * unnecessary RPC's if the shortcut is already pinned.
1022 */
1023 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1024 synchronized (sBgLock) {
1025 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1026 if (count == null) {
1027 count = new MutableInt(1);
1028 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1029 } else {
1030 count.value++;
1031 }
1032 if (shouldPin && count.value == 1) {
1033 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1034 }
1035 }
1036 }
1037
1038 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001039 * Update the order of the workspace screens in the database. The array list contains
1040 * a list of screen ids in the order that they should appear.
1041 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001042 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001043 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001044 final ContentResolver cr = context.getContentResolver();
1045 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1046
1047 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001048 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 while (iter.hasNext()) {
1050 long id = iter.next();
1051 if (id < 0) {
1052 iter.remove();
1053 }
1054 }
1055
1056 Runnable r = new Runnable() {
1057 @Override
1058 public void run() {
Yura085c8532014-02-11 15:15:29 +00001059 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001061 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001062 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 for (int i = 0; i < count; i++) {
1064 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001065 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1067 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001068 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 }
Yura085c8532014-02-11 15:15:29 +00001070
1071 try {
1072 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1073 } catch (Exception ex) {
1074 throw new RuntimeException(ex);
1075 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001076
Winson Chungba9c37f2013-08-30 14:11:37 -07001077 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001078 sBgWorkspaceScreens.clear();
1079 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001080 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 }
1082 };
1083 runOnWorkerThread(r);
1084 }
1085
1086 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001087 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 */
Winsonc0b52fe2015-09-09 16:38:15 -07001089 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 final ContentResolver cr = context.getContentResolver();
1091
Michael Jurkac9d95c52011-08-29 14:03:34 -07001092 Runnable r = new Runnable() {
1093 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001094 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001096 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001097 sBgItemsIdMap.remove(info.id);
1098 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 sBgWorkspaceItems.remove(info);
1100 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001101
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001102 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001103 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001104 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001105 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 for (ItemInfo childInfo : info.contents) {
1107 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001110 }
1111 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 }
1114
1115 /**
1116 * Set this as the current Launcher activity object for the loader.
1117 */
1118 public void initialize(Callbacks callbacks) {
1119 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001120 Preconditions.assertUIThread();
1121 // Remove any queued UI runnables
1122 mHandler.cancelAll();
1123 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 }
1125 }
1126
Kenny Guyed131872014-04-30 03:02:21 +01001127 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001128 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001129 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001130 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001131 user));
1132 }
1133
1134 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001135 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001136 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001137 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001138 user));
1139 }
1140
1141 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001142 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001143 int op = PackageUpdatedTask.OP_ADD;
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 onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001150 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001151 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001152 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001153 }
1154
1155 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001156 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001157 boolean replacing) {
1158 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001159 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001160 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1161 user));
1162 }
Kenny Guyed131872014-04-30 03:02:21 +01001163 }
1164
Kenny Guy44cba692016-01-21 19:50:02 +00001165 @Override
1166 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001167 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001168 PackageUpdatedTask.OP_SUSPEND, packageNames,
1169 user));
1170 }
1171
1172 @Override
1173 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001174 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001175 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1176 user));
1177 }
1178
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001179 @Override
1180 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1181 UserHandleCompat user) {
1182 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1183 }
1184
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1187 * ACTION_PACKAGE_CHANGED.
1188 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001189 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001190 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001191 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001192
Joe Onorato36115782010-06-17 13:28:48 -04001193 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001194 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001195 // If we have changed locale we need to clear out the labels in all apps/workspace.
1196 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001197 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1198 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001199 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001200 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001201 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1202 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1203 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001204 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1205 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001206 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1207 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1208 enqueueItemUpdatedTask(new PackageUpdatedTask(
1209 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1210 new String[0], user));
1211 }
1212
1213 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1214 // we need to run the state change task again.
1215 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1216 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1217 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1218 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001219 }
Tony Wickham827cef22016-03-17 15:39:39 -07001220 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1221 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001222 }
1223 }
1224
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001225 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001226 resetLoadedState(true, true);
1227
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // Do this here because if the launcher activity is running it will be restarted.
1229 // If it's not running startLoaderFromBackground will merely tell it that it needs
1230 // to reload.
1231 startLoaderFromBackground();
1232 }
1233
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1235 synchronized (mLock) {
1236 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1237 // mWorkspaceLoaded to true later
1238 stopLoaderLocked();
1239 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1240 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001241 // Always reset deep shortcuts loaded.
1242 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001243 }
1244 }
1245
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001246 /**
1247 * When the launcher is in the background, it's possible for it to miss paired
1248 * configuration changes. So whenever we trigger the loader from the background
1249 * tell the launcher that it needs to re-run the loader when it comes back instead
1250 * of doing it now.
1251 */
1252 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001253 Callbacks callbacks = getCallback();
1254 if (callbacks != null) {
1255 // Only actually run the loader if they're not paused.
1256 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001257 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001258 }
1259 }
Joe Onorato36115782010-06-17 13:28:48 -04001260 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001261
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001262 /**
1263 * If there is already a loader task running, tell it to stop.
1264 */
1265 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001266 LoaderTask oldTask = mLoaderTask;
1267 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001268 oldTask.stopLocked();
1269 }
Reena Lee93f824a2011-09-23 17:20:28 -07001270 }
1271
Adam Cohen1a85c582014-09-30 09:48:49 -07001272 public boolean isCurrentCallbacks(Callbacks callbacks) {
1273 return (mCallbacks != null && mCallbacks.get() == callbacks);
1274 }
1275
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001276 /**
1277 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1278 * @return true if the page could be bound synchronously.
1279 */
1280 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001281 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1282 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001283 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001284 // Don't bother to start the thread if we know it's not going to do anything
1285 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001286 final Callbacks oldCallbacks = mCallbacks.get();
1287 // Clear any pending bind-runnables from the synchronized load process.
1288 runOnMainThread(new Runnable() {
1289 public void run() {
1290 oldCallbacks.clearPendingBinds();
1291 }
1292 });
1293
Joe Onorato36115782010-06-17 13:28:48 -04001294 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001295 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001296 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001297 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1298 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001299 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001300 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 } else {
1302 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1303 sWorker.post(mLoaderTask);
1304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001307 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 public void stopLoader() {
1311 synchronized (mLock) {
1312 if (mLoaderTask != null) {
1313 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
1315 }
Joe Onorato36115782010-06-17 13:28:48 -04001316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001318 /**
1319 * Loads the workspace screen ids in an ordered list.
1320 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001321 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001322 final ContentResolver contentResolver = context.getContentResolver();
1323 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001324
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001325 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001326 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1327 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001328 }
1329
Joe Onorato36115782010-06-17 13:28:48 -04001330 /**
1331 * Runnable for the thread that loads the contents of the launcher:
1332 * - workspace icons
1333 * - widgets
1334 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001335 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001336 */
1337 private class LoaderTask implements Runnable {
1338 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001339 private int mPageToBindFirst;
1340
Adam Cohen091440a2015-03-18 14:16:05 -07001341 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001342 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001343 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001344
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001345 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001346 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001347 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 }
1349
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001350 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001351 mIsLoadingAndBindingWorkspace = true;
1352
Joe Onorato36115782010-06-17 13:28:48 -04001353 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001357
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001358 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001359 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001360 synchronized (LoaderTask.this) {
1361 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001363 }
1364 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 }
1366 }
1367
Joe Onorato36115782010-06-17 13:28:48 -04001368 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001369 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001370 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001371
Joe Onorato36115782010-06-17 13:28:48 -04001372 private void waitForIdle() {
1373 // Wait until the either we're stopped or the other threads are done.
1374 // This way we don't start loading all apps until the workspace has settled
1375 // down.
1376 synchronized (LoaderTask.this) {
1377 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001378
Joe Onorato36115782010-06-17 13:28:48 -04001379 mHandler.postIdle(new Runnable() {
1380 public void run() {
1381 synchronized (LoaderTask.this) {
1382 mLoadAndBindStepFinished = true;
1383 if (DEBUG_LOADERS) {
1384 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001385 }
Joe Onorato36115782010-06-17 13:28:48 -04001386 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001387 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389 });
1390
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001391 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001392 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001393 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1394 // wait no longer than 1sec at a time
1395 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001396 } catch (InterruptedException ex) {
1397 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001398 }
1399 }
Joe Onorato36115782010-06-17 13:28:48 -04001400 if (DEBUG_LOADERS) {
1401 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001402 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001403 + "ms for previous step to finish binding");
1404 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001407
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001409 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 // Ensure that we have a valid page index to load synchronously
1411 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1412 "valid page index");
1413 }
1414 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1415 // Ensure that we don't try and bind a specified page when the pages have not been
1416 // loaded already (we should load everything asynchronously in that case)
1417 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1418 }
1419 synchronized (mLock) {
1420 if (mIsLoaderTaskRunning) {
1421 // Ensure that we are never running the background loading at this point since
1422 // we also touch the background collections
1423 throw new RuntimeException("Error! Background loading is already running");
1424 }
1425 }
1426
1427 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1428 // data structures, we can't allow any other thread to touch that data, but because
1429 // this call is synchronous, we can get away with not locking).
1430
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001431 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001432 // operations from the previous activity. We need to ensure that all queued operations
1433 // are executed before any synchronous binding work is done.
1434 mHandler.flush();
1435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001436 // Divide the set of loaded items into those that we are binding synchronously, and
1437 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001438 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1440 // arise from that.
1441 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001442
1443 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 }
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001448 if (mStopped) {
1449 return;
1450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 mIsLoaderTaskRunning = true;
1452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 // Optimize for end-user experience: if the Launcher is up and // running with the
1454 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1455 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001456 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001457 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001458 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001459
Joe Onorato36115782010-06-17 13:28:48 -04001460 if (mStopped) {
1461 break keep_running;
1462 }
1463
Joe Onorato36115782010-06-17 13:28:48 -04001464 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001465
1466 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001467 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1468 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001469
1470 waitForIdle();
1471
1472 // third step
1473 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1474 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Clear out this reference, otherwise we end up holding it until all of the
1478 // callback runnables are done.
1479 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480
Joe Onorato36115782010-06-17 13:28:48 -04001481 synchronized (mLock) {
1482 // If we are still the last one to be scheduled, remove ourselves.
1483 if (mLoaderTask == this) {
1484 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001486 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001487 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 }
1490
1491 public void stopLocked() {
1492 synchronized (LoaderTask.this) {
1493 mStopped = true;
1494 this.notify();
1495 }
1496 }
1497
1498 /**
1499 * Gets the callbacks object. If we've been stopped, or if the launcher object
1500 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1501 * object that was around when the deferred message was scheduled, and if there's
1502 * a new Callbacks object around then also return null. This will save us from
1503 * calling onto it with data that will be ignored.
1504 */
1505 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1506 synchronized (mLock) {
1507 if (mStopped) {
1508 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510
1511 if (mCallbacks == null) {
1512 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514
1515 final Callbacks callbacks = mCallbacks.get();
1516 if (callbacks != oldCallbacks) {
1517 return null;
1518 }
1519 if (callbacks == null) {
1520 Log.w(TAG, "no mCallbacks");
1521 return null;
1522 }
1523
1524 return callbacks;
1525 }
1526 }
1527
1528 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001529 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001530 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001531 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001532 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001533
Adam Cohendcd297f2013-06-18 13:13:40 -07001534 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001535 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001536 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001537 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1538 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001539 Log.e(TAG, "Error loading shortcut into hotseat " + item
1540 + " into position (" + item.screenId + ":" + item.cellX + ","
1541 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001542 return false;
1543 }
1544
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001545 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001546 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1547
Adam Cohen2e6da152015-05-06 11:42:25 -07001548 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001549 Log.e(TAG, "Error loading shortcut " + item
1550 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001551 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001552 + ")");
1553 return false;
1554 }
1555
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001556 if (hotseatOccupancy != null) {
1557 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 Log.e(TAG, "Error loading shortcut into hotseat " + item
1559 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001560 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001562 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001563 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001564 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001566 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1568 occupancy.cells[(int) item.screenId][0] = true;
1569 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001570 return true;
1571 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001572 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1573 if (!workspaceScreens.contains((Long) item.screenId)) {
1574 // The item has an invalid screen id.
1575 return false;
1576 }
1577 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001578 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001579 return true;
1580 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001582 final int countX = profile.numColumns;
1583 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001584 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1585 item.cellX < 0 || item.cellY < 0 ||
1586 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1587 Log.e(TAG, "Error loading shortcut " + item
1588 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1589 + item.cellX + "," + item.cellY
1590 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1591 return false;
1592 }
1593
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001594 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001595 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1596 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001597 // Mark the first row as occupied (if the feature is enabled)
1598 // in order to account for the QSB.
1599 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001600 }
1601 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001602 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001603 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001604
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001605 // Check if any workspace icons overlap with each other
1606 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1607 occupancy.markCells(item, true);
1608 return true;
1609 } else {
1610 Log.e(TAG, "Error loading shortcut " + item
1611 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1612 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1613 + ") already occupied");
1614 return false;
1615 }
Joe Onorato36115782010-06-17 13:28:48 -04001616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617
Winson Chungba9c37f2013-08-30 14:11:37 -07001618 /** Clears all the sBg data structures */
1619 private void clearSBgDataStructures() {
1620 synchronized (sBgLock) {
1621 sBgWorkspaceItems.clear();
1622 sBgAppWidgets.clear();
1623 sBgFolders.clear();
1624 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001625 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001626 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 }
1628 }
1629
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001630 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001631 if (LauncherAppState.PROFILE_STARTUP) {
1632 Trace.beginSection("Loading Workspace");
1633 }
Joe Onorato36115782010-06-17 13:28:48 -04001634 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635
Joe Onorato36115782010-06-17 13:28:48 -04001636 final Context context = mContext;
1637 final ContentResolver contentResolver = context.getContentResolver();
1638 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001639 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001640 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001641 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001642
Winson Chung892c74d2013-08-22 16:15:50 -07001643 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001644 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001645 int countX = profile.numColumns;
1646 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001647
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001648 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001649 try {
1650 ImportDataTask.performImportIfPossible(context);
1651 } catch (Exception e) {
1652 // Migration failed. Clear workspace.
1653 clearDb = true;
1654 }
1655
1656 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001657 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1658 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001659 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001660 }
1661
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001662 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001663 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001664 LauncherSettings.Settings.call(contentResolver,
1665 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001666 }
1667
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001668 Log.d(TAG, "loadWorkspace: loading default favorites");
1669 LauncherSettings.Settings.call(contentResolver,
1670 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001671
Winson Chung2abf94d2012-07-18 18:16:38 -07001672 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001673 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001674 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001675 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001676 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001677
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001678 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1679 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001680 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001681 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001682 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001683 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001684
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001685 // +1 for the hotseat (it can be larger than the workspace)
1686 // Load workspace in reverse order to ensure that latest items are loaded first (and
1687 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001688 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001689 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 try {
1692 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1693 final int intentIndex = c.getColumnIndexOrThrow
1694 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 final int containerIndex = c.getColumnIndexOrThrow(
1696 LauncherSettings.Favorites.CONTAINER);
1697 final int itemTypeIndex = c.getColumnIndexOrThrow(
1698 LauncherSettings.Favorites.ITEM_TYPE);
1699 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1700 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001701 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1702 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 final int screenIndex = c.getColumnIndexOrThrow(
1704 LauncherSettings.Favorites.SCREEN);
1705 final int cellXIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.CELLX);
1707 final int cellYIndex = c.getColumnIndexOrThrow
1708 (LauncherSettings.Favorites.CELLY);
1709 final int spanXIndex = c.getColumnIndexOrThrow
1710 (LauncherSettings.Favorites.SPANX);
1711 final int spanYIndex = c.getColumnIndexOrThrow(
1712 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001713 final int rankIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001715 final int restoredIndex = c.getColumnIndexOrThrow(
1716 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001717 final int profileIdIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001719 final int optionsIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001721 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722
Sunny Goyal7f834d22015-04-21 10:10:23 -07001723 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001724 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001725 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001726 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001727 long serialNo = mUserManager.getSerialNumberForUser(user);
1728 allUsers.put(serialNo, user);
1729 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001730
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001731 boolean userUnlocked = mUserManager.isUserUnlocked(user);
1732 unlockedUsers.put(serialNo, userUnlocked);
1733
1734 // We can only query for shortcuts when the user is unlocked.
1735 if (userUnlocked) {
1736 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1737 .queryForPinnedShortcuts(null, user);
1738 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1739 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1740 shortcut);
1741 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001742 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001743 }
1744
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 ShortcutInfo info;
1746 String intentDescription;
1747 LauncherAppWidgetInfo appWidgetInfo;
1748 int container;
1749 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001750 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001752 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001753 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 while (!mStopped && c.moveToNext()) {
1756 try {
1757 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001758 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001759 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001760 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 switch (itemType) {
1763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001765 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001766 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001768 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001769 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001770 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001771 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001772 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001773 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001774 if (user == null) {
1775 // User has been deleted remove the item.
1776 itemsToRemove.add(id);
1777 continue;
1778 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 try {
1780 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001781 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001782 if (cn != null && cn.getPackageName() != null) {
1783 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1784 cn.getPackageName(), user);
1785 boolean validComponent = validPkg &&
1786 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001787 if (validPkg) {
1788 targetPackage = cn.getPackageName();
1789 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001790
1791 if (validComponent) {
1792 if (restored) {
1793 // no special handling necessary for this item
1794 restoredRows.add(id);
1795 restored = false;
1796 }
Kenny Guyff05f432016-01-22 17:48:29 +00001797 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001798 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001799 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001800 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001801 intent = null;
1802 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1803 // We allow auto install apps to have their intent
1804 // updated after an install.
1805 intent = manager.getLaunchIntentForPackage(
1806 cn.getPackageName());
1807 if (intent != null) {
1808 ContentValues values = new ContentValues();
1809 values.put(LauncherSettings.Favorites.INTENT,
1810 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001811 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001812 }
1813 }
1814
1815 if (intent == null) {
1816 // The app is installed but the component is no
1817 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001818 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001819 itemsToRemove.add(id);
1820 continue;
1821 } else {
1822 // no special handling necessary for this item
1823 restoredRows.add(id);
1824 restored = false;
1825 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001826 } else if (restored) {
1827 // Package is not yet available but might be
1828 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001829 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001830
1831 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1832 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001833 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001834 // App restore has started. Update the flag
1835 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1836 ContentValues values = new ContentValues();
1837 values.put(LauncherSettings.Favorites.RESTORED,
1838 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 updateItem(id, values);
1840 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1841 // This is a common app. Try to replace this.
1842 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1843 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1844 if (parser.findDefaultApp()) {
1845 // Default app found. Replace it.
1846 intent = parser.parsedIntent;
1847 cn = intent.getComponent();
1848 ContentValues values = parser.parsedValues;
1849 values.put(LauncherSettings.Favorites.RESTORED, 0);
1850 updateItem(id, values);
1851 restored = false;
1852 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001853
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001854 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001855 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001856 itemsToRemove.add(id);
1857 continue;
1858 }
Sunny Goyal94485362014-09-18 16:13:58 -07001859 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001860 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001861 itemsToRemove.add(id);
1862 continue;
1863 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001864 } else if (PackageManagerHelper.isAppOnSdcard(
1865 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001866 // Package is present but not available.
1867 allowMissingTarget = true;
1868 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1869 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001870 // SdCard is not ready yet. Package might get available,
1871 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001872 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001873 HashSet<String> pkgs = sPendingPackages.get(user);
1874 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001875 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001876 sPendingPackages.put(user, pkgs);
1877 }
1878 pkgs.add(cn.getPackageName());
1879 allowMissingTarget = true;
1880 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001881
1882 } else {
1883 // Do not wait for external media load anymore.
1884 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001885 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001886 itemsToRemove.add(id);
1887 continue;
Winson Chungee055712013-07-30 14:46:24 -07001888 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001889 } else if (cn == null) {
1890 // For shortcuts with no component, keep them as they are
1891 restoredRows.add(id);
1892 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001895 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001896 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 continue;
1898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899
Sunny Goyal34b65272015-03-11 16:56:52 -07001900 boolean useLowResIcon = container >= 0 &&
1901 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1902
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001903 if (itemReplaced) {
1904 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001905 info = getAppShortcutInfo(intent, user, null,
1906 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001907 } else {
1908 // Don't replace items for other profiles.
1909 itemsToRemove.add(id);
1910 continue;
1911 }
1912 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001913 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001914 info = getRestoredItemInfo(c, intent,
1915 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001916 intent = getRestoredItemIntent(c, context, intent);
1917 } else {
1918 // Don't restore items for other profiles.
1919 itemsToRemove.add(id);
1920 continue;
1921 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001922 } else if (itemType ==
1923 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001924 info = getAppShortcutInfo(intent, user, c,
1925 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001926 } else if (itemType ==
1927 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001928
1929 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1930 if (unlockedUsers.get(serialNumber)) {
1931 ShortcutInfoCompat pinnedShortcut =
1932 shortcutKeyToPinnedShortcuts.get(key);
1933 if (pinnedShortcut == null) {
1934 // The shortcut is no longer valid.
1935 itemsToRemove.add(id);
1936 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001937 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001938 info = new ShortcutInfo(pinnedShortcut, context);
1939 intent = info.intent;
1940 } else {
1941 // Create a shortcut info in disabled mode for now.
1942 info = new ShortcutInfo();
1943 info.user = user;
1944 info.itemType = itemType;
1945 loadInfoFromCursor(info, c, cursorIconInfo);
1946
1947 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001948 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001949 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001950 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001951 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001952
Sunny Goyald09c3702016-04-06 16:18:20 -07001953 // Shortcuts are only available on the primary profile
1954 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1955 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1956 }
1957
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001958 // App shortcuts that used to be automatically added to Launcher
1959 // didn't always have the correct intent flags set, so do that
1960 // here
1961 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001962 intent.getCategories() != null &&
1963 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001964 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 intent.addFlags(
1966 Intent.FLAG_ACTIVITY_NEW_TASK |
1967 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1968 }
Michael Jurka96879562012-03-22 05:54:33 -07001969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001972 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001975 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 info.cellX = c.getInt(cellXIndex);
1977 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001978 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001979 info.spanX = 1;
1980 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001981 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001982 if (info.promisedIntent != null) {
1983 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1984 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001985 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001986 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1987 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1988 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001989
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001991 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001992 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 break;
1994 }
1995
Sunny Goyal756adbc2015-04-16 15:20:51 -07001996 if (restored) {
1997 ComponentName cn = info.getTargetComponent();
1998 if (cn != null) {
1999 Integer progress = installingPkgs.get(cn.getPackageName());
2000 if (progress != null) {
2001 info.setInstallProgress(progress);
2002 } else {
2003 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2004 }
2005 }
2006 }
2007
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 switch (container) {
2009 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2010 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2011 sBgWorkspaceItems.add(info);
2012 break;
2013 default:
2014 // Item is in a user folder
2015 FolderInfo folderInfo =
2016 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002017 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 break;
2019 }
2020 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002021 } else {
2022 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 }
2024 break;
2025
2026 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2027 id = c.getLong(idIndex);
2028 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2029
Sunny Goyala508e4f2015-05-21 09:33:57 -07002030 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002031 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002034 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 folderInfo.cellX = c.getInt(cellXIndex);
2036 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002037 folderInfo.spanX = 1;
2038 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002039 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040
Daniel Sandler8802e962010-05-26 16:28:16 -04002041 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002042 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002043 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002044 break;
2045 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002046
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2049 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2050 sBgWorkspaceItems.add(folderInfo);
2051 break;
Joe Onorato36115782010-06-17 13:28:48 -04002052 }
Joe Onorato17a89222011-02-08 17:26:11 -08002053
Chris Wrenf4d08112014-01-16 18:13:56 -05002054 if (restored) {
2055 // no special handling required for restored folders
2056 restoredRows.add(id);
2057 }
2058
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2060 sBgFolders.put(folderInfo.id, folderInfo);
2061 break;
2062
2063 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002064 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002066 boolean customWidget = itemType ==
2067 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2068
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002070 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002071 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002072 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002073 user = allUsers.get(serialNumber);
2074 if (user == null) {
2075 itemsToRemove.add(id);
2076 continue;
2077 }
2078
Sunny Goyalff572272014-07-23 13:58:07 -07002079 final ComponentName component =
2080 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002081
Sunny Goyal651077b2014-06-30 14:15:31 -07002082 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002083 final boolean isIdValid = (restoreStatus &
2084 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002085 final boolean wasProviderReady = (restoreStatus &
2086 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002087
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002088 if (widgetProvidersMap == null) {
2089 widgetProvidersMap = AppWidgetManagerCompat
2090 .getInstance(mContext).getAllProvidersMap();
2091 }
2092 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2093 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002094 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002095 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002096
2097 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002098 if (!isSafeMode && !customWidget &&
2099 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002100 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002101 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 itemsToRemove.add(id);
2103 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002104 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002105 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2106 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002107
Sunny Goyal53f96722015-07-13 19:54:53 -07002108 // The provider is available. So the widget is either
2109 // available or not available. We do not need to track
2110 // any future restore updates.
2111 int status = restoreStatus &
2112 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002113 if (!wasProviderReady) {
2114 // If provider was not previously ready, update the
2115 // status and UI flag.
2116
2117 // Id would be valid only if the widget restore broadcast was received.
2118 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002119 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002120 } else {
2121 status &= ~LauncherAppWidgetInfo
2122 .FLAG_PROVIDER_NOT_READY;
2123 }
2124 }
2125 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002126 } else {
2127 Log.v(TAG, "Widget restore pending id=" + id
2128 + " appWidgetId=" + appWidgetId
2129 + " status =" + restoreStatus);
2130 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002131 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002132 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002133 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002134
2135 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2136 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002137 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002138 // App restore has started. Update the flag
2139 appWidgetInfo.restoreStatus |=
2140 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002141 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002142 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002143 itemsToRemove.add(id);
2144 continue;
2145 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002146
2147 appWidgetInfo.installProgress =
2148 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002149 }
Sunny Goyalff572272014-07-23 13:58:07 -07002150
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002152 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 appWidgetInfo.cellX = c.getInt(cellXIndex);
2154 appWidgetInfo.cellY = c.getInt(cellYIndex);
2155 appWidgetInfo.spanX = c.getInt(spanXIndex);
2156 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002157 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002158
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2160 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2161 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002162 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2163 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 continue;
2165 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002167 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002168 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002169 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002170 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 break;
2172 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002173
Adam Cohen59400422014-03-05 18:07:04 -08002174 if (!customWidget) {
2175 String providerName =
2176 appWidgetInfo.providerName.flattenToString();
2177 if (!providerName.equals(savedProvider) ||
2178 (appWidgetInfo.restoreStatus != restoreStatus)) {
2179 ContentValues values = new ContentValues();
2180 values.put(
2181 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2182 providerName);
2183 values.put(LauncherSettings.Favorites.RESTORED,
2184 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002185 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002186 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002187 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2189 sBgAppWidgets.add(appWidgetInfo);
2190 }
Joe Onorato36115782010-06-17 13:28:48 -04002191 break;
2192 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002194 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002195 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 }
2197 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002198 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 }
2200
Winson Chungba9c37f2013-08-30 14:11:37 -07002201 // Break early if we've stopped loading
2202 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002203 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002204 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002205 }
2206
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002209 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2210 Utilities.createDbSelectionQuery(
2211 LauncherSettings.Favorites._ID, itemsToRemove), null);
2212 if (DEBUG_LOADERS) {
2213 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2214 LauncherSettings.Favorites._ID, itemsToRemove));
2215 }
2216
2217 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002218 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2219 .call(contentResolver,
2220 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2221 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2222 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002223 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2224 sBgFolders.remove(folderId);
2225 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002226 }
2227 }
2228
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002229 // Unpin shortcuts that don't exist on the workspace.
2230 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2231 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2232 if (numTimesPinned == null || numTimesPinned.value == 0) {
2233 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2234 mDeepShortcutManager.unpinShortcut(key);
2235 }
2236 }
2237
Sunny Goyal317698b2015-07-29 11:45:41 -07002238 // Sort all the folder items and make sure the first 3 items are high resolution.
2239 for (FolderInfo folder : sBgFolders) {
2240 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2241 int pos = 0;
2242 for (ShortcutInfo info : folder.contents) {
2243 if (info.usingLowResIcon) {
2244 info.updateIcon(mIconCache, false);
2245 }
2246 pos ++;
2247 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2248 break;
2249 }
2250 }
2251 }
2252
Chris Wrenf4d08112014-01-16 18:13:56 -05002253 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002254 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002255 ContentValues values = new ContentValues();
2256 values.put(LauncherSettings.Favorites.RESTORED, 0);
2257 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2258 Utilities.createDbSelectionQuery(
2259 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002260 }
2261
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002262 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2263 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002264 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002265 null, sWorker);
2266 }
2267
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002268 // Remove any empty screens
2269 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002270 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002271 long screenId = item.screenId;
2272 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2273 unusedScreens.contains(screenId)) {
2274 unusedScreens.remove(screenId);
2275 }
2276 }
2277
2278 // If there are any empty screens remove them, and update.
2279 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002280 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 }
2283
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 if (DEBUG_LOADERS) {
2285 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2286 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002288 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002290
Sunny Goyale2df0622015-04-24 11:27:00 -07002291 for (int i = 0; i < nScreens; i++) {
2292 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002293 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 line += " | ";
2295 }
Joe Onorato36115782010-06-17 13:28:48 -04002296 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002298 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002299 }
Joe Onorato36115782010-06-17 13:28:48 -04002300 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002301 if (LauncherAppState.PROFILE_STARTUP) {
2302 Trace.endSection();
2303 }
Adam Cohene25af792013-06-06 23:08:25 -07002304 }
2305
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002306 /**
2307 * Partially updates the item without any notification. Must be called on the worker thread.
2308 */
2309 private void updateItem(long itemId, ContentValues update) {
2310 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002311 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002312 update,
2313 BaseColumns._ID + "= ?",
2314 new String[]{Long.toString(itemId)});
2315 }
2316
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 /** Filters the set of items who are directly or indirectly (via another container) on the
2318 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002319 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 ArrayList<ItemInfo> allWorkspaceItems,
2321 ArrayList<ItemInfo> currentScreenItems,
2322 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002323 // Purge any null ItemInfos
2324 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2325 while (iter.hasNext()) {
2326 ItemInfo i = iter.next();
2327 if (i == null) {
2328 iter.remove();
2329 }
2330 }
2331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 // Order the set of items by their containers first, this allows use to walk through the
2333 // list sequentially, build up a list of containers that are in the specified screen,
2334 // as well as all items in those containers.
2335 Set<Long> itemsOnScreen = new HashSet<Long>();
2336 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2337 @Override
2338 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002339 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 }
2341 });
2342 for (ItemInfo info : allWorkspaceItems) {
2343 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002344 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 currentScreenItems.add(info);
2346 itemsOnScreen.add(info.id);
2347 } else {
2348 otherScreenItems.add(info);
2349 }
2350 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2351 currentScreenItems.add(info);
2352 itemsOnScreen.add(info.id);
2353 } else {
2354 if (itemsOnScreen.contains(info.container)) {
2355 currentScreenItems.add(info);
2356 itemsOnScreen.add(info.id);
2357 } else {
2358 otherScreenItems.add(info);
2359 }
2360 }
2361 }
2362 }
2363
2364 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002365 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 ArrayList<LauncherAppWidgetInfo> appWidgets,
2367 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2368 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369
2370 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002371 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002373 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 currentScreenWidgets.add(widget);
2375 } else {
2376 otherScreenWidgets.add(widget);
2377 }
2378 }
2379 }
2380
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2382 * right) */
2383 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002384 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002385 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002386 final int screenCols = profile.numColumns;
2387 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002389 @Override
2390 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002391 if (lhs.container == rhs.container) {
2392 // Within containers, order by their spatial position in that container
2393 switch ((int) lhs.container) {
2394 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2395 long lr = (lhs.screenId * screenCellCount +
2396 lhs.cellY * screenCols + lhs.cellX);
2397 long rr = (rhs.screenId * screenCellCount +
2398 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002399 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002400 }
2401 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2402 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002403 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002404 }
2405 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002406 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002407 throw new RuntimeException("Unexpected container type when " +
2408 "sorting workspace items.");
2409 }
2410 return 0;
2411 }
2412 } else {
2413 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002414 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002415 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002416 }
2417 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002419
Adam Cohendcd297f2013-06-18 13:13:40 -07002420 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2421 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002422 final Runnable r = new Runnable() {
2423 @Override
2424 public void run() {
2425 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2426 if (callbacks != null) {
2427 callbacks.bindScreens(orderedScreens);
2428 }
2429 }
2430 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002431 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002432 }
2433
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002434 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2435 final ArrayList<ItemInfo> workspaceItems,
2436 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002437 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438
2439 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002440 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002442 final int start = i;
2443 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 final Runnable r = new Runnable() {
2445 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002447 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002448 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002449 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2450 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 }
2452 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002454 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 // Bind the widgets, one at a time
2458 N = appWidgets.size();
2459 for (int i = 0; i < N; i++) {
2460 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2461 final Runnable r = new Runnable() {
2462 public void run() {
2463 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2464 if (callbacks != null) {
2465 callbacks.bindAppWidget(widget);
2466 }
2467 }
2468 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002469 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 }
2471 }
2472
2473 /**
2474 * Binds all loaded data to actual views on the main thread.
2475 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002476 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 final long t = SystemClock.uptimeMillis();
2478 Runnable r;
2479
2480 // Don't use these two variables in any of the callback runnables.
2481 // Otherwise we hold a reference to them.
2482 final Callbacks oldCallbacks = mCallbacks.get();
2483 if (oldCallbacks == null) {
2484 // This launcher has exited and nobody bothered to tell us. Just bail.
2485 Log.w(TAG, "LoaderTask running with no launcher");
2486 return;
2487 }
2488
Winson Chung9b9fb962013-11-15 15:39:34 -08002489 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002490 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2491 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2492 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002493
Winson Chung2abf94d2012-07-18 18:16:38 -07002494 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 workspaceItems.addAll(sBgWorkspaceItems);
2496 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002497 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 }
2499
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002500 final int currentScreen;
2501 {
2502 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2503 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2504 if (currScreen >= orderedScreenIds.size()) {
2505 // There may be no workspace screens (just hotseat items and an empty page).
2506 currScreen = PagedView.INVALID_RESTORE_PAGE;
2507 }
2508 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002509 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002510 final boolean validFirstPage = currentScreen >= 0;
2511 final long currentScreenId =
2512 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002513
Winson Chung9b9fb962013-11-15 15:39:34 -08002514 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002515 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2516 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2517 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2518 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519
Winson Chung9b9fb962013-11-15 15:39:34 -08002520 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002522 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2525 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2526
2527 // Tell the workspace that we're about to start binding items
2528 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002529 public void run() {
2530 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2531 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002532 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002534 }
2535 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002537 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538
Adam Cohendcd297f2013-06-18 13:13:40 -07002539 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2540
Sunny Goyal527c7d32015-08-28 15:19:36 -07002541 Executor mainExecutor = new DeferredMainThreadExecutor();
2542 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002543 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002545 // In case of validFirstPage, only bind the first screen, and defer binding the
2546 // remaining screens after first onDraw (and an optional the fade animation whichever
2547 // happens later).
2548 // This ensures that the first screen is immediately visible (eg. during rotation)
2549 // In case of !validFirstPage, bind all pages one after other.
2550 final Executor deferredExecutor =
2551 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2552
2553 mainExecutor.execute(new Runnable() {
2554 @Override
2555 public void run() {
2556 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2557 if (callbacks != null) {
2558 callbacks.finishFirstPageBind(
2559 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2560 }
2561 }
2562 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002563
Sunny Goyal44c06432016-04-02 10:56:02 -07002564 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565
2566 // Tell the workspace that we're done binding items
2567 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002568 public void run() {
2569 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2570 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002571 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573
Sunny Goyal639e9062015-08-19 19:17:06 -07002574 mIsLoadingAndBindingWorkspace = false;
2575
2576 // Run all the bind complete runnables after workspace is bound.
2577 if (!mBindCompleteRunnables.isEmpty()) {
2578 synchronized (mBindCompleteRunnables) {
2579 for (final Runnable r : mBindCompleteRunnables) {
2580 runOnWorkerThread(r);
2581 }
2582 mBindCompleteRunnables.clear();
2583 }
2584 }
2585
Winson Chung98e030b2012-05-07 16:01:11 -07002586 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002587 if (DEBUG_LOADERS) {
2588 Log.d(TAG, "bound workspace in "
2589 + (SystemClock.uptimeMillis()-t) + "ms");
2590 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002591
Joe Onorato36115782010-06-17 13:28:48 -04002592 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002594 deferredExecutor.execute(r);
2595
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002596 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002597 r = new Runnable() {
2598 public void run() {
2599 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2600 if (callbacks != null) {
2601 // We are loading synchronously, which means, some of the pages will be
2602 // bound after first draw. Inform the callbacks that page binding is
2603 // not complete, and schedule the remaining pages.
2604 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2605 callbacks.onPageBoundSynchronously(currentScreen);
2606 }
2607 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2608 }
2609 }
2610 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002611 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002612 }
Joe Onorato36115782010-06-17 13:28:48 -04002613 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002614
Joe Onorato36115782010-06-17 13:28:48 -04002615 private void loadAndBindAllApps() {
2616 if (DEBUG_LOADERS) {
2617 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2618 }
2619 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002620 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002621 synchronized (LoaderTask.this) {
2622 if (mStopped) {
2623 return;
2624 }
2625 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002626 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002627 synchronized (LoaderTask.this) {
2628 if (mStopped) {
2629 return;
2630 }
2631 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002632 }
Joe Onorato36115782010-06-17 13:28:48 -04002633 } else {
2634 onlyBindAllApps();
2635 }
2636 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002637
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002638 private void updateIconCache() {
2639 // Ignore packages which have a promise icon.
2640 HashSet<String> packagesToIgnore = new HashSet<>();
2641 synchronized (sBgLock) {
2642 for (ItemInfo info : sBgItemsIdMap) {
2643 if (info instanceof ShortcutInfo) {
2644 ShortcutInfo si = (ShortcutInfo) info;
2645 if (si.isPromise() && si.getTargetComponent() != null) {
2646 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2647 }
2648 } else if (info instanceof LauncherAppWidgetInfo) {
2649 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2650 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2651 packagesToIgnore.add(lawi.providerName.getPackageName());
2652 }
2653 }
2654 }
2655 }
2656 mIconCache.updateDbIcons(packagesToIgnore);
2657 }
2658
Joe Onorato36115782010-06-17 13:28:48 -04002659 private void onlyBindAllApps() {
2660 final Callbacks oldCallbacks = mCallbacks.get();
2661 if (oldCallbacks == null) {
2662 // This launcher has exited and nobody bothered to tell us. Just bail.
2663 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2664 return;
2665 }
2666
2667 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002668 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002669 final ArrayList<AppInfo> list
2670 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002671 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002672 public void run() {
2673 final long t = SystemClock.uptimeMillis();
2674 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2675 if (callbacks != null) {
2676 callbacks.bindAllApplications(list);
2677 }
2678 if (DEBUG_LOADERS) {
2679 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002680 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002681 }
2682 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002683 };
Tony Wickham80f57872016-06-29 18:12:15 -07002684 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002685 }
2686
Winson Chung64359a52013-07-08 17:17:08 -07002687 private void loadAllApps() {
2688 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002689
Joe Onorato36115782010-06-17 13:28:48 -04002690 final Callbacks oldCallbacks = mCallbacks.get();
2691 if (oldCallbacks == null) {
2692 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002693 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002694 return;
2695 }
2696
Kenny Guyed131872014-04-30 03:02:21 +01002697 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2698
Winson Chung64359a52013-07-08 17:17:08 -07002699 // Clear the list of apps
2700 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002701 for (UserHandleCompat user : profiles) {
2702 // Query for the set of apps
2703 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002704 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002705 if (DEBUG_LOADERS) {
2706 Log.d(TAG, "getActivityList took "
2707 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2708 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2709 }
2710 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002711 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002712 if (apps == null || apps.isEmpty()) {
2713 return;
2714 }
Kenny Guyff05f432016-01-22 17:48:29 +00002715 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002716 // Create the ApplicationInfos
2717 for (int i = 0; i < apps.size(); i++) {
2718 LauncherActivityInfoCompat app = apps.get(i);
2719 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002720 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002721 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002722
Sunny Goyal756a28a2015-04-23 17:07:55 -07002723 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2724 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002725 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002726
2727 @Override
2728 public void run() {
2729 heuristic.processUserApps(apps);
2730 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002731 };
2732 runOnMainThread(new Runnable() {
2733
2734 @Override
2735 public void run() {
2736 // Check isLoadingWorkspace on the UI thread, as it is updated on
2737 // the UI thread.
2738 if (mIsLoadingAndBindingWorkspace) {
2739 synchronized (mBindCompleteRunnables) {
2740 mBindCompleteRunnables.add(r);
2741 }
2742 } else {
2743 runOnWorkerThread(r);
2744 }
2745 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002746 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002747 }
Winson Chung64359a52013-07-08 17:17:08 -07002748 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002749 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002750 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2751 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002752
2753 // Post callback on main thread
2754 mHandler.post(new Runnable() {
2755 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002756
Winson Chung64359a52013-07-08 17:17:08 -07002757 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002758 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002759 if (callbacks != null) {
2760 callbacks.bindAllApplications(added);
2761 if (DEBUG_LOADERS) {
2762 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002763 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002764 }
2765 } else {
2766 Log.i(TAG, "not binding apps: no Launcher activity");
2767 }
2768 }
2769 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002770 // Cleanup any data stored for a deleted user.
2771 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002772 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002773 Log.d(TAG, "Icons processed in "
2774 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002775 }
2776 }
2777
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002778 private void loadAndBindDeepShortcuts() {
2779 if (DEBUG_LOADERS) {
2780 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2781 }
2782 if (!mDeepShortcutsLoaded) {
2783 mBgDeepShortcutMap.clear();
2784 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002785 if (mUserManager.isUserUnlocked(user)) {
2786 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2787 .queryForAllShortcuts(user);
2788 updateDeepShortcutMap(null, user, shortcuts);
2789 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002790 }
2791 synchronized (LoaderTask.this) {
2792 if (mStopped) {
2793 return;
2794 }
2795 mDeepShortcutsLoaded = true;
2796 }
2797 }
Tony Wickham80f57872016-06-29 18:12:15 -07002798 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002799 }
2800
Joe Onoratobe386092009-11-17 17:32:16 -08002801 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002802 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002803 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002804 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2805 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2806 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2807 }
Joe Onorato36115782010-06-17 13:28:48 -04002808 }
2809 }
2810
Sunny Goyal860538d2016-07-20 12:35:16 -07002811 /**
2812 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2813 */
2814 private void updateDeepShortcutMap(
2815 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002816 if (packageName != null) {
2817 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2818 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002819 ComponentKey next = keysIter.next();
2820 if (next.componentName.getPackageName().equals(packageName)
2821 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002822 keysIter.remove();
2823 }
2824 }
2825 }
2826
2827 // Now add the new shortcuts to the map.
2828 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002829 boolean shouldShowInContainer = shortcut.isEnabled()
2830 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2831 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002832 ComponentKey targetComponent
2833 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2834 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2835 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002836 }
2837 }
2838
Tony Wickham80f57872016-06-29 18:12:15 -07002839 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002840 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2841 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002842 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002843 @Override
2844 public void run() {
2845 Callbacks callbacks = getCallback();
2846 if (callbacks != null) {
2847 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2848 }
2849 }
Tony Wickham80f57872016-06-29 18:12:15 -07002850 };
2851 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002852 }
2853
Sunny Goyal75b0f552015-05-20 21:57:06 -07002854 /**
2855 * Called when the icons for packages have been updated in the icon cache.
2856 */
2857 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2858 final Callbacks callbacks = getCallback();
2859 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2860 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2861
2862 // If any package icon has changed (app was updated while launcher was dead),
2863 // update the corresponding shortcuts.
2864 synchronized (sBgLock) {
2865 for (ItemInfo info : sBgItemsIdMap) {
2866 if (info instanceof ShortcutInfo && user.equals(info.user)
2867 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2868 ShortcutInfo si = (ShortcutInfo) info;
2869 ComponentName cn = si.getTargetComponent();
2870 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2871 si.updateIcon(mIconCache);
2872 updatedShortcuts.add(si);
2873 }
2874 }
2875 }
2876 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2877 }
2878
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002879 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002880
2881 if (!updatedApps.isEmpty()) {
2882 mHandler.post(new Runnable() {
2883
2884 public void run() {
2885 Callbacks cb = getCallback();
2886 if (cb != null && callbacks == cb) {
2887 cb.bindAppsUpdated(updatedApps);
2888 }
2889 }
2890 });
2891 }
2892 }
2893
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002894 private void bindUpdatedShortcuts(
2895 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2896 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2897 }
2898
2899 private void bindUpdatedShortcuts(
2900 final ArrayList<ShortcutInfo> updatedShortcuts,
2901 final ArrayList<ShortcutInfo> removedShortcuts,
2902 final UserHandleCompat user) {
2903 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002904 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002905 mHandler.post(new Runnable() {
2906
2907 public void run() {
2908 Callbacks cb = getCallback();
2909 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002910 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002911 }
2912 }
2913 });
2914 }
2915 }
2916
2917 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002918 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002919 }
2920
Adam Cohen091440a2015-03-18 14:16:05 -07002921 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002922
2923 @Override
2924 public void onReceive(Context context, Intent intent) {
2925 synchronized (sBgLock) {
2926 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2927 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002928 final PackageManager manager = context.getPackageManager();
2929 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2930 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002931 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2932 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002933 packagesRemoved.clear();
2934 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002935 for (String pkg : entry.getValue()) {
2936 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002937 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002938 packagesUnavailable.add(pkg);
2939 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 packagesRemoved.add(pkg);
2941 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002942 }
2943 }
2944 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002945 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002946 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2947 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002948 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002949 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002950 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2951 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002952 }
Sunny Goyal34942622014-08-29 17:20:55 -07002953 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002954 }
2955 }
2956 }
2957
Joe Onorato36115782010-06-17 13:28:48 -04002958 private class PackageUpdatedTask implements Runnable {
2959 int mOp;
2960 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002961 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002962
2963 public static final int OP_NONE = 0;
2964 public static final int OP_ADD = 1;
2965 public static final int OP_UPDATE = 2;
2966 public static final int OP_REMOVE = 3; // uninstlled
2967 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002968 public static final int OP_SUSPEND = 5; // package suspended
2969 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002970 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002971
Kenny Guyed131872014-04-30 03:02:21 +01002972 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002973 mOp = op;
2974 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002975 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002976 }
2977
2978 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002979 if (!mHasLoaderCompletedOnce) {
2980 // Loader has not yet run.
2981 return;
2982 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002983 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002984
2985 final String[] packages = mPackages;
2986 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002987 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002988 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002989 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002990 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002991 for (int i=0; i<N; i++) {
2992 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002993 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002994 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002996
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2998 if (heuristic != null) {
2999 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003000 }
Joe Onorato36115782010-06-17 13:28:48 -04003001 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003002 }
Joe Onorato36115782010-06-17 13:28:48 -04003003 case OP_UPDATE:
3004 for (int i=0; i<N; i++) {
3005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003006 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003007 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003008 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003009 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003010 // Since package was just updated, the target must be available now.
3011 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003012 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003013 case OP_REMOVE: {
3014 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3015 if (heuristic != null) {
3016 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003017 }
Joe Onorato36115782010-06-17 13:28:48 -04003018 for (int i=0; i<N; i++) {
3019 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003020 mIconCache.removeIconsForPkg(packages[i], mUser);
3021 }
3022 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003023 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003024 case OP_UNAVAILABLE:
3025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3027 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003028 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003029 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003031 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003032 case OP_SUSPEND:
3033 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003034 flagOp = mOp == OP_SUSPEND ?
3035 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3036 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003037 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003038 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3039 break;
3040 case OP_USER_AVAILABILITY_CHANGE:
3041 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3042 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3043 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3044 // We want to update all packages for this user.
3045 pkgFilter = StringFilter.matchesAll();
3046 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003047 break;
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
3049
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003050 ArrayList<AppInfo> added = null;
3051 ArrayList<AppInfo> modified = null;
3052 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003053
Adam Cohen487f7dd2012-06-28 18:12:10 -07003054 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003055 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003056 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003057 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003058 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003059 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003060 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Winson Chung5d55f332012-07-16 20:45:03 -07003062 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003063 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003064 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003065 }
3066
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003067 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068
Joe Onorato36115782010-06-17 13:28:48 -04003069 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003070 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003071 for (AppInfo ai : added) {
3072 addedOrUpdatedApps.put(ai.componentName, ai);
3073 }
Joe Onorato36115782010-06-17 13:28:48 -04003074 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003075
Joe Onorato36115782010-06-17 13:28:48 -04003076 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003077 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003078 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 for (AppInfo ai : modified) {
3080 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003081 }
3082
Joe Onorato36115782010-06-17 13:28:48 -04003083 mHandler.post(new Runnable() {
3084 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003085 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003086 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003087 callbacks.bindAppsUpdated(modifiedFinal);
3088 }
3089 }
3090 });
3091 }
Winson Chung83892cc2013-05-01 16:53:33 -07003092
Sunny Goyal4390ace2014-10-13 11:33:11 -07003093 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003094 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3096 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3097 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3098
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003100 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003101 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3102 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003103 boolean infoUpdated = false;
3104 boolean shortcutUpdated = false;
3105
3106 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003107 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003108 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003109 Bitmap icon = Utilities.createIconBitmap(
3110 si.iconResource.packageName,
3111 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 if (icon != null) {
3113 si.setIcon(icon);
3114 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003116 }
3117 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118
3119 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003120 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3122
3123 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3125 // Auto install icon
3126 PackageManager pm = context.getPackageManager();
3127 ResolveInfo matched = pm.resolveActivity(
3128 new Intent(Intent.ACTION_MAIN)
3129 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3130 PackageManager.MATCH_DEFAULT_ONLY);
3131 if (matched == null) {
3132 // Try to find the best match activity.
3133 Intent intent = pm.getLaunchIntentForPackage(
3134 cn.getPackageName());
3135 if (intent != null) {
3136 cn = intent.getComponent();
3137 appInfo = addedOrUpdatedApps.get(cn);
3138 }
3139
3140 if ((intent == null) || (appInfo == null)) {
3141 removedShortcuts.add(si);
3142 continue;
3143 }
3144 si.promisedIntent = intent;
3145 }
3146 }
3147
3148 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003149 if (appInfo != null) {
3150 si.flags = appInfo.flags;
3151 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003152
Sunny Goyal756adbc2015-04-16 15:20:51 -07003153 si.intent = si.promisedIntent;
3154 si.promisedIntent = null;
3155 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003156 infoUpdated = true;
3157 si.updateIcon(mIconCache);
3158 }
3159
3160 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3161 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3162 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003163 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164 si.contentDescription = appInfo.contentDescription;
3165 infoUpdated = true;
3166 }
3167
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003168 int oldDisabledFlags = si.isDisabled;
3169 si.isDisabled = flagOp.apply(si.isDisabled);
3170 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 shortcutUpdated = true;
3172 }
3173 }
3174
3175 if (infoUpdated || shortcutUpdated) {
3176 updatedShortcuts.add(si);
3177 }
3178 if (infoUpdated) {
3179 updateItemInDatabase(context, si);
3180 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003181 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3183 if (mUser.equals(widgetInfo.user)
3184 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003185 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003186 widgetInfo.restoreStatus &=
3187 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3188 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003189
3190 // adding this flag ensures that launcher shows 'click to setup'
3191 // if the widget has a config activity. In case there is no config
3192 // activity, it will be marked as 'restored' during bind.
3193 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3194
Sunny Goyal4390ace2014-10-13 11:33:11 -07003195 widgets.add(widgetInfo);
3196 updateItemInDatabase(context, widgetInfo);
3197 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003198 }
3199 }
3200 }
3201
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003202 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3203 if (!removedShortcuts.isEmpty()) {
3204 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003206
Sunny Goyal4390ace2014-10-13 11:33:11 -07003207 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003208 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 });
3217 }
3218 }
3219
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003220 final HashSet<String> removedPackages = new HashSet<>();
3221 final HashSet<ComponentName> removedComponents = new HashSet<>();
3222 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003223 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003224 Collections.addAll(removedPackages, packages);
3225
3226 // No need to update the removedComponents as
3227 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003228 } else if (mOp == OP_UPDATE) {
3229 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003230 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003231 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003232 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003233 }
3234 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003235
3236 // Update removedComponents as some components can get removed during package update
3237 for (AppInfo info : removedApps) {
3238 removedComponents.add(info.componentName);
3239 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003240 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003241
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003242 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3243 for (String pn : removedPackages) {
3244 deletePackageFromDatabase(context, pn, mUser);
3245 }
3246 for (ComponentName cn : removedComponents) {
3247 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003248 }
3249
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003251 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3252
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003254 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003255 mHandler.post(new Runnable() {
3256 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003257 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003258 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003259 callbacks.bindWorkspaceComponentsRemoved(
3260 removedPackages, removedComponents, mUser);
3261 }
3262 }
3263 });
3264 }
3265
3266 if (!removedApps.isEmpty()) {
3267 // Remove corresponding apps from All-Apps
3268 final Callbacks callbacks = getCallback();
3269 mHandler.post(new Runnable() {
3270 public void run() {
3271 Callbacks cb = getCallback();
3272 if (callbacks == cb && cb != null) {
3273 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003274 }
3275 }
3276 });
Joe Onoratobe386092009-11-17 17:32:16 -08003277 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003278
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003279 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3280 // get widget update signals.
3281 if (!Utilities.ATLEAST_MARSHMALLOW &&
3282 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003283 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003284 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003285 public void run() {
3286 Callbacks cb = getCallback();
3287 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003288 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003289 }
3290 }
3291 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003292 }
3293 }
3294 }
3295
Sunny Goyal10923b32016-07-20 15:42:44 -07003296 /**
3297 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3298 */
3299 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3300 enqueueItemUpdatedTask(new Runnable() {
3301 @Override
3302 public void run() {
3303 info.updateFromDeepShortcutInfo(
3304 fullDetail, LauncherAppState.getInstance().getContext());
3305 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3306 update.add(info);
3307 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3308 }
3309 });
3310 }
3311
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003312 private class ShortcutsChangedTask implements Runnable {
3313 private String mPackageName;
3314 private List<ShortcutInfoCompat> mShortcuts;
3315 private UserHandleCompat mUser;
3316
3317 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3318 UserHandleCompat user) {
3319 mPackageName = packageName;
3320 mShortcuts = shortcuts;
3321 mUser = user;
3322 }
3323
3324 @Override
3325 public void run() {
3326 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3327
3328 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3329 for (ShortcutInfoCompat shortcut : mShortcuts) {
3330 idsToShortcuts.put(shortcut.getId(), shortcut);
3331 }
3332
3333 // Find ShortcutInfo's that have changed on the workspace.
3334 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3335 for (ItemInfo itemInfo : sBgItemsIdMap) {
3336 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3337 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003338 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3339 String shortcutId = si.getDeepShortcutId();
3340 if (idsToShortcuts.containsKey(shortcutId)) {
3341 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3342 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003343 }
3344 }
3345 }
3346
3347 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3348 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3349 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3350 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3351 Context context = LauncherAppState.getInstance().getContext();
3352 for (ShortcutInfoCompat fullDetails : shortcuts) {
3353 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3354 .get(fullDetails.getId());
3355 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003356 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003357 updatedShortcutInfos.add(shortcutInfo);
3358 }
3359 }
3360 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3361
3362 // Update the deep shortcut map, in case the list of ids has changed for an activity.
Sunny Goyal860538d2016-07-20 12:35:16 -07003363 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003364 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003365 }
3366 }
3367
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003368 /**
3369 * Task to handle changing of lock state of the user
3370 */
3371 private class UserLockStateChangedTask implements Runnable {
3372
3373 private final UserHandleCompat mUser;
3374
3375 public UserLockStateChangedTask(UserHandleCompat user) {
3376 mUser = user;
3377 }
3378
3379 @Override
3380 public void run() {
3381 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3382 Context context = mApp.getContext();
3383
3384 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3385 if (isUserUnlocked) {
3386 for (ShortcutInfoCompat shortcut :
3387 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser)) {
3388 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3389 }
3390 }
3391
3392 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3393 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3394 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3395 for (ItemInfo itemInfo : sBgItemsIdMap) {
3396 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3397 && mUser.equals(itemInfo.user)) {
3398 ShortcutInfo si = (ShortcutInfo) itemInfo;
3399 if (isUserUnlocked) {
3400 ShortcutInfoCompat shortcut =
3401 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3402 // We couldn't verify the shortcut during loader. If its no longer available
3403 // (probably due to clear data), delete the workspace item as well
3404 if (shortcut == null) {
3405 deletedShortcutInfos.add(si);
3406 continue;
3407 }
3408 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3409 si.updateFromDeepShortcutInfo(shortcut, context);
3410 } else {
3411 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3412 }
3413 updatedShortcutInfos.add(si);
3414 }
3415 }
3416 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3417 if (!deletedShortcutInfos.isEmpty()) {
3418 deleteItemsFromDatabase(context, deletedShortcutInfos);
3419 }
3420
3421 // Remove shortcut id map for that user
3422 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3423 while (keysIter.hasNext()) {
3424 if (keysIter.next().user.equals(mUser)) {
3425 keysIter.remove();
3426 }
3427 }
3428
3429 if (isUserUnlocked) {
3430 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3431 }
3432 bindDeepShortcuts();
3433 }
3434 }
3435
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003436 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3437 mHandler.post(new Runnable() {
3438 @Override
3439 public void run() {
3440 Callbacks cb = getCallback();
3441 if (callbacks == cb && cb != null) {
3442 callbacks.bindWidgetsModel(model);
3443 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003444 }
3445 });
3446 }
3447
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003448 public void refreshAndBindWidgetsAndShortcuts(
3449 final Callbacks callbacks, final boolean bindFirst) {
3450 runOnWorkerThread(new Runnable() {
3451 @Override
3452 public void run() {
3453 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3454 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003455 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003456 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3457 bindWidgetsModel(callbacks, model);
3458 // update the Widget entries inside DB on the worker thread.
3459 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3460 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003461 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003462 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003463 }
3464
Adam Cohen091440a2015-03-18 14:16:05 -07003465 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003466 UserHandleCompat user) {
3467 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3468 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003469 }
Adam Cohen556f6132014-01-15 15:18:08 -08003470
Kenny Guyed131872014-04-30 03:02:21 +01003471 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3472 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003473 if (cn == null) {
3474 return false;
3475 }
Kenny Guyed131872014-04-30 03:02:21 +01003476 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3477 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003478 return false;
3479 }
Kenny Guyed131872014-04-30 03:02:21 +01003480 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003481 }
3482
Adam Cohena28b78e2014-05-20 17:03:04 -07003483 public static boolean isValidPackage(Context context, String packageName,
3484 UserHandleCompat user) {
3485 if (packageName == null) {
3486 return false;
3487 }
3488 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3489 return launcherApps.isPackageEnabledForProfile(packageName, user);
3490 }
3491
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003493 * Make an ShortcutInfo object for a restored application or shortcut item that points
3494 * to a package that is not yet installed on the system.
3495 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003496 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3497 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003498 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003499 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003500
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003501 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003502 // the fallback icon
3503 if (icon == null) {
3504 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3505 } else {
3506 info.setIcon(icon);
3507 }
Sunny Goyal34942622014-08-29 17:20:55 -07003508
3509 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003510 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003511 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003512 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003513 }
Sunny Goyal34942622014-08-29 17:20:55 -07003514 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3515 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003516 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003517 }
Sunny Goyal34942622014-08-29 17:20:55 -07003518 } else {
3519 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3520 }
3521
Winson Chung82b016c2015-05-08 17:00:10 -07003522 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003523 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003524 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003525 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003526 return info;
3527 }
3528
3529 /**
3530 * Make an Intent object for a restored application or shortcut item that points
3531 * to the market page for the item.
3532 */
Adam Cohen091440a2015-03-18 14:16:05 -07003533 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003534 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003535 return getMarketIntent(componentName.getPackageName());
3536 }
3537
3538 static Intent getMarketIntent(String packageName) {
3539 return new Intent(Intent.ACTION_VIEW)
3540 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003541 .scheme("market")
3542 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003543 .appendQueryParameter("id", packageName)
3544 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003545 }
3546
3547 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003548 * Make an ShortcutInfo object for a shortcut that is an application.
3549 *
3550 * If c is not null, then it will be used to fill in missing data like the title and icon.
3551 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003552 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003553 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003554 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003555 if (user == null) {
3556 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003557 return null;
3558 }
3559
Kenny Guyed131872014-04-30 03:02:21 +01003560 ComponentName componentName = intent.getComponent();
3561 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003562 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003563 return null;
3564 }
3565
3566 Intent newIntent = new Intent(intent.getAction(), null);
3567 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3568 newIntent.setComponent(componentName);
3569 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003570 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003571 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3572 return null;
3573 }
3574
3575 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003576 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003577 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003578 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003579 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003580 }
3581
Sunny Goyald09c3702016-04-06 16:18:20 -07003582 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3583 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3584 }
3585
Joe Onorato56d82912010-03-07 14:32:10 -05003586 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003587 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003588 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003589 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003590
Joe Onorato56d82912010-03-07 14:32:10 -05003591 // fall back to the class name of the activity
3592 if (info.title == null) {
3593 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003594 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003595
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003597 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003598 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003599 if (lai != null) {
3600 info.flags = AppInfo.initFlags(lai);
3601 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003603 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003604
Sunny Goyale2df0622015-04-24 11:27:00 -07003605 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003606 ItemInfoFilter f) {
3607 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3608 for (ItemInfo i : infos) {
3609 if (i instanceof ShortcutInfo) {
3610 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003611 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003612 if (cn != null && f.filterItem(null, info, cn)) {
3613 filtered.add(info);
3614 }
3615 } else if (i instanceof FolderInfo) {
3616 FolderInfo info = (FolderInfo) i;
3617 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003618 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003619 if (cn != null && f.filterItem(info, s, cn)) {
3620 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003621 }
3622 }
Winson Chung64359a52013-07-08 17:17:08 -07003623 } else if (i instanceof LauncherAppWidgetInfo) {
3624 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3625 ComponentName cn = info.providerName;
3626 if (cn != null && f.filterItem(null, info, cn)) {
3627 filtered.add(info);
3628 }
Winson Chung8a435102012-08-30 17:16:53 -07003629 }
3630 }
Winson Chung64359a52013-07-08 17:17:08 -07003631 return new ArrayList<ItemInfo>(filtered);
3632 }
3633
Adam Cohen091440a2015-03-18 14:16:05 -07003634 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003635 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003636 ItemInfoFilter filter = new ItemInfoFilter() {
3637 @Override
3638 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003639 if (info.user == null) {
3640 return cn.equals(cname);
3641 } else {
3642 return cn.equals(cname) && info.user.equals(user);
3643 }
Winson Chung64359a52013-07-08 17:17:08 -07003644 }
3645 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003646 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003647 }
3648
Sunny Goyal1a745e82014-10-02 15:58:31 -07003649 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003650 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003651 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003652 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003653 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003654 // Non-app shortcuts are only supported for current user.
3655 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003657
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003658 // TODO: If there's an explicit component and we can't install that, delete it.
3659
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003660 loadInfoFromCursor(info, c, iconInfo);
3661 return info;
3662 }
Joe Onorato56d82912010-03-07 14:32:10 -05003663
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003664 /**
3665 * Make an ShortcutInfo object for a shortcut that isn't an application.
3666 */
3667 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3668 info.title = iconInfo.getTitle(c);
3669 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003670 // the fallback icon
3671 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003672 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003673 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003675 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003677
Sunny Goyal2350bc92014-10-14 16:42:54 -07003678 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003679 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3680 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3681 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3682
Adam Cohend9198822011-11-22 16:42:47 -08003683 if (intent == null) {
3684 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3685 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3686 return null;
3687 }
3688
Joe Onorato0589f0f2010-02-08 13:44:00 -08003689 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003690 boolean customIcon = false;
3691 ShortcutIconResource iconResource = null;
3692
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003693 if (bitmap instanceof Bitmap) {
3694 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003695 customIcon = true;
3696 } else {
3697 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003698 if (extra instanceof ShortcutIconResource) {
3699 iconResource = (ShortcutIconResource) extra;
3700 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003701 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003702 }
3703 }
3704
Michael Jurkac9d95c52011-08-29 14:03:34 -07003705 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003706
Kenny Guyed131872014-04-30 03:02:21 +01003707 // Only support intents for current user for now. Intents sent from other
3708 // users wouldn't get here without intent forwarding anyway.
3709 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003710 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003711 icon = mIconCache.getDefaultIcon(info.user);
3712 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003713 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003714 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003715
Winson Chung82b016c2015-05-08 17:00:10 -07003716 info.title = Utilities.trim(name);
3717 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003718 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003719 info.iconResource = iconResource;
3720
3721 return info;
3722 }
3723
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003725 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 * or make a new one.
3727 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003728 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 // See if a placeholder was created for us already
3730 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003731 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003733 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 folders.put(id, folderInfo);
3735 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003736 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 }
3738
Joe Onoratobe386092009-11-17 17:32:16 -08003739
Sunny Goyal651077b2014-06-30 14:15:31 -07003740 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3741 return (provider != null) && (provider.provider != null)
3742 && (provider.provider.getPackageName() != null);
3743 }
3744
Joe Onoratobe386092009-11-17 17:32:16 -08003745 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003746 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003747 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3748 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3749 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3750 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003751 if (mLoaderTask != null) {
3752 mLoaderTask.dumpState();
3753 } else {
3754 Log.d(TAG, "mLoaderTask=null");
3755 }
Joe Onoratobe386092009-11-17 17:32:16 -08003756 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003757
3758 public Callbacks getCallback() {
3759 return mCallbacks != null ? mCallbacks.get() : null;
3760 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003761
3762 /**
3763 * @return {@link FolderInfo} if its already loaded.
3764 */
3765 public FolderInfo findFolderById(Long folderId) {
3766 synchronized (sBgLock) {
3767 return sBgFolders.get(folderId);
3768 }
3769 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003770
Sunny Goyal527c7d32015-08-28 15:19:36 -07003771 @Thunk class DeferredMainThreadExecutor implements Executor {
3772
3773 @Override
3774 public void execute(Runnable command) {
3775 runOnMainThread(command);
3776 }
3777 }
3778
Sunny Goyal756adbc2015-04-16 15:20:51 -07003779 /**
3780 * @return the looper for the worker thread which can be used to start background tasks.
3781 */
3782 public static Looper getWorkerLooper() {
3783 return sWorkerThread.getLooper();
3784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003785}