blob: 649f42dd17f39519210ead14c2584ee6cd8d1f7b [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
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.database.Cursor;
33import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040035import android.os.Handler;
36import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070037import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import android.os.SystemClock;
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;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070050import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070051import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070054import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070055import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.Folder;
57import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070058import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080059import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070063import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070064import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070065import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070066import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070067import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070068import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070069import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070070import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070071import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080072
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.lang.ref.WeakReference;
74import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070075import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070077import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.Collections;
79import java.util.Comparator;
80import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070082import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070084import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070085import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070086import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088/**
89 * Maintains in-memory state of the Launcher. It is expected that there should be only one
90 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070091 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 */
Kenny Guyed131872014-04-30 03:02:21 +010093public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010094 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080095 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040096 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070097 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040098
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700100
Joe Onorato36115782010-06-17 13:28:48 -0400101 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500102 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final LauncherAppState mApp;
105 @Thunk final Object mLock = new Object();
106 @Thunk DeferredHandler mHandler = new DeferredHandler();
107 @Thunk LoaderTask mLoaderTask;
108 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700109 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700112 static {
113 sWorkerThread.start();
114 }
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700116
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 // We start off with everything not loaded. After that, we assume that
118 // our monitoring of the package manager provides all updates and we never
119 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk boolean mWorkspaceLoaded;
121 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700122
Sunny Goyal756a28a2015-04-23 17:07:55 -0700123 /**
124 * Set of runnables to be called on the background thread after the workspace binding
125 * is complete.
126 */
127 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
128
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800132 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700133 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800134 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800135
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700136 // The lock that must be acquired before referencing any static bg data structures. Unlike
137 // other locks, this one can generally be held long-term because we never expect any of these
138 // static data structures to be referenced outside of the worker thread except on the first
139 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700140 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700141
Adam Cohen487f7dd2012-06-28 18:12:10 -0700142 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700144 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
147 // created by LauncherModel that are directly on the home screen (however, no widgets or
148 // shortcuts within folders).
149 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
152 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 new ArrayList<LauncherAppWidgetInfo>();
154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700156 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700157
Adam Cohendcd297f2013-06-18 13:13:40 -0700158 // sBgWorkspaceScreens is the ordered set of workspace screens.
159 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
160
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700161 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700162 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
163 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700164
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700165 // </ only access in worker thread >
166
Adam Cohen091440a2015-03-18 14:16:05 -0700167 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Adam Cohen091440a2015-03-18 14:16:05 -0700169 @Thunk final LauncherAppsCompat mLauncherApps;
170 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100171
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700173 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700175 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700177 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
178 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700179 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700180 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800181 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200183 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700184 public void bindAppsAdded(ArrayList<Long> newScreens,
185 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700186 ArrayList<ItemInfo> addAnimated,
187 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700189 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
190 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
191 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700192 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700193 public void bindWorkspaceComponentsRemoved(
194 HashSet<String> packageNames, HashSet<ComponentName> components,
195 UserHandleCompat user);
196 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800197 public void notifyWidgetProvidersChanged();
198 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700199 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700200 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700201 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700202 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Winson Chung64359a52013-07-08 17:17:08 -0700205 public interface ItemInfoFilter {
206 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
207 }
208
Bjorn Bringert1307f632013-10-03 22:31:03 +0100209 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800210 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400211 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100212 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700213 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800214 mIconCache = iconCache;
215
Kenny Guyed131872014-04-30 03:02:21 +0100216 mLauncherApps = LauncherAppsCompat.getInstance(context);
217 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 }
219
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700220 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
221 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700223 if (sWorkerThread.getThreadId() == Process.myTid()) {
224 // If we are on the worker thread, post onto the main handler
225 mHandler.post(r);
226 } else {
227 r.run();
228 }
229 }
230
231 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
232 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700233 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700234 if (sWorkerThread.getThreadId() == Process.myTid()) {
235 r.run();
236 } else {
237 // If we are not on the worker thread, then post to the worker handler
238 sWorker.post(r);
239 }
240 }
241
Sunny Goyal756adbc2015-04-16 15:20:51 -0700242 public void setPackageState(final PackageInstallInfo installInfo) {
243 Runnable updateRunnable = new Runnable() {
244
245 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500246 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700247 synchronized (sBgLock) {
248 final HashSet<ItemInfo> updates = new HashSet<>();
249
250 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
251 // Ignore install success events as they are handled by Package add events.
252 return;
253 }
254
Sunny Goyale2df0622015-04-24 11:27:00 -0700255 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700256 if (info instanceof ShortcutInfo) {
257 ShortcutInfo si = (ShortcutInfo) info;
258 ComponentName cn = si.getTargetComponent();
259 if (si.isPromise() && (cn != null)
260 && installInfo.packageName.equals(cn.getPackageName())) {
261 si.setInstallProgress(installInfo.progress);
262
263 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
264 // Mark this info as broken.
265 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
266 }
267 updates.add(si);
268 }
269 }
270 }
271
272 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
273 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
274 widget.installProgress = installInfo.progress;
275 updates.add(widget);
276 }
277 }
278
279 if (!updates.isEmpty()) {
280 // Push changes to the callback.
281 Runnable r = new Runnable() {
282 public void run() {
283 Callbacks callbacks = getCallback();
284 if (callbacks != null) {
285 callbacks.bindRestoreItemsChange(updates);
286 }
287 }
288 };
289 mHandler.post(r);
290 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500291 }
292 }
293 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700294 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500295 }
296
Sunny Goyal756adbc2015-04-16 15:20:51 -0700297 /**
298 * Updates the icons and label of all pending icons for the provided package name.
299 */
300 public void updateSessionDisplayInfo(final String packageName) {
301 Runnable updateRunnable = new Runnable() {
302
303 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700304 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700305 synchronized (sBgLock) {
306 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
307 final UserHandleCompat user = UserHandleCompat.myUserHandle();
308
Sunny Goyale2df0622015-04-24 11:27:00 -0700309 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700310 if (info instanceof ShortcutInfo) {
311 ShortcutInfo si = (ShortcutInfo) info;
312 ComponentName cn = si.getTargetComponent();
313 if (si.isPromise() && (cn != null)
314 && packageName.equals(cn.getPackageName())) {
315 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
316 // For auto install apps update the icon as well as label.
317 mIconCache.getTitleAndIcon(si,
318 si.promisedIntent, user,
319 si.shouldUseLowResIcon());
320 } else {
321 // Only update the icon for restored apps.
322 si.updateIcon(mIconCache);
323 }
324 updates.add(si);
325 }
326 }
327 }
328
329 if (!updates.isEmpty()) {
330 // Push changes to the callback.
331 Runnable r = new Runnable() {
332 public void run() {
333 Callbacks callbacks = getCallback();
334 if (callbacks != null) {
335 callbacks.bindShortcutsChanged(updates,
336 new ArrayList<ShortcutInfo>(), user);
337 }
338 }
339 };
340 mHandler.post(r);
341 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700342 }
343 }
344 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700346 }
347
Adam Cohen76a47a12014-02-05 11:47:43 -0800348 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800349 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800350
351 if (allAppsApps == null) {
352 throw new RuntimeException("allAppsApps must not be null");
353 }
354 if (allAppsApps.isEmpty()) {
355 return;
356 }
357
358 // Process the newly added applications and add them to the database first
359 Runnable r = new Runnable() {
360 public void run() {
361 runOnMainThread(new Runnable() {
362 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800363 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500365 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800366 }
367 }
368 });
369 }
370 };
371 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700372 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800373
Sunny Goyala9116722015-04-29 13:55:58 -0700374 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800375 int[] xy, int spanX, int spanY) {
376 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700377 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700378
379 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800380 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700381 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700382 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800383 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800384 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700385 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800386 }
387
388 /**
389 * Find a position on the screen for the given size or adds a new screen.
390 * @return screenId and the coordinates for the item.
391 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700392 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700393 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800394 ArrayList<Long> workspaceScreens,
395 ArrayList<Long> addedWorkspaceScreensFinal,
396 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700397 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800398
Sunny Goyala9116722015-04-29 13:55:58 -0700399 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700400 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700401 synchronized (sBgLock) {
402 for (ItemInfo info : sBgItemsIdMap) {
403 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
404 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
405 if (items == null) {
406 items = new ArrayList<>();
407 screenItems.put(info.screenId, items);
408 }
409 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 }
413
414 // Find appropriate space for the item.
415 long screenId = 0;
416 int[] cordinates = new int[2];
417 boolean found = false;
418
419 int screenCount = workspaceScreens.size();
420 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700421 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 if (preferredScreenIndex < screenCount) {
423 screenId = workspaceScreens.get(preferredScreenIndex);
424 found = findNextAvailableIconSpaceInScreen(
425 screenItems.get(screenId), cordinates, spanX, spanY);
426 }
427
428 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700429 // Search on any of the screens starting from the first screen.
430 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 screenId = workspaceScreens.get(screen);
432 if (findNextAvailableIconSpaceInScreen(
433 screenItems.get(screenId), cordinates, spanX, spanY)) {
434 // We found a space for it
435 found = true;
436 break;
437 }
438 }
439 }
440
441 if (!found) {
442 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700443 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
444 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
445 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446
447 // Save the screen id for binding in the workspace
448 workspaceScreens.add(screenId);
449 addedWorkspaceScreensFinal.add(screenId);
450
451 // If we still can't find an empty space, then God help us all!!!
452 if (!findNextAvailableIconSpaceInScreen(
453 screenItems.get(screenId), cordinates, spanX, spanY)) {
454 throw new RuntimeException("Can't find space to add the item");
455 }
456 }
457 return Pair.create(screenId, cordinates);
458 }
459
460 /**
461 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700463 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700464 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800466 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700467 return;
Winson Chung997a9232013-07-24 15:33:46 -0700468 }
Winson Chung64359a52013-07-08 17:17:08 -0700469 // Process the newly added applications and add them to the database first
470 Runnable r = new Runnable() {
471 public void run() {
472 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
473 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
474
Winson Chung76828c82013-08-19 15:43:29 -0700475 // Get the list of workspace screens. We need to append to this list and
476 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
477 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800478 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700479 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700481 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700483 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700485 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 }
Winson Chung76828c82013-08-19 15:43:29 -0700487
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800488 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700489 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700490 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800491 long screenId = coords.first;
492 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700493
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700494 ItemInfo itemInfo;
495 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
496 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800497 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700498 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700499 } else {
500 throw new RuntimeException("Unexpected info type");
501 }
Winson Chung94d67682013-09-25 16:29:40 -0700502
Winson Chung64359a52013-07-08 17:17:08 -0700503 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700505 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700506 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700507 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700508 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700509 }
510 }
511
Winson Chung76828c82013-08-19 15:43:29 -0700512 // Update the workspace screens
513 updateWorkspaceScreenOrder(context, workspaceScreens);
514
Adam Cohen76a47a12014-02-05 11:47:43 -0800515 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700516 runOnMainThread(new Runnable() {
517 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800518 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700519 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700520 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
521 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700522 if (!addedShortcutsFinal.isEmpty()) {
523 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
524 long lastScreenId = info.screenId;
525 for (ItemInfo i : addedShortcutsFinal) {
526 if (i.screenId == lastScreenId) {
527 addAnimated.add(i);
528 } else {
529 addNotAnimated.add(i);
530 }
Winson Chung997a9232013-07-24 15:33:46 -0700531 }
532 }
Winson Chungd64d1762013-08-20 14:37:16 -0700533 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800534 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700535 }
Winson Chung64359a52013-07-08 17:17:08 -0700536 }
Winson Chung997a9232013-07-24 15:33:46 -0700537 });
538 }
Winson Chung64359a52013-07-08 17:17:08 -0700539 }
540 };
541 runOnWorkerThread(r);
542 }
543
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 /**
545 * Adds an item to the DB if it was not created previously, or move it to a new
546 * <container, screen, cellX, cellY>
547 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800548 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700549 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 if (item.container == ItemInfo.NO_ID) {
551 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700552 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 } else {
554 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700555 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
557 }
558
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700559 static void checkItemInfoLocked(
560 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
561 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
562 if (modelItem != null && item != modelItem) {
563 // check all the data is consistent
564 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
565 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
566 ShortcutInfo shortcut = (ShortcutInfo) item;
567 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
568 modelShortcut.intent.filterEquals(shortcut.intent) &&
569 modelShortcut.id == shortcut.id &&
570 modelShortcut.itemType == shortcut.itemType &&
571 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700572 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700573 modelShortcut.cellX == shortcut.cellX &&
574 modelShortcut.cellY == shortcut.cellY &&
575 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700576 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700577 // For all intents and purposes, this is the same object
578 return;
579 }
580 }
581
582 // the modelItem needs to match up perfectly with item if our model is
583 // to be consistent with the database-- for now, just require
584 // modelItem == item or the equality check above
585 String msg = "item: " + ((item != null) ? item.toString() : "null") +
586 "modelItem: " +
587 ((modelItem != null) ? modelItem.toString() : "null") +
588 "Error: ItemInfo passed to checkItemInfo doesn't match original";
589 RuntimeException e = new RuntimeException(msg);
590 if (stackTrace != null) {
591 e.setStackTrace(stackTrace);
592 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800593 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 }
595 }
596
Michael Jurka816474f2012-06-25 14:49:02 -0700597 static void checkItemInfo(final ItemInfo item) {
598 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
599 final long itemId = item.id;
600 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700601 public void run() {
602 synchronized (sBgLock) {
603 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700604 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700605 }
606 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700608 }
609
Michael Jurkac9d95c52011-08-29 14:03:34 -0700610 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
611 final ItemInfo item, final String callingFunction) {
612 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700613 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700614 final ContentResolver cr = context.getContentResolver();
615
Adam Cohen487f7dd2012-06-28 18:12:10 -0700616 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700617 Runnable r = new Runnable() {
618 public void run() {
619 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700620 updateItemArrays(item, itemId, stackTrace);
621 }
622 };
623 runOnWorkerThread(r);
624 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700625
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700626 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
627 final ArrayList<ItemInfo> items, final String callingFunction) {
628 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700629
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700630 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
631 Runnable r = new Runnable() {
632 public void run() {
633 ArrayList<ContentProviderOperation> ops =
634 new ArrayList<ContentProviderOperation>();
635 int count = items.size();
636 for (int i = 0; i < count; i++) {
637 ItemInfo item = items.get(i);
638 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700639 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700641
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700642 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
643 updateItemArrays(item, itemId, stackTrace);
644
645 }
646 try {
647 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
648 } catch (Exception e) {
649 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700650 }
651 }
652 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700653 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700654 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700655
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700656 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
657 // Lock on mBgLock *after* the db operation
658 synchronized (sBgLock) {
659 checkItemInfoLocked(itemId, item, stackTrace);
660
661 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
662 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
663 // Item is in a folder, make sure this folder exists
664 if (!sBgFolders.containsKey(item.container)) {
665 // An items container is being set to a that of an item which is not in
666 // the list of Folders.
667 String msg = "item: " + item + " container being set to: " +
668 item.container + ", not in the list of folders";
669 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700670 }
671 }
672
673 // Items are added/removed from the corresponding FolderInfo elsewhere, such
674 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
675 // that are on the desktop, as appropriate
676 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800677 if (modelItem != null &&
678 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
679 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 switch (modelItem.itemType) {
681 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
682 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
683 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
684 if (!sBgWorkspaceItems.contains(modelItem)) {
685 sBgWorkspaceItems.add(modelItem);
686 }
687 break;
688 default:
689 break;
690 }
691 } else {
692 sBgWorkspaceItems.remove(modelItem);
693 }
694 }
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700699 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700700 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 item.cellX = cellX;
704 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 // We store hotseat items in canonical form which is this orientation invariant position
707 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700709 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700713 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
715 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
718 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800719 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700723 }
724
725 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
727 * cellX, cellY have already been updated on the ItemInfos.
728 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800729 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700730 final long container, final int screen) {
731
732 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
733 int count = items.size();
734
735 for (int i = 0; i < count; i++) {
736 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 item.container = container;
738
739 // We store hotseat items in canonical form which is this orientation invariant position
740 // in the hotseat
741 if (context instanceof Launcher && screen < 0 &&
742 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 item.cellY);
745 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 }
748
749 final ContentValues values = new ContentValues();
750 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
751 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
752 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800753 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700754 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755
756 contentValues.add(values);
757 }
758 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
759 }
760
761 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700762 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800763 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700764 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700766 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800767 item.cellX = cellX;
768 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700769 item.spanX = spanX;
770 item.spanY = spanY;
771
772 // We store hotseat items in canonical form which is this orientation invariant position
773 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 }
Adam Cohend4844c32011-02-18 19:25:06 -0800780
Adam Cohend4844c32011-02-18 19:25:06 -0800781 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800782 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
784 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800785 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
787 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800789
Michael Jurka816474f2012-06-25 14:49:02 -0700790 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792
793 /**
794 * Update an item to the database in a specified container.
795 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700796 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100798 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700799 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800800 }
801
Sunny Goyal756a28a2015-04-23 17:07:55 -0700802 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700803 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700804 synchronized (mLock) {
805 if (!mHasLoaderCompletedOnce ||
806 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
807 throw new RuntimeException("Trying to add shortcut while loader is running");
808 }
809 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700810 }
811 }
812
Adam Cohend4844c32011-02-18 19:25:06 -0800813 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700814 * Returns true if the shortcuts already exists on the workspace. This must be called after
815 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700817 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
818 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700819 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700820 if (intent.getComponent() != null) {
821 // If component is not null, an intent with null package will produce
822 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700823 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700824 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700825 intentWithPkg = intent.toUri(0);
826 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700827 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700828 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
829 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700830 }
831 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700832 intentWithPkg = intent.toUri(0);
833 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700834 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700835
836 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700837 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700838 if (item instanceof ShortcutInfo) {
839 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700840 Intent targetIntent = info.promisedIntent == null
841 ? info.intent : info.promisedIntent;
842 if (targetIntent != null && info.user.equals(user)) {
843 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700844 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
845 return true;
846 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700847 }
848 }
849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700851 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700852 }
853
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 * Add an item to the database in a specified container. Sets the container, screen, cellX and
856 * cellY fields of the item. Also assigns an ID to the item.
857 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700858 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700859 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 item.cellX = cellX;
862 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 // We store hotseat items in canonical form which is this orientation invariant position
864 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700866 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700867 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871
872 final ContentValues values = new ContentValues();
873 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100874 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875
Sunny Goyald2497482015-09-22 18:24:19 -0700876 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
877 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
878
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700879 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700880
Jason Monk8e19cf22014-03-20 15:06:57 -0400881 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700882 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700883 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700884 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700887 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400888 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 sBgItemsIdMap.put(item.id, item);
890 switch (item.itemType) {
891 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
892 sBgFolders.put(item.id, (FolderInfo) item);
893 // Fall through
894 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
895 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
896 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
897 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
898 sBgWorkspaceItems.add(item);
899 } else {
900 if (!sBgFolders.containsKey(item.container)) {
901 // Adding an item to a folder that doesn't exist.
902 String msg = "adding item: " + item + " to a folder that " +
903 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700904 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700907 break;
908 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
909 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
910 break;
911 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700912 }
913 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700916 }
917
Sunny Goyal34942622014-08-29 17:20:55 -0700918 private static ArrayList<ItemInfo> getItemsByPackageName(
919 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700920 ItemInfoFilter filter = new ItemInfoFilter() {
921 @Override
922 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
923 return cn.getPackageName().equals(pn) && info.user.equals(user);
924 }
925 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700926 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700927 }
928
929 /**
930 * Removes all the items from the database corresponding to the specified package.
931 */
932 static void deletePackageFromDatabase(Context context, final String pn,
933 final UserHandleCompat user) {
934 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700935 }
936
937 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700938 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700940 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700941 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
942 items.add(item);
943 deleteItemsFromDatabase(context, items);
944 }
945
946 /**
947 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700948 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700949 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700951 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700953 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700954 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700955 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700957 // Lock on mBgLock *after* the db operation
958 synchronized (sBgLock) {
959 switch (item.itemType) {
960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700962 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700963 if (info.container == item.id) {
964 // We are deleting a folder which still contains items that
965 // think they are contained by that folder.
966 String msg = "deleting a folder (" + item + ") which still " +
967 "contains items (" + info + ")";
968 Log.e(TAG, msg);
969 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700970 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700971 sBgWorkspaceItems.remove(item);
972 break;
973 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
974 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
975 sBgWorkspaceItems.remove(item);
976 break;
977 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
978 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
979 break;
980 }
981 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700983 }
984 }
Michael Jurka83df1882011-08-31 20:59:26 -0700985 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700986 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 }
988
989 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 * Update the order of the workspace screens in the database. The array list contains
991 * a list of screen ids in the order that they should appear.
992 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700993 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700994 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700995 final ContentResolver cr = context.getContentResolver();
996 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
997
998 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700999 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 while (iter.hasNext()) {
1001 long id = iter.next();
1002 if (id < 0) {
1003 iter.remove();
1004 }
1005 }
1006
1007 Runnable r = new Runnable() {
1008 @Override
1009 public void run() {
Yura085c8532014-02-11 15:15:29 +00001010 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001011 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001012 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001013 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001014 for (int i = 0; i < count; i++) {
1015 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001016 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1018 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001019 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001020 }
Yura085c8532014-02-11 15:15:29 +00001021
1022 try {
1023 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1024 } catch (Exception ex) {
1025 throw new RuntimeException(ex);
1026 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001027
Winson Chungba9c37f2013-08-30 14:11:37 -07001028 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001029 sBgWorkspaceScreens.clear();
1030 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001031 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001032 }
1033 };
1034 runOnWorkerThread(r);
1035 }
1036
1037 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001038 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 */
Winsonc0b52fe2015-09-09 16:38:15 -07001040 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 final ContentResolver cr = context.getContentResolver();
1042
Michael Jurkac9d95c52011-08-29 14:03:34 -07001043 Runnable r = new Runnable() {
1044 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001045 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001047 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 sBgItemsIdMap.remove(info.id);
1049 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 sBgWorkspaceItems.remove(info);
1051 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001052
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001053 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001054 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001056 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 for (ItemInfo childInfo : info.contents) {
1058 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001060 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001061 }
1062 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 }
1065
1066 /**
1067 * Set this as the current Launcher activity object for the loader.
1068 */
1069 public void initialize(Callbacks callbacks) {
1070 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001071 Preconditions.assertUIThread();
1072 // Remove any queued UI runnables
1073 mHandler.cancelAll();
1074 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 }
1076 }
1077
Kenny Guyed131872014-04-30 03:02:21 +01001078 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001079 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001080 int op = PackageUpdatedTask.OP_UPDATE;
1081 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1082 user));
1083 }
1084
1085 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001086 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001087 int op = PackageUpdatedTask.OP_REMOVE;
1088 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1089 user));
1090 }
1091
1092 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001093 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001094 int op = PackageUpdatedTask.OP_ADD;
1095 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1096 user));
1097 }
1098
1099 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001100 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001101 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001102 enqueuePackageUpdated(
1103 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001104 }
1105
1106 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001107 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001108 boolean replacing) {
1109 if (!replacing) {
1110 enqueuePackageUpdated(new PackageUpdatedTask(
1111 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1112 user));
1113 }
Kenny Guyed131872014-04-30 03:02:21 +01001114 }
1115
Kenny Guy44cba692016-01-21 19:50:02 +00001116 @Override
1117 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1118 enqueuePackageUpdated(new PackageUpdatedTask(
1119 PackageUpdatedTask.OP_SUSPEND, packageNames,
1120 user));
1121 }
1122
1123 @Override
1124 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1125 enqueuePackageUpdated(new PackageUpdatedTask(
1126 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1127 user));
1128 }
1129
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1132 * ACTION_PACKAGE_CHANGED.
1133 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001134 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001135 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001136 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001137
Joe Onorato36115782010-06-17 13:28:48 -04001138 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001139 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001140 // If we have changed locale we need to clear out the labels in all apps/workspace.
1141 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001142 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001143 Callbacks callbacks = getCallback();
1144 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001145 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001146 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001147 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001148 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001149 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001150 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001151 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1152 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001153 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1154 if (user != null) {
1155 enqueuePackageUpdated(new PackageUpdatedTask(
1156 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1157 new String[0], user));
1158 }
Tony Wickham827cef22016-03-17 15:39:39 -07001159 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1160 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001161 }
1162 }
1163
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001164 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001165 resetLoadedState(true, true);
1166
Reena Lee93f824a2011-09-23 17:20:28 -07001167 // Do this here because if the launcher activity is running it will be restarted.
1168 // If it's not running startLoaderFromBackground will merely tell it that it needs
1169 // to reload.
1170 startLoaderFromBackground();
1171 }
1172
Winson Chungf0c6ae02012-03-21 16:10:31 -07001173 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1174 synchronized (mLock) {
1175 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1176 // mWorkspaceLoaded to true later
1177 stopLoaderLocked();
1178 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1179 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1180 }
1181 }
1182
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001183 /**
1184 * When the launcher is in the background, it's possible for it to miss paired
1185 * configuration changes. So whenever we trigger the loader from the background
1186 * tell the launcher that it needs to re-run the loader when it comes back instead
1187 * of doing it now.
1188 */
1189 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001190 Callbacks callbacks = getCallback();
1191 if (callbacks != null) {
1192 // Only actually run the loader if they're not paused.
1193 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001194 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001195 }
1196 }
Joe Onorato36115782010-06-17 13:28:48 -04001197 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001198
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001199 /**
1200 * If there is already a loader task running, tell it to stop.
1201 */
1202 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001203 LoaderTask oldTask = mLoaderTask;
1204 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001205 oldTask.stopLocked();
1206 }
Reena Lee93f824a2011-09-23 17:20:28 -07001207 }
1208
Adam Cohen1a85c582014-09-30 09:48:49 -07001209 public boolean isCurrentCallbacks(Callbacks callbacks) {
1210 return (mCallbacks != null && mCallbacks.get() == callbacks);
1211 }
1212
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001213 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001214 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1215 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001216 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001217 // Don't bother to start the thread if we know it's not going to do anything
1218 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001219 final Callbacks oldCallbacks = mCallbacks.get();
1220 // Clear any pending bind-runnables from the synchronized load process.
1221 runOnMainThread(new Runnable() {
1222 public void run() {
1223 oldCallbacks.clearPendingBinds();
1224 }
1225 });
1226
Joe Onorato36115782010-06-17 13:28:48 -04001227 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001228 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001229 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001230 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001231 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001232 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1233 } else {
1234 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1235 sWorker.post(mLoaderTask);
1236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 }
1239 }
1240
Joe Onorato36115782010-06-17 13:28:48 -04001241 public void stopLoader() {
1242 synchronized (mLock) {
1243 if (mLoaderTask != null) {
1244 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 }
1246 }
Joe Onorato36115782010-06-17 13:28:48 -04001247 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001249 /**
1250 * Loads the workspace screen ids in an ordered list.
1251 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001252 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001253 final ContentResolver contentResolver = context.getContentResolver();
1254 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001255
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001256 // Get screens ordered by rank.
1257 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1258 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1259 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001260 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001261 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001262 while (sc.moveToNext()) {
1263 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001264 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001265 } catch (Exception e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001266 FileLog.d(TAG, "Invalid screen id", e);
Winson Chung76828c82013-08-19 15:43:29 -07001267 }
1268 }
1269 } finally {
1270 sc.close();
1271 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001272 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001273 }
1274
Joe Onorato36115782010-06-17 13:28:48 -04001275 /**
1276 * Runnable for the thread that loads the contents of the launcher:
1277 * - workspace icons
1278 * - widgets
1279 * - all apps icons
1280 */
1281 private class LoaderTask implements Runnable {
1282 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001283 private int mPageToBindFirst;
1284
Adam Cohen091440a2015-03-18 14:16:05 -07001285 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001286 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001287 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001288
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001289 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001290 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001291 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 }
1293
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001294 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001295 mIsLoadingAndBindingWorkspace = true;
1296
Joe Onorato36115782010-06-17 13:28:48 -04001297 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001298 if (DEBUG_LOADERS) {
1299 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001301
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001302 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001303 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001304 synchronized (LoaderTask.this) {
1305 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001306 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001307 }
1308 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
1310 }
1311
Joe Onorato36115782010-06-17 13:28:48 -04001312 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001313 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001314 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001315
Joe Onorato36115782010-06-17 13:28:48 -04001316 private void waitForIdle() {
1317 // Wait until the either we're stopped or the other threads are done.
1318 // This way we don't start loading all apps until the workspace has settled
1319 // down.
1320 synchronized (LoaderTask.this) {
1321 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001322
Joe Onorato36115782010-06-17 13:28:48 -04001323 mHandler.postIdle(new Runnable() {
1324 public void run() {
1325 synchronized (LoaderTask.this) {
1326 mLoadAndBindStepFinished = true;
1327 if (DEBUG_LOADERS) {
1328 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001329 }
Joe Onorato36115782010-06-17 13:28:48 -04001330 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001331 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001332 }
Joe Onorato36115782010-06-17 13:28:48 -04001333 });
1334
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001335 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001336 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001337 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1338 // wait no longer than 1sec at a time
1339 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001340 } catch (InterruptedException ex) {
1341 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001342 }
1343 }
Joe Onorato36115782010-06-17 13:28:48 -04001344 if (DEBUG_LOADERS) {
1345 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001346 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001347 + "ms for previous step to finish binding");
1348 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001351
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001352 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001353 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354 // Ensure that we have a valid page index to load synchronously
1355 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1356 "valid page index");
1357 }
1358 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1359 // Ensure that we don't try and bind a specified page when the pages have not been
1360 // loaded already (we should load everything asynchronously in that case)
1361 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1362 }
1363 synchronized (mLock) {
1364 if (mIsLoaderTaskRunning) {
1365 // Ensure that we are never running the background loading at this point since
1366 // we also touch the background collections
1367 throw new RuntimeException("Error! Background loading is already running");
1368 }
1369 }
1370
1371 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1372 // data structures, we can't allow any other thread to touch that data, but because
1373 // this call is synchronous, we can get away with not locking).
1374
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001375 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001376 // operations from the previous activity. We need to ensure that all queued operations
1377 // are executed before any synchronous binding work is done.
1378 mHandler.flush();
1379
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 // Divide the set of loaded items into those that we are binding synchronously, and
1381 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001382 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1384 // arise from that.
1385 onlyBindAllApps();
1386 }
1387
Joe Onorato36115782010-06-17 13:28:48 -04001388 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001390 if (mStopped) {
1391 return;
1392 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 mIsLoaderTaskRunning = true;
1394 }
Joe Onorato36115782010-06-17 13:28:48 -04001395 // Optimize for end-user experience: if the Launcher is up and // running with the
1396 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1397 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001398 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001399 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001400 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001401
Joe Onorato36115782010-06-17 13:28:48 -04001402 if (mStopped) {
1403 break keep_running;
1404 }
1405
Joe Onorato36115782010-06-17 13:28:48 -04001406 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001407
1408 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001409 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1410 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412
Joe Onorato36115782010-06-17 13:28:48 -04001413 // Clear out this reference, otherwise we end up holding it until all of the
1414 // callback runnables are done.
1415 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416
Joe Onorato36115782010-06-17 13:28:48 -04001417 synchronized (mLock) {
1418 // If we are still the last one to be scheduled, remove ourselves.
1419 if (mLoaderTask == this) {
1420 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001423 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 }
1426
1427 public void stopLocked() {
1428 synchronized (LoaderTask.this) {
1429 mStopped = true;
1430 this.notify();
1431 }
1432 }
1433
1434 /**
1435 * Gets the callbacks object. If we've been stopped, or if the launcher object
1436 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1437 * object that was around when the deferred message was scheduled, and if there's
1438 * a new Callbacks object around then also return null. This will save us from
1439 * calling onto it with data that will be ignored.
1440 */
1441 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1442 synchronized (mLock) {
1443 if (mStopped) {
1444 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001445 }
Joe Onorato36115782010-06-17 13:28:48 -04001446
1447 if (mCallbacks == null) {
1448 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001449 }
Joe Onorato36115782010-06-17 13:28:48 -04001450
1451 final Callbacks callbacks = mCallbacks.get();
1452 if (callbacks != oldCallbacks) {
1453 return null;
1454 }
1455 if (callbacks == null) {
1456 Log.w(TAG, "no mCallbacks");
1457 return null;
1458 }
1459
1460 return callbacks;
1461 }
1462 }
1463
1464 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001465 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001466 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001467 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001468 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001469
Adam Cohendcd297f2013-06-18 13:13:40 -07001470 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001471 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001472 // Return early if we detect that an item is under the hotseat button
1473 if (mCallbacks == null ||
1474 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001475 Log.e(TAG, "Error loading shortcut into hotseat " + item
1476 + " into position (" + item.screenId + ":" + item.cellX + ","
1477 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001478 return false;
1479 }
1480
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001481 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001482 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1483
Adam Cohen2e6da152015-05-06 11:42:25 -07001484 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001485 Log.e(TAG, "Error loading shortcut " + item
1486 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001487 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001488 + ")");
1489 return false;
1490 }
1491
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001492 if (hotseatOccupancy != null) {
1493 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 Log.e(TAG, "Error loading shortcut into hotseat " + item
1495 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001496 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001498 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001499 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001500 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001502 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001503 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1504 occupancy.cells[(int) item.screenId][0] = true;
1505 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001506 return true;
1507 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001508 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1509 if (!workspaceScreens.contains((Long) item.screenId)) {
1510 // The item has an invalid screen id.
1511 return false;
1512 }
1513 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001514 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001515 return true;
1516 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001517
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001518 final int countX = profile.numColumns;
1519 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001520 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1521 item.cellX < 0 || item.cellY < 0 ||
1522 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1523 Log.e(TAG, "Error loading shortcut " + item
1524 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1525 + item.cellX + "," + item.cellY
1526 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1527 return false;
1528 }
1529
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001530 if (!occupied.containsKey(item.screenId)) {
1531 occupied.put(item.screenId, new GridOccupancy(countX + 1, countY + 1));
Joe Onorato36115782010-06-17 13:28:48 -04001532 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001533 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001534
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001535 // Check if any workspace icons overlap with each other
1536 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1537 occupancy.markCells(item, true);
1538 return true;
1539 } else {
1540 Log.e(TAG, "Error loading shortcut " + item
1541 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1542 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1543 + ") already occupied");
1544 return false;
1545 }
Joe Onorato36115782010-06-17 13:28:48 -04001546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001547
Winson Chungba9c37f2013-08-30 14:11:37 -07001548 /** Clears all the sBg data structures */
1549 private void clearSBgDataStructures() {
1550 synchronized (sBgLock) {
1551 sBgWorkspaceItems.clear();
1552 sBgAppWidgets.clear();
1553 sBgFolders.clear();
1554 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001555 sBgWorkspaceScreens.clear();
1556 }
1557 }
1558
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001559 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001560 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561
Joe Onorato36115782010-06-17 13:28:48 -04001562 final Context context = mContext;
1563 final ContentResolver contentResolver = context.getContentResolver();
1564 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001565 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001566 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001567 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001568
Winson Chung892c74d2013-08-22 16:15:50 -07001569 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001570 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001571 int countX = profile.numColumns;
1572 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001573
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001574 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001575 if (GridSizeMigrationTask.ENABLED &&
1576 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1577 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001578 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001579 }
1580
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001581 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001582 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001583 LauncherSettings.Settings.call(contentResolver,
1584 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001585 }
1586
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001587 Log.d(TAG, "loadWorkspace: loading default favorites");
1588 LauncherSettings.Settings.call(contentResolver,
1589 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001590
Winson Chung2abf94d2012-07-18 18:16:38 -07001591 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001592 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001593 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001594 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001595 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001596
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001597 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1598 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001599 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001600 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001601 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001602
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001603 // +1 for the hotseat (it can be larger than the workspace)
1604 // Load workspace in reverse order to ensure that latest items are loaded first (and
1605 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001607 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001609 try {
1610 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1611 final int intentIndex = c.getColumnIndexOrThrow
1612 (LauncherSettings.Favorites.INTENT);
1613 final int titleIndex = c.getColumnIndexOrThrow
1614 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001615 final int containerIndex = c.getColumnIndexOrThrow(
1616 LauncherSettings.Favorites.CONTAINER);
1617 final int itemTypeIndex = c.getColumnIndexOrThrow(
1618 LauncherSettings.Favorites.ITEM_TYPE);
1619 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1620 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001621 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1622 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 final int screenIndex = c.getColumnIndexOrThrow(
1624 LauncherSettings.Favorites.SCREEN);
1625 final int cellXIndex = c.getColumnIndexOrThrow
1626 (LauncherSettings.Favorites.CELLX);
1627 final int cellYIndex = c.getColumnIndexOrThrow
1628 (LauncherSettings.Favorites.CELLY);
1629 final int spanXIndex = c.getColumnIndexOrThrow
1630 (LauncherSettings.Favorites.SPANX);
1631 final int spanYIndex = c.getColumnIndexOrThrow(
1632 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001633 final int rankIndex = c.getColumnIndexOrThrow(
1634 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001635 final int restoredIndex = c.getColumnIndexOrThrow(
1636 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001637 final int profileIdIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001639 final int optionsIndex = c.getColumnIndexOrThrow(
1640 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001641 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642
Sunny Goyal7f834d22015-04-21 10:10:23 -07001643 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001644 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001645 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001646 long serialNo = mUserManager.getSerialNumberForUser(user);
1647 allUsers.put(serialNo, user);
1648 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001649 }
1650
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001651 ShortcutInfo info;
1652 String intentDescription;
1653 LauncherAppWidgetInfo appWidgetInfo;
1654 int container;
1655 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001656 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001658 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001659 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001661 while (!mStopped && c.moveToNext()) {
1662 try {
1663 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001664 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001665 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001666 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001668 switch (itemType) {
1669 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1670 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001671 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001673 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001674 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001675 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001676 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001677 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001678 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001679 if (user == null) {
1680 // User has been deleted remove the item.
1681 itemsToRemove.add(id);
1682 continue;
1683 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 try {
1685 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001686 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001687 if (cn != null && cn.getPackageName() != null) {
1688 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1689 cn.getPackageName(), user);
1690 boolean validComponent = validPkg &&
1691 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001692 if (validPkg) {
1693 targetPackage = cn.getPackageName();
1694 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001695
1696 if (validComponent) {
1697 if (restored) {
1698 // no special handling necessary for this item
1699 restoredRows.add(id);
1700 restored = false;
1701 }
Kenny Guyff05f432016-01-22 17:48:29 +00001702 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001703 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001704 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001705 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001706 intent = null;
1707 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1708 // We allow auto install apps to have their intent
1709 // updated after an install.
1710 intent = manager.getLaunchIntentForPackage(
1711 cn.getPackageName());
1712 if (intent != null) {
1713 ContentValues values = new ContentValues();
1714 values.put(LauncherSettings.Favorites.INTENT,
1715 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001716 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001717 }
1718 }
1719
1720 if (intent == null) {
1721 // The app is installed but the component is no
1722 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001723 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001724 itemsToRemove.add(id);
1725 continue;
1726 } else {
1727 // no special handling necessary for this item
1728 restoredRows.add(id);
1729 restored = false;
1730 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001731 } else if (restored) {
1732 // Package is not yet available but might be
1733 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001734 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001735
1736 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1737 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001738 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001739 // App restore has started. Update the flag
1740 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1741 ContentValues values = new ContentValues();
1742 values.put(LauncherSettings.Favorites.RESTORED,
1743 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001744 updateItem(id, values);
1745 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1746 // This is a common app. Try to replace this.
1747 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1748 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1749 if (parser.findDefaultApp()) {
1750 // Default app found. Replace it.
1751 intent = parser.parsedIntent;
1752 cn = intent.getComponent();
1753 ContentValues values = parser.parsedValues;
1754 values.put(LauncherSettings.Favorites.RESTORED, 0);
1755 updateItem(id, values);
1756 restored = false;
1757 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001758
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001759 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001760 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001761 itemsToRemove.add(id);
1762 continue;
1763 }
Sunny Goyal94485362014-09-18 16:13:58 -07001764 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001765 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001766 itemsToRemove.add(id);
1767 continue;
1768 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001769 } else if (PackageManagerHelper.isAppOnSdcard(
1770 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001771 // Package is present but not available.
1772 allowMissingTarget = true;
1773 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1774 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001775 // SdCard is not ready yet. Package might get available,
1776 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001777 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001778 HashSet<String> pkgs = sPendingPackages.get(user);
1779 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001780 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001781 sPendingPackages.put(user, pkgs);
1782 }
1783 pkgs.add(cn.getPackageName());
1784 allowMissingTarget = true;
1785 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001786
1787 } else {
1788 // Do not wait for external media load anymore.
1789 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001790 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001791 itemsToRemove.add(id);
1792 continue;
Winson Chungee055712013-07-30 14:46:24 -07001793 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001794 } else if (cn == null) {
1795 // For shortcuts with no component, keep them as they are
1796 restoredRows.add(id);
1797 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001798 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001800 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001801 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001802 continue;
1803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Sunny Goyal34b65272015-03-11 16:56:52 -07001805 boolean useLowResIcon = container >= 0 &&
1806 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1807
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001808 if (itemReplaced) {
1809 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001810 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001811 cursorIconInfo.iconIndex, titleIndex,
1812 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001813 } else {
1814 // Don't replace items for other profiles.
1815 itemsToRemove.add(id);
1816 continue;
1817 }
1818 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001819 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001820 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001821 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001822 intent = getRestoredItemIntent(c, context, intent);
1823 } else {
1824 // Don't restore items for other profiles.
1825 itemsToRemove.add(id);
1826 continue;
1827 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001828 } else if (itemType ==
1829 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001830 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001831 cursorIconInfo.iconIndex, titleIndex,
1832 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001834 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001835
Sunny Goyald09c3702016-04-06 16:18:20 -07001836 // Shortcuts are only available on the primary profile
1837 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1838 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1839 }
1840
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 // App shortcuts that used to be automatically added to Launcher
1842 // didn't always have the correct intent flags set, so do that
1843 // here
1844 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001845 intent.getCategories() != null &&
1846 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001847 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 intent.addFlags(
1849 Intent.FLAG_ACTIVITY_NEW_TASK |
1850 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1851 }
Michael Jurka96879562012-03-22 05:54:33 -07001852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001855 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001858 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 info.cellX = c.getInt(cellXIndex);
1860 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001861 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001862 info.spanX = 1;
1863 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001864 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001865 if (info.promisedIntent != null) {
1866 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1867 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001868 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001869 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1870 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1871 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001872
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001874 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001875 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 break;
1877 }
1878
Sunny Goyal756adbc2015-04-16 15:20:51 -07001879 if (restored) {
1880 ComponentName cn = info.getTargetComponent();
1881 if (cn != null) {
1882 Integer progress = installingPkgs.get(cn.getPackageName());
1883 if (progress != null) {
1884 info.setInstallProgress(progress);
1885 } else {
1886 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1887 }
1888 }
1889 }
1890
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 switch (container) {
1892 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1893 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1894 sBgWorkspaceItems.add(info);
1895 break;
1896 default:
1897 // Item is in a user folder
1898 FolderInfo folderInfo =
1899 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001900 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 break;
1902 }
1903 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001904 } else {
1905 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 }
1907 break;
1908
1909 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1910 id = c.getLong(idIndex);
1911 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1912
Sunny Goyala508e4f2015-05-21 09:33:57 -07001913 // Do not trim the folder label, as is was set by the user.
1914 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001915 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 folderInfo.cellX = c.getInt(cellXIndex);
1919 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001920 folderInfo.spanX = 1;
1921 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001922 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923
Daniel Sandler8802e962010-05-26 16:28:16 -04001924 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001925 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001926 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001927 break;
1928 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001929
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1932 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1933 sBgWorkspaceItems.add(folderInfo);
1934 break;
Joe Onorato36115782010-06-17 13:28:48 -04001935 }
Joe Onorato17a89222011-02-08 17:26:11 -08001936
Chris Wrenf4d08112014-01-16 18:13:56 -05001937 if (restored) {
1938 // no special handling required for restored folders
1939 restoredRows.add(id);
1940 }
1941
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001942 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1943 sBgFolders.put(folderInfo.id, folderInfo);
1944 break;
1945
1946 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001947 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001949 boolean customWidget = itemType ==
1950 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1951
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001953 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001954 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001955 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001956 user = allUsers.get(serialNumber);
1957 if (user == null) {
1958 itemsToRemove.add(id);
1959 continue;
1960 }
1961
Sunny Goyalff572272014-07-23 13:58:07 -07001962 final ComponentName component =
1963 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001964
Sunny Goyal651077b2014-06-30 14:15:31 -07001965 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001966 final boolean isIdValid = (restoreStatus &
1967 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001968 final boolean wasProviderReady = (restoreStatus &
1969 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001970
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001971 if (widgetProvidersMap == null) {
1972 widgetProvidersMap = AppWidgetManagerCompat
1973 .getInstance(mContext).getAllProvidersMap();
1974 }
1975 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1976 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001977 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001978 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001979
1980 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001981 if (!isSafeMode && !customWidget &&
1982 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001983 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001984 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 itemsToRemove.add(id);
1986 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001987 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001988 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1989 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001990
Sunny Goyal53f96722015-07-13 19:54:53 -07001991 // The provider is available. So the widget is either
1992 // available or not available. We do not need to track
1993 // any future restore updates.
1994 int status = restoreStatus &
1995 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001996 if (!wasProviderReady) {
1997 // If provider was not previously ready, update the
1998 // status and UI flag.
1999
2000 // Id would be valid only if the widget restore broadcast was received.
2001 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002002 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002003 } else {
2004 status &= ~LauncherAppWidgetInfo
2005 .FLAG_PROVIDER_NOT_READY;
2006 }
2007 }
2008 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002009 } else {
2010 Log.v(TAG, "Widget restore pending id=" + id
2011 + " appWidgetId=" + appWidgetId
2012 + " status =" + restoreStatus);
2013 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002014 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002015 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002016 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002017
2018 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2019 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002020 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002021 // App restore has started. Update the flag
2022 appWidgetInfo.restoreStatus |=
2023 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002024 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002025 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002026 itemsToRemove.add(id);
2027 continue;
2028 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002029
2030 appWidgetInfo.installProgress =
2031 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002032 }
Sunny Goyalff572272014-07-23 13:58:07 -07002033
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002035 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 appWidgetInfo.cellX = c.getInt(cellXIndex);
2037 appWidgetInfo.cellY = c.getInt(cellYIndex);
2038 appWidgetInfo.spanX = c.getInt(spanXIndex);
2039 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002040 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2043 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2044 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002045 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2046 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 continue;
2048 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002050 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002052 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002053 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 break;
2055 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002056
Adam Cohen59400422014-03-05 18:07:04 -08002057 if (!customWidget) {
2058 String providerName =
2059 appWidgetInfo.providerName.flattenToString();
2060 if (!providerName.equals(savedProvider) ||
2061 (appWidgetInfo.restoreStatus != restoreStatus)) {
2062 ContentValues values = new ContentValues();
2063 values.put(
2064 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2065 providerName);
2066 values.put(LauncherSettings.Favorites.RESTORED,
2067 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002068 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002069 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002070 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2072 sBgAppWidgets.add(appWidgetInfo);
2073 }
Joe Onorato36115782010-06-17 13:28:48 -04002074 break;
2075 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002077 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002078 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 }
2080 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002081 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 }
2083
Winson Chungba9c37f2013-08-30 14:11:37 -07002084 // Break early if we've stopped loading
2085 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002086 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002087 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002088 }
2089
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002092 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2093 Utilities.createDbSelectionQuery(
2094 LauncherSettings.Favorites._ID, itemsToRemove), null);
2095 if (DEBUG_LOADERS) {
2096 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2097 LauncherSettings.Favorites._ID, itemsToRemove));
2098 }
2099
2100 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002101 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2102 .call(contentResolver,
2103 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2104 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2105 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002106 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2107 sBgFolders.remove(folderId);
2108 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002109 }
2110 }
2111
Sunny Goyal317698b2015-07-29 11:45:41 -07002112 // Sort all the folder items and make sure the first 3 items are high resolution.
2113 for (FolderInfo folder : sBgFolders) {
2114 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2115 int pos = 0;
2116 for (ShortcutInfo info : folder.contents) {
2117 if (info.usingLowResIcon) {
2118 info.updateIcon(mIconCache, false);
2119 }
2120 pos ++;
2121 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2122 break;
2123 }
2124 }
2125 }
2126
Chris Wrenf4d08112014-01-16 18:13:56 -05002127 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002128 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002129 ContentValues values = new ContentValues();
2130 values.put(LauncherSettings.Favorites.RESTORED, 0);
2131 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2132 Utilities.createDbSelectionQuery(
2133 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002134 }
2135
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002136 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2137 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002138 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002139 null, sWorker);
2140 }
2141
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002142 // Remove any empty screens
2143 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002144 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002145 long screenId = item.screenId;
2146 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2147 unusedScreens.contains(screenId)) {
2148 unusedScreens.remove(screenId);
2149 }
2150 }
2151
2152 // If there are any empty screens remove them, and update.
2153 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002154 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002155 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002156 }
2157
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 if (DEBUG_LOADERS) {
2159 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2160 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002161 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002162 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002164
Sunny Goyale2df0622015-04-24 11:27:00 -07002165 for (int i = 0; i < nScreens; i++) {
2166 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002167 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002168 line += " | ";
2169 }
Joe Onorato36115782010-06-17 13:28:48 -04002170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002172 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 }
Joe Onorato36115782010-06-17 13:28:48 -04002174 }
Adam Cohene25af792013-06-06 23:08:25 -07002175 }
2176
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002177 /**
2178 * Partially updates the item without any notification. Must be called on the worker thread.
2179 */
2180 private void updateItem(long itemId, ContentValues update) {
2181 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002182 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002183 update,
2184 BaseColumns._ID + "= ?",
2185 new String[]{Long.toString(itemId)});
2186 }
2187
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 /** Filters the set of items who are directly or indirectly (via another container) on the
2189 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002190 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 ArrayList<ItemInfo> allWorkspaceItems,
2192 ArrayList<ItemInfo> currentScreenItems,
2193 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002194 // Purge any null ItemInfos
2195 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2196 while (iter.hasNext()) {
2197 ItemInfo i = iter.next();
2198 if (i == null) {
2199 iter.remove();
2200 }
2201 }
2202
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 // Order the set of items by their containers first, this allows use to walk through the
2204 // list sequentially, build up a list of containers that are in the specified screen,
2205 // as well as all items in those containers.
2206 Set<Long> itemsOnScreen = new HashSet<Long>();
2207 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2208 @Override
2209 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002210 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 }
2212 });
2213 for (ItemInfo info : allWorkspaceItems) {
2214 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002215 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 currentScreenItems.add(info);
2217 itemsOnScreen.add(info.id);
2218 } else {
2219 otherScreenItems.add(info);
2220 }
2221 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2222 currentScreenItems.add(info);
2223 itemsOnScreen.add(info.id);
2224 } else {
2225 if (itemsOnScreen.contains(info.container)) {
2226 currentScreenItems.add(info);
2227 itemsOnScreen.add(info.id);
2228 } else {
2229 otherScreenItems.add(info);
2230 }
2231 }
2232 }
2233 }
2234
2235 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002236 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 ArrayList<LauncherAppWidgetInfo> appWidgets,
2238 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2239 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240
2241 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002242 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002244 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 currentScreenWidgets.add(widget);
2246 } else {
2247 otherScreenWidgets.add(widget);
2248 }
2249 }
2250 }
2251
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2253 * right) */
2254 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002255 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002256 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002257 final int screenCols = profile.numColumns;
2258 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002260 @Override
2261 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002262 if (lhs.container == rhs.container) {
2263 // Within containers, order by their spatial position in that container
2264 switch ((int) lhs.container) {
2265 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2266 long lr = (lhs.screenId * screenCellCount +
2267 lhs.cellY * screenCols + lhs.cellX);
2268 long rr = (rhs.screenId * screenCellCount +
2269 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002270 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002271 }
2272 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2273 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002274 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002275 }
2276 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002277 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002278 throw new RuntimeException("Unexpected container type when " +
2279 "sorting workspace items.");
2280 }
2281 return 0;
2282 }
2283 } else {
2284 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002285 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002286 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002287 }
2288 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002290
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2292 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 final Runnable r = new Runnable() {
2294 @Override
2295 public void run() {
2296 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2297 if (callbacks != null) {
2298 callbacks.bindScreens(orderedScreens);
2299 }
2300 }
2301 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002302 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 }
2304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2306 final ArrayList<ItemInfo> workspaceItems,
2307 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002308 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309
2310 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002311 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002313 final int start = i;
2314 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 final Runnable r = new Runnable() {
2316 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002318 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002319 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002320 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2321 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002322 }
2323 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002325 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002326 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 // Bind the widgets, one at a time
2329 N = appWidgets.size();
2330 for (int i = 0; i < N; i++) {
2331 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2332 final Runnable r = new Runnable() {
2333 public void run() {
2334 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2335 if (callbacks != null) {
2336 callbacks.bindAppWidget(widget);
2337 }
2338 }
2339 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002340 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 }
2342 }
2343
2344 /**
2345 * Binds all loaded data to actual views on the main thread.
2346 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002347 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 final long t = SystemClock.uptimeMillis();
2349 Runnable r;
2350
2351 // Don't use these two variables in any of the callback runnables.
2352 // Otherwise we hold a reference to them.
2353 final Callbacks oldCallbacks = mCallbacks.get();
2354 if (oldCallbacks == null) {
2355 // This launcher has exited and nobody bothered to tell us. Just bail.
2356 Log.w(TAG, "LoaderTask running with no launcher");
2357 return;
2358 }
2359
Winson Chung9b9fb962013-11-15 15:39:34 -08002360 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002361 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2362 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2363 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002364
Winson Chung2abf94d2012-07-18 18:16:38 -07002365 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 workspaceItems.addAll(sBgWorkspaceItems);
2367 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002368 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 }
2370
Derek Prothro7aff3992013-12-10 14:00:37 -05002371 final boolean isLoadingSynchronously =
2372 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002373 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002375 if (currScreen >= orderedScreenIds.size()) {
2376 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002377 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002378 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002379 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002380 final long currentScreenId = currentScreen < 0
2381 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002382
Winson Chung9b9fb962013-11-15 15:39:34 -08002383 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002384 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2385 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2386 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2387 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388
Winson Chung9b9fb962013-11-15 15:39:34 -08002389 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002391 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2394 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2395
2396 // Tell the workspace that we're about to start binding items
2397 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002398 public void run() {
2399 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2400 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002401 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002403 }
2404 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002406 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2409
Sunny Goyal527c7d32015-08-28 15:19:36 -07002410 Executor mainExecutor = new DeferredMainThreadExecutor();
2411 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002412 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413
Sunny Goyal527c7d32015-08-28 15:19:36 -07002414 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2415 // remaining screens after first onDraw is called. This ensures that the first screen
2416 // is immediately visible (eg. during rotation)
2417 // In case of !isLoadingSynchronously, bind all pages one after other.
2418 final Executor deferredExecutor = isLoadingSynchronously ?
2419 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2420
Sunny Goyal44c06432016-04-02 10:56:02 -07002421 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422
2423 // Tell the workspace that we're done binding items
2424 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002425 public void run() {
2426 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2427 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002428 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002429 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430
Sunny Goyal639e9062015-08-19 19:17:06 -07002431 mIsLoadingAndBindingWorkspace = false;
2432
2433 // Run all the bind complete runnables after workspace is bound.
2434 if (!mBindCompleteRunnables.isEmpty()) {
2435 synchronized (mBindCompleteRunnables) {
2436 for (final Runnable r : mBindCompleteRunnables) {
2437 runOnWorkerThread(r);
2438 }
2439 mBindCompleteRunnables.clear();
2440 }
2441 }
2442
Winson Chung98e030b2012-05-07 16:01:11 -07002443 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002444 if (DEBUG_LOADERS) {
2445 Log.d(TAG, "bound workspace in "
2446 + (SystemClock.uptimeMillis()-t) + "ms");
2447 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002448
Joe Onorato36115782010-06-17 13:28:48 -04002449 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002451 deferredExecutor.execute(r);
2452
Winson Chung4a2afa32012-07-19 14:53:05 -07002453 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002454 r = new Runnable() {
2455 public void run() {
2456 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2457 if (callbacks != null) {
2458 // We are loading synchronously, which means, some of the pages will be
2459 // bound after first draw. Inform the callbacks that page binding is
2460 // not complete, and schedule the remaining pages.
2461 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2462 callbacks.onPageBoundSynchronously(currentScreen);
2463 }
2464 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2465 }
2466 }
2467 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002468 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002469 }
Joe Onorato36115782010-06-17 13:28:48 -04002470 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002471
Joe Onorato36115782010-06-17 13:28:48 -04002472 private void loadAndBindAllApps() {
2473 if (DEBUG_LOADERS) {
2474 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2475 }
2476 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002477 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002478 synchronized (LoaderTask.this) {
2479 if (mStopped) {
2480 return;
2481 }
2482 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002483 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002484 synchronized (LoaderTask.this) {
2485 if (mStopped) {
2486 return;
2487 }
2488 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002489 }
Joe Onorato36115782010-06-17 13:28:48 -04002490 } else {
2491 onlyBindAllApps();
2492 }
2493 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002494
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002495 private void updateIconCache() {
2496 // Ignore packages which have a promise icon.
2497 HashSet<String> packagesToIgnore = new HashSet<>();
2498 synchronized (sBgLock) {
2499 for (ItemInfo info : sBgItemsIdMap) {
2500 if (info instanceof ShortcutInfo) {
2501 ShortcutInfo si = (ShortcutInfo) info;
2502 if (si.isPromise() && si.getTargetComponent() != null) {
2503 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2504 }
2505 } else if (info instanceof LauncherAppWidgetInfo) {
2506 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2507 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2508 packagesToIgnore.add(lawi.providerName.getPackageName());
2509 }
2510 }
2511 }
2512 }
2513 mIconCache.updateDbIcons(packagesToIgnore);
2514 }
2515
Joe Onorato36115782010-06-17 13:28:48 -04002516 private void onlyBindAllApps() {
2517 final Callbacks oldCallbacks = mCallbacks.get();
2518 if (oldCallbacks == null) {
2519 // This launcher has exited and nobody bothered to tell us. Just bail.
2520 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2521 return;
2522 }
2523
2524 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002525 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002526 final ArrayList<AppInfo> list
2527 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002528 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002529 public void run() {
2530 final long t = SystemClock.uptimeMillis();
2531 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532 if (callbacks != null) {
2533 callbacks.bindAllApplications(list);
2534 }
2535 if (DEBUG_LOADERS) {
2536 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002537 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002538 }
2539 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002540 };
2541 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002542 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002543 r.run();
2544 } else {
2545 mHandler.post(r);
2546 }
Joe Onorato36115782010-06-17 13:28:48 -04002547 }
2548
Winson Chung64359a52013-07-08 17:17:08 -07002549 private void loadAllApps() {
2550 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002551
Joe Onorato36115782010-06-17 13:28:48 -04002552 final Callbacks oldCallbacks = mCallbacks.get();
2553 if (oldCallbacks == null) {
2554 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002555 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002556 return;
2557 }
2558
Kenny Guyed131872014-04-30 03:02:21 +01002559 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2560
Winson Chung64359a52013-07-08 17:17:08 -07002561 // Clear the list of apps
2562 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002563 for (UserHandleCompat user : profiles) {
2564 // Query for the set of apps
2565 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002566 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002567 if (DEBUG_LOADERS) {
2568 Log.d(TAG, "getActivityList took "
2569 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2570 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2571 }
2572 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002573 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002574 if (apps == null || apps.isEmpty()) {
2575 return;
2576 }
Kenny Guyff05f432016-01-22 17:48:29 +00002577 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002578 // Create the ApplicationInfos
2579 for (int i = 0; i < apps.size(); i++) {
2580 LauncherActivityInfoCompat app = apps.get(i);
2581 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002582 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002583 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002584
Sunny Goyal756a28a2015-04-23 17:07:55 -07002585 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2586 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002587 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002588
2589 @Override
2590 public void run() {
2591 heuristic.processUserApps(apps);
2592 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002593 };
2594 runOnMainThread(new Runnable() {
2595
2596 @Override
2597 public void run() {
2598 // Check isLoadingWorkspace on the UI thread, as it is updated on
2599 // the UI thread.
2600 if (mIsLoadingAndBindingWorkspace) {
2601 synchronized (mBindCompleteRunnables) {
2602 mBindCompleteRunnables.add(r);
2603 }
2604 } else {
2605 runOnWorkerThread(r);
2606 }
2607 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002608 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002609 }
Winson Chung64359a52013-07-08 17:17:08 -07002610 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002611 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002612 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2613 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002614
2615 // Post callback on main thread
2616 mHandler.post(new Runnable() {
2617 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002618
Winson Chung64359a52013-07-08 17:17:08 -07002619 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002620 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002621 if (callbacks != null) {
2622 callbacks.bindAllApplications(added);
2623 if (DEBUG_LOADERS) {
2624 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002625 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002626 }
2627 } else {
2628 Log.i(TAG, "not binding apps: no Launcher activity");
2629 }
2630 }
2631 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002632 // Cleanup any data stored for a deleted user.
2633 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002634 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002635 Log.d(TAG, "Icons processed in "
2636 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002637 }
2638 }
2639
2640 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002641 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002642 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2644 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2645 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2646 }
Joe Onorato36115782010-06-17 13:28:48 -04002647 }
2648 }
2649
Sunny Goyal75b0f552015-05-20 21:57:06 -07002650 /**
2651 * Called when the icons for packages have been updated in the icon cache.
2652 */
2653 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2654 final Callbacks callbacks = getCallback();
2655 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2656 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2657
2658 // If any package icon has changed (app was updated while launcher was dead),
2659 // update the corresponding shortcuts.
2660 synchronized (sBgLock) {
2661 for (ItemInfo info : sBgItemsIdMap) {
2662 if (info instanceof ShortcutInfo && user.equals(info.user)
2663 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2664 ShortcutInfo si = (ShortcutInfo) info;
2665 ComponentName cn = si.getTargetComponent();
2666 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2667 si.updateIcon(mIconCache);
2668 updatedShortcuts.add(si);
2669 }
2670 }
2671 }
2672 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2673 }
2674
2675 if (!updatedShortcuts.isEmpty()) {
2676 final UserHandleCompat userFinal = user;
2677 mHandler.post(new Runnable() {
2678
2679 public void run() {
2680 Callbacks cb = getCallback();
2681 if (cb != null && callbacks == cb) {
2682 cb.bindShortcutsChanged(updatedShortcuts,
2683 new ArrayList<ShortcutInfo>(), userFinal);
2684 }
2685 }
2686 });
2687 }
2688
2689 if (!updatedApps.isEmpty()) {
2690 mHandler.post(new Runnable() {
2691
2692 public void run() {
2693 Callbacks cb = getCallback();
2694 if (cb != null && callbacks == cb) {
2695 cb.bindAppsUpdated(updatedApps);
2696 }
2697 }
2698 });
2699 }
2700 }
2701
Joe Onorato36115782010-06-17 13:28:48 -04002702 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002703 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002704 }
2705
Adam Cohen091440a2015-03-18 14:16:05 -07002706 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002707
2708 @Override
2709 public void onReceive(Context context, Intent intent) {
2710 synchronized (sBgLock) {
2711 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2712 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002713 final PackageManager manager = context.getPackageManager();
2714 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2715 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002716 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2717 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002718 packagesRemoved.clear();
2719 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002720 for (String pkg : entry.getValue()) {
2721 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002722 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002723 packagesUnavailable.add(pkg);
2724 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002725 packagesRemoved.add(pkg);
2726 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002727 }
2728 }
2729 if (!packagesRemoved.isEmpty()) {
2730 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2731 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2732 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002733 if (!packagesUnavailable.isEmpty()) {
2734 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2735 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2736 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002737 }
Sunny Goyal34942622014-08-29 17:20:55 -07002738 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002739 }
2740 }
2741 }
2742
Joe Onorato36115782010-06-17 13:28:48 -04002743 private class PackageUpdatedTask implements Runnable {
2744 int mOp;
2745 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002746 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002747
2748 public static final int OP_NONE = 0;
2749 public static final int OP_ADD = 1;
2750 public static final int OP_UPDATE = 2;
2751 public static final int OP_REMOVE = 3; // uninstlled
2752 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002753 public static final int OP_SUSPEND = 5; // package suspended
2754 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002755 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002756
Kenny Guyed131872014-04-30 03:02:21 +01002757 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002758 mOp = op;
2759 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002760 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002761 }
2762
2763 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002764 if (!mHasLoaderCompletedOnce) {
2765 // Loader has not yet run.
2766 return;
2767 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002768 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002769
2770 final String[] packages = mPackages;
2771 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002772 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002773 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002774 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002775 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002776 for (int i=0; i<N; i++) {
2777 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002778 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002779 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002780 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002781
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002782 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2783 if (heuristic != null) {
2784 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002785 }
Joe Onorato36115782010-06-17 13:28:48 -04002786 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002787 }
Joe Onorato36115782010-06-17 13:28:48 -04002788 case OP_UPDATE:
2789 for (int i=0; i<N; i++) {
2790 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002791 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002792 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002793 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002794 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002795 // Since package was just updated, the target must be available now.
2796 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002797 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002798 case OP_REMOVE: {
2799 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2800 if (heuristic != null) {
2801 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002802 }
Joe Onorato36115782010-06-17 13:28:48 -04002803 for (int i=0; i<N; i++) {
2804 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002805 mIconCache.removeIconsForPkg(packages[i], mUser);
2806 }
2807 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002808 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002809 case OP_UNAVAILABLE:
2810 for (int i=0; i<N; i++) {
2811 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2812 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002813 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002814 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002815 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002816 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002817 case OP_SUSPEND:
2818 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002819 flagOp = mOp == OP_SUSPEND ?
2820 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2821 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002822 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002823 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2824 break;
2825 case OP_USER_AVAILABILITY_CHANGE:
2826 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2827 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2828 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2829 // We want to update all packages for this user.
2830 pkgFilter = StringFilter.matchesAll();
2831 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002832 break;
Joe Onorato36115782010-06-17 13:28:48 -04002833 }
2834
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002835 ArrayList<AppInfo> added = null;
2836 ArrayList<AppInfo> modified = null;
2837 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002838
Adam Cohen487f7dd2012-06-28 18:12:10 -07002839 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002840 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002841 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002842 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002843 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002844 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002845 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002846 }
Winson Chung5d55f332012-07-16 20:45:03 -07002847 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002848 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002849 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002850 }
2851
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002852 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002853
Joe Onorato36115782010-06-17 13:28:48 -04002854 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002855 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002856 for (AppInfo ai : added) {
2857 addedOrUpdatedApps.put(ai.componentName, ai);
2858 }
Joe Onorato36115782010-06-17 13:28:48 -04002859 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002860
Joe Onorato36115782010-06-17 13:28:48 -04002861 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002862 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002863 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002864 for (AppInfo ai : modified) {
2865 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002866 }
2867
Joe Onorato36115782010-06-17 13:28:48 -04002868 mHandler.post(new Runnable() {
2869 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002870 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002871 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002872 callbacks.bindAppsUpdated(modifiedFinal);
2873 }
2874 }
2875 });
2876 }
Winson Chung83892cc2013-05-01 16:53:33 -07002877
Sunny Goyal4390ace2014-10-13 11:33:11 -07002878 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002879 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002880 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2881 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2882 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2883
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002884 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002885 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002886 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2887 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002888 boolean infoUpdated = false;
2889 boolean shortcutUpdated = false;
2890
2891 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002892 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002893 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002894 Bitmap icon = Utilities.createIconBitmap(
2895 si.iconResource.packageName,
2896 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002897 if (icon != null) {
2898 si.setIcon(icon);
2899 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002900 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002901 }
2902 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002903
2904 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002905 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002906 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2907
2908 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002909 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2910 // Auto install icon
2911 PackageManager pm = context.getPackageManager();
2912 ResolveInfo matched = pm.resolveActivity(
2913 new Intent(Intent.ACTION_MAIN)
2914 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2915 PackageManager.MATCH_DEFAULT_ONLY);
2916 if (matched == null) {
2917 // Try to find the best match activity.
2918 Intent intent = pm.getLaunchIntentForPackage(
2919 cn.getPackageName());
2920 if (intent != null) {
2921 cn = intent.getComponent();
2922 appInfo = addedOrUpdatedApps.get(cn);
2923 }
2924
2925 if ((intent == null) || (appInfo == null)) {
2926 removedShortcuts.add(si);
2927 continue;
2928 }
2929 si.promisedIntent = intent;
2930 }
2931 }
2932
2933 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002934 if (appInfo != null) {
2935 si.flags = appInfo.flags;
2936 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002937
Sunny Goyal756adbc2015-04-16 15:20:51 -07002938 si.intent = si.promisedIntent;
2939 si.promisedIntent = null;
2940 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002941 infoUpdated = true;
2942 si.updateIcon(mIconCache);
2943 }
2944
2945 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2946 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2947 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002948 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002949 si.contentDescription = appInfo.contentDescription;
2950 infoUpdated = true;
2951 }
2952
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002953 int oldDisabledFlags = si.isDisabled;
2954 si.isDisabled = flagOp.apply(si.isDisabled);
2955 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002956 shortcutUpdated = true;
2957 }
2958 }
2959
2960 if (infoUpdated || shortcutUpdated) {
2961 updatedShortcuts.add(si);
2962 }
2963 if (infoUpdated) {
2964 updateItemInDatabase(context, si);
2965 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002966 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002967 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2968 if (mUser.equals(widgetInfo.user)
2969 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07002970 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002971 widgetInfo.restoreStatus &=
2972 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2973 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002974
2975 // adding this flag ensures that launcher shows 'click to setup'
2976 // if the widget has a config activity. In case there is no config
2977 // activity, it will be marked as 'restored' during bind.
2978 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2979
Sunny Goyal4390ace2014-10-13 11:33:11 -07002980 widgets.add(widgetInfo);
2981 updateItemInDatabase(context, widgetInfo);
2982 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002983 }
2984 }
2985 }
2986
Sunny Goyal4390ace2014-10-13 11:33:11 -07002987 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002988 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002989 mHandler.post(new Runnable() {
2990
2991 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002992 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002993 if (callbacks == cb && cb != null) {
2994 callbacks.bindShortcutsChanged(
2995 updatedShortcuts, removedShortcuts, mUser);
2996 }
2997 }
2998 });
2999 if (!removedShortcuts.isEmpty()) {
3000 deleteItemsFromDatabase(context, removedShortcuts);
3001 }
3002 }
3003 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003004 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003005 mHandler.post(new Runnable() {
3006 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003007 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003008 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003009 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003010 }
3011 }
3012 });
3013 }
3014 }
3015
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016 final HashSet<String> removedPackages = new HashSet<>();
3017 final HashSet<ComponentName> removedComponents = new HashSet<>();
3018 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003019 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 Collections.addAll(removedPackages, packages);
3021
3022 // No need to update the removedComponents as
3023 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003024 } else if (mOp == OP_UPDATE) {
3025 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003026 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003027 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003028 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003029 }
3030 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003031
3032 // Update removedComponents as some components can get removed during package update
3033 for (AppInfo info : removedApps) {
3034 removedComponents.add(info.componentName);
3035 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003036 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003037
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003038 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3039 for (String pn : removedPackages) {
3040 deletePackageFromDatabase(context, pn, mUser);
3041 }
3042 for (ComponentName cn : removedComponents) {
3043 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003044 }
3045
Winson Chungdf95eb12013-10-16 14:57:07 -07003046 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003047 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3048
Winson Chungdf95eb12013-10-16 14:57:07 -07003049 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003050 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003051 mHandler.post(new Runnable() {
3052 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003053 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003054 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003055 callbacks.bindWorkspaceComponentsRemoved(
3056 removedPackages, removedComponents, mUser);
3057 }
3058 }
3059 });
3060 }
3061
3062 if (!removedApps.isEmpty()) {
3063 // Remove corresponding apps from All-Apps
3064 final Callbacks callbacks = getCallback();
3065 mHandler.post(new Runnable() {
3066 public void run() {
3067 Callbacks cb = getCallback();
3068 if (callbacks == cb && cb != null) {
3069 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003070 }
3071 }
3072 });
Joe Onoratobe386092009-11-17 17:32:16 -08003073 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003074
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003075 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3076 // get widget update signals.
3077 if (!Utilities.ATLEAST_MARSHMALLOW &&
3078 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003079 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003080 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003081 public void run() {
3082 Callbacks cb = getCallback();
3083 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003084 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003085 }
3086 }
3087 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003088 }
3089 }
3090 }
3091
3092 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3093 mHandler.post(new Runnable() {
3094 @Override
3095 public void run() {
3096 Callbacks cb = getCallback();
3097 if (callbacks == cb && cb != null) {
3098 callbacks.bindWidgetsModel(model);
3099 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003100 }
3101 });
3102 }
3103
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003104 public void refreshAndBindWidgetsAndShortcuts(
3105 final Callbacks callbacks, final boolean bindFirst) {
3106 runOnWorkerThread(new Runnable() {
3107 @Override
3108 public void run() {
3109 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3110 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003111 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003112 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3113 bindWidgetsModel(callbacks, model);
3114 // update the Widget entries inside DB on the worker thread.
3115 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3116 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003117 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003118 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003119 }
3120
Adam Cohen091440a2015-03-18 14:16:05 -07003121 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003122 UserHandleCompat user) {
3123 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3124 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003125 }
Adam Cohen556f6132014-01-15 15:18:08 -08003126
Kenny Guyed131872014-04-30 03:02:21 +01003127 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3128 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003129 if (cn == null) {
3130 return false;
3131 }
Kenny Guyed131872014-04-30 03:02:21 +01003132 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3133 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003134 return false;
3135 }
Kenny Guyed131872014-04-30 03:02:21 +01003136 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003137 }
3138
Adam Cohena28b78e2014-05-20 17:03:04 -07003139 public static boolean isValidPackage(Context context, String packageName,
3140 UserHandleCompat user) {
3141 if (packageName == null) {
3142 return false;
3143 }
3144 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3145 return launcherApps.isPackageEnabledForProfile(packageName, user);
3146 }
3147
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003149 * Make an ShortcutInfo object for a restored application or shortcut item that points
3150 * to a package that is not yet installed on the system.
3151 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003152 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003153 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003154 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003155 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003156
3157 Bitmap icon = iconInfo.loadIcon(c, info, context);
3158 // the fallback icon
3159 if (icon == null) {
3160 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3161 } else {
3162 info.setIcon(icon);
3163 }
Sunny Goyal34942622014-08-29 17:20:55 -07003164
3165 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003166 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003167 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003168 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003169 }
Sunny Goyal34942622014-08-29 17:20:55 -07003170 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3171 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003172 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003173 }
Sunny Goyal34942622014-08-29 17:20:55 -07003174 } else {
3175 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3176 }
3177
Winson Chung82b016c2015-05-08 17:00:10 -07003178 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003179 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003180 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003181 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003182 return info;
3183 }
3184
3185 /**
3186 * Make an Intent object for a restored application or shortcut item that points
3187 * to the market page for the item.
3188 */
Adam Cohen091440a2015-03-18 14:16:05 -07003189 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003190 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003191 return getMarketIntent(componentName.getPackageName());
3192 }
3193
3194 static Intent getMarketIntent(String packageName) {
3195 return new Intent(Intent.ACTION_VIEW)
3196 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003197 .scheme("market")
3198 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003199 .appendQueryParameter("id", packageName)
3200 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003201 }
3202
3203 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003204 * Make an ShortcutInfo object for a shortcut that is an application.
3205 *
3206 * If c is not null, then it will be used to fill in missing data like the title and icon.
3207 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003208 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003209 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003210 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003211 if (user == null) {
3212 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003213 return null;
3214 }
3215
Kenny Guyed131872014-04-30 03:02:21 +01003216 ComponentName componentName = intent.getComponent();
3217 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003218 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003219 return null;
3220 }
3221
3222 Intent newIntent = new Intent(intent.getAction(), null);
3223 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3224 newIntent.setComponent(componentName);
3225 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003226 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003227 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3228 return null;
3229 }
3230
3231 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003232 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003233 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3234 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3235 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003236 }
3237
Sunny Goyald09c3702016-04-06 16:18:20 -07003238 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3239 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3240 }
3241
Joe Onorato56d82912010-03-07 14:32:10 -05003242 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003243 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003244 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003245 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003246
Joe Onorato56d82912010-03-07 14:32:10 -05003247 // fall back to the class name of the activity
3248 if (info.title == null) {
3249 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003250 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003251
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003253 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003254 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003255 if (lai != null) {
3256 info.flags = AppInfo.initFlags(lai);
3257 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003260
Sunny Goyale2df0622015-04-24 11:27:00 -07003261 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003262 ItemInfoFilter f) {
3263 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3264 for (ItemInfo i : infos) {
3265 if (i instanceof ShortcutInfo) {
3266 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003267 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003268 if (cn != null && f.filterItem(null, info, cn)) {
3269 filtered.add(info);
3270 }
3271 } else if (i instanceof FolderInfo) {
3272 FolderInfo info = (FolderInfo) i;
3273 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003274 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003275 if (cn != null && f.filterItem(info, s, cn)) {
3276 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003277 }
3278 }
Winson Chung64359a52013-07-08 17:17:08 -07003279 } else if (i instanceof LauncherAppWidgetInfo) {
3280 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3281 ComponentName cn = info.providerName;
3282 if (cn != null && f.filterItem(null, info, cn)) {
3283 filtered.add(info);
3284 }
Winson Chung8a435102012-08-30 17:16:53 -07003285 }
3286 }
Winson Chung64359a52013-07-08 17:17:08 -07003287 return new ArrayList<ItemInfo>(filtered);
3288 }
3289
Adam Cohen091440a2015-03-18 14:16:05 -07003290 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003291 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003292 ItemInfoFilter filter = new ItemInfoFilter() {
3293 @Override
3294 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003295 if (info.user == null) {
3296 return cn.equals(cname);
3297 } else {
3298 return cn.equals(cname) && info.user.equals(user);
3299 }
Winson Chung64359a52013-07-08 17:17:08 -07003300 }
3301 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003302 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003303 }
3304
Sunny Goyal1a745e82014-10-02 15:58:31 -07003305 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003306 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307 */
Adam Cohen091440a2015-03-18 14:16:05 -07003308 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003309 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003310 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003311 // Non-app shortcuts are only supported for current user.
3312 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003315 // TODO: If there's an explicit component and we can't install that, delete it.
3316
Winson Chung82b016c2015-05-08 17:00:10 -07003317 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003318
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003319 Bitmap icon = iconInfo.loadIcon(c, info, context);
3320 // the fallback icon
3321 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003322 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003323 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003325 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326 return info;
3327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328
Sunny Goyal2350bc92014-10-14 16:42:54 -07003329 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003330 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3331 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3332 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3333
Adam Cohend9198822011-11-22 16:42:47 -08003334 if (intent == null) {
3335 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3336 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3337 return null;
3338 }
3339
Joe Onorato0589f0f2010-02-08 13:44:00 -08003340 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003341 boolean customIcon = false;
3342 ShortcutIconResource iconResource = null;
3343
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003344 if (bitmap instanceof Bitmap) {
3345 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003346 customIcon = true;
3347 } else {
3348 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003349 if (extra instanceof ShortcutIconResource) {
3350 iconResource = (ShortcutIconResource) extra;
3351 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003352 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003353 }
3354 }
3355
Michael Jurkac9d95c52011-08-29 14:03:34 -07003356 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003357
Kenny Guyed131872014-04-30 03:02:21 +01003358 // Only support intents for current user for now. Intents sent from other
3359 // users wouldn't get here without intent forwarding anyway.
3360 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003361 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003362 icon = mIconCache.getDefaultIcon(info.user);
3363 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003364 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003365 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003366
Winson Chung82b016c2015-05-08 17:00:10 -07003367 info.title = Utilities.trim(name);
3368 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003369 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003370 info.iconResource = iconResource;
3371
3372 return info;
3373 }
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003376 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 * or make a new one.
3378 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003379 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 // See if a placeholder was created for us already
3381 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003382 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003384 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 folders.put(id, folderInfo);
3386 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003387 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003388 }
3389
Joe Onoratobe386092009-11-17 17:32:16 -08003390
Sunny Goyal651077b2014-06-30 14:15:31 -07003391 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3392 return (provider != null) && (provider.provider != null)
3393 && (provider.provider.getPackageName() != null);
3394 }
3395
Joe Onoratobe386092009-11-17 17:32:16 -08003396 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003397 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003398 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3399 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3400 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3401 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003402 if (mLoaderTask != null) {
3403 mLoaderTask.dumpState();
3404 } else {
3405 Log.d(TAG, "mLoaderTask=null");
3406 }
Joe Onoratobe386092009-11-17 17:32:16 -08003407 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003408
3409 public Callbacks getCallback() {
3410 return mCallbacks != null ? mCallbacks.get() : null;
3411 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003412
3413 /**
3414 * @return {@link FolderInfo} if its already loaded.
3415 */
3416 public FolderInfo findFolderById(Long folderId) {
3417 synchronized (sBgLock) {
3418 return sBgFolders.get(folderId);
3419 }
3420 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003421
Sunny Goyal527c7d32015-08-28 15:19:36 -07003422 @Thunk class DeferredMainThreadExecutor implements Executor {
3423
3424 @Override
3425 public void execute(Runnable command) {
3426 runOnMainThread(command);
3427 }
3428 }
3429
Sunny Goyal756adbc2015-04-16 15:20:51 -07003430 /**
3431 * @return the looper for the worker thread which can be used to start background tasks.
3432 */
3433 public static Looper getWorkerLooper() {
3434 return sWorkerThread.getLooper();
3435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436}