blob: 0b67310fa999cf8f6ca55bfa5d222306d4199721 [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;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070036import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070058import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000059import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070060import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070061import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070062import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070063import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080064
Michael Jurkac2f801e2011-07-12 14:19:46 -070065import java.lang.ref.WeakReference;
66import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070067import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070069import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070070import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.Collections;
72import java.util.Comparator;
73import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070074import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070075import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070077import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080/**
81 * Maintains in-memory state of the Launcher. It is expected that there should be only one
82 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070083 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 */
Kenny Guyed131872014-04-30 03:02:21 +010085public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010086 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040088 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070089 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070092
Dan Sandlerd5024042014-01-09 15:01:33 -050093 public static final int LOADER_FLAG_NONE = 0;
94 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
95 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
96
Joe Onorato36115782010-06-17 13:28:48 -040097 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050098 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080099
Adam Cohen091440a2015-03-18 14:16:05 -0700100 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800101 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final LauncherAppState mApp;
104 @Thunk final Object mLock = new Object();
105 @Thunk DeferredHandler mHandler = new DeferredHandler();
106 @Thunk LoaderTask mLoaderTask;
107 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700108 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Jason Monkbbe1e242014-05-16 17:37:34 -0400110 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700113 static {
114 sWorkerThread.start();
115 }
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 // We start off with everything not loaded. After that, we assume that
119 // our monitoring of the package manager provides all updates and we never
120 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk boolean mWorkspaceLoaded;
122 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // When we are loading pages synchronously, we can't just post the binding of items on the side
125 // pages as this delays the rotation process. Instead, we wait for a callback from the first
126 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
127 // a normal load, we also clear this set of Runnables.
128 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
129
Sunny Goyal756a28a2015-04-23 17:07:55 -0700130 /**
131 * Set of runnables to be called on the background thread after the workspace binding
132 * is complete.
133 */
134 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
135
Adam Cohen091440a2015-03-18 14:16:05 -0700136 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700139 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700140 // Entire list of widgets.
141 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700151 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700163 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 // sBgWorkspaceScreens is the ordered set of workspace screens.
166 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
167
Adam Cohen59400422014-03-05 18:07:04 -0800168 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000169 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk final LauncherAppsCompat mLauncherApps;
180 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
185 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
187 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700188 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700190 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700199 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
200 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
201 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700202 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700203 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700204 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700205 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100206 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700207 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700209 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Winson Chungee055712013-07-30 14:46:24 -0700219 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700220 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400221 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
222 // resource string.
223 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
224 ProviderInfo providerInfo =
225 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
226 ProviderInfo redirectProvider =
227 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700228
229 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400230 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 if (mOldContentProviderExists) {
233 Log.d(TAG, "Old launcher provider exists.");
234 } else {
235 Log.d(TAG, "Old launcher provider does not exist.");
236 }
237
Daniel Sandlere4f98912013-06-25 15:13:26 -0400238 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100239 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700240 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Kenny Guyed131872014-04-30 03:02:21 +0100243 mLauncherApps = LauncherAppsCompat.getInstance(context);
244 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
248 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 // If we are on the worker thread, post onto the main handler
252 mHandler.post(r);
253 } else {
254 r.run();
255 }
256 }
257
258 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
259 * posted on the worker thread handler. */
260 private static void runOnWorkerThread(Runnable r) {
261 if (sWorkerThread.getThreadId() == Process.myTid()) {
262 r.run();
263 } else {
264 // If we are not on the worker thread, then post to the worker handler
265 sWorker.post(r);
266 }
267 }
268
Sunny Goyal756a28a2015-04-23 17:07:55 -0700269 /**
270 * Runs the specified runnable after the loader is complete
271 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700272 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700273 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
274 synchronized (mBindCompleteRunnables) {
275 mBindCompleteRunnables.add(r);
276 }
277 } else {
278 runOnWorkerThread(r);
279 }
280 }
281
Winson Chunge43a1e72014-01-15 10:33:02 -0800282 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
283 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800284 }
285
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 public void setPackageState(final PackageInstallInfo installInfo) {
287 Runnable updateRunnable = new Runnable() {
288
289 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 synchronized (sBgLock) {
292 final HashSet<ItemInfo> updates = new HashSet<>();
293
294 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
295 // Ignore install success events as they are handled by Package add events.
296 return;
297 }
298
Sunny Goyale2df0622015-04-24 11:27:00 -0700299 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700300 if (info instanceof ShortcutInfo) {
301 ShortcutInfo si = (ShortcutInfo) info;
302 ComponentName cn = si.getTargetComponent();
303 if (si.isPromise() && (cn != null)
304 && installInfo.packageName.equals(cn.getPackageName())) {
305 si.setInstallProgress(installInfo.progress);
306
307 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
308 // Mark this info as broken.
309 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
310 }
311 updates.add(si);
312 }
313 }
314 }
315
316 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
317 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
318 widget.installProgress = installInfo.progress;
319 updates.add(widget);
320 }
321 }
322
323 if (!updates.isEmpty()) {
324 // Push changes to the callback.
325 Runnable r = new Runnable() {
326 public void run() {
327 Callbacks callbacks = getCallback();
328 if (callbacks != null) {
329 callbacks.bindRestoreItemsChange(updates);
330 }
331 }
332 };
333 mHandler.post(r);
334 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500335 }
336 }
337 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500339 }
340
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 /**
342 * Updates the icons and label of all pending icons for the provided package name.
343 */
344 public void updateSessionDisplayInfo(final String packageName) {
345 Runnable updateRunnable = new Runnable() {
346
347 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700348 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700349 synchronized (sBgLock) {
350 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
351 final UserHandleCompat user = UserHandleCompat.myUserHandle();
352
Sunny Goyale2df0622015-04-24 11:27:00 -0700353 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700354 if (info instanceof ShortcutInfo) {
355 ShortcutInfo si = (ShortcutInfo) info;
356 ComponentName cn = si.getTargetComponent();
357 if (si.isPromise() && (cn != null)
358 && packageName.equals(cn.getPackageName())) {
359 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
360 // For auto install apps update the icon as well as label.
361 mIconCache.getTitleAndIcon(si,
362 si.promisedIntent, user,
363 si.shouldUseLowResIcon());
364 } else {
365 // Only update the icon for restored apps.
366 si.updateIcon(mIconCache);
367 }
368 updates.add(si);
369 }
370 }
371 }
372
373 if (!updates.isEmpty()) {
374 // Push changes to the callback.
375 Runnable r = new Runnable() {
376 public void run() {
377 Callbacks callbacks = getCallback();
378 if (callbacks != null) {
379 callbacks.bindShortcutsChanged(updates,
380 new ArrayList<ShortcutInfo>(), user);
381 }
382 }
383 };
384 mHandler.post(r);
385 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700386 }
387 }
388 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700389 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700390 }
391
Adam Cohen76a47a12014-02-05 11:47:43 -0800392 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800393 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800394
395 if (allAppsApps == null) {
396 throw new RuntimeException("allAppsApps must not be null");
397 }
398 if (allAppsApps.isEmpty()) {
399 return;
400 }
401
402 // Process the newly added applications and add them to the database first
403 Runnable r = new Runnable() {
404 public void run() {
405 runOnMainThread(new Runnable() {
406 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800407 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800408 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500409 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 }
411 }
412 });
413 }
414 };
415 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700416 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800417
Sunny Goyala9116722015-04-29 13:55:58 -0700418 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 int[] xy, int spanX, int spanY) {
420 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700421 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
422 final int xCount = (int) profile.numColumns;
423 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 boolean[][] occupied = new boolean[xCount][yCount];
425 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700426 for (ItemInfo r : occupiedPos) {
427 int right = r.cellX + r.spanX;
428 int bottom = r.cellY + r.spanY;
429 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
430 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 occupied[x][y] = true;
432 }
433 }
434 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800435 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700436 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 }
438
439 /**
440 * Find a position on the screen for the given size or adds a new screen.
441 * @return screenId and the coordinates for the item.
442 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700443 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 ArrayList<Long> workspaceScreens,
446 ArrayList<Long> addedWorkspaceScreensFinal,
447 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700448 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449
Sunny Goyala9116722015-04-29 13:55:58 -0700450 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700451 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700452 synchronized (sBgLock) {
453 for (ItemInfo info : sBgItemsIdMap) {
454 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
455 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
456 if (items == null) {
457 items = new ArrayList<>();
458 screenItems.put(info.screenId, items);
459 }
460 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
464
465 // Find appropriate space for the item.
466 long screenId = 0;
467 int[] cordinates = new int[2];
468 boolean found = false;
469
470 int screenCount = workspaceScreens.size();
471 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700472 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800473 if (preferredScreenIndex < screenCount) {
474 screenId = workspaceScreens.get(preferredScreenIndex);
475 found = findNextAvailableIconSpaceInScreen(
476 screenItems.get(screenId), cordinates, spanX, spanY);
477 }
478
479 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700480 // Search on any of the screens starting from the first screen.
481 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 screenId = workspaceScreens.get(screen);
483 if (findNextAvailableIconSpaceInScreen(
484 screenItems.get(screenId), cordinates, spanX, spanY)) {
485 // We found a space for it
486 found = true;
487 break;
488 }
489 }
490 }
491
492 if (!found) {
493 // Still no position found. Add a new screen to the end.
494 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
495
496 // Save the screen id for binding in the workspace
497 workspaceScreens.add(screenId);
498 addedWorkspaceScreensFinal.add(screenId);
499
500 // If we still can't find an empty space, then God help us all!!!
501 if (!findNextAvailableIconSpaceInScreen(
502 screenItems.get(screenId), cordinates, spanX, spanY)) {
503 throw new RuntimeException("Can't find space to add the item");
504 }
505 }
506 return Pair.create(screenId, cordinates);
507 }
508
509 /**
510 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700513 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800515 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700516 return;
Winson Chung997a9232013-07-24 15:33:46 -0700517 }
Winson Chung64359a52013-07-08 17:17:08 -0700518 // Process the newly added applications and add them to the database first
519 Runnable r = new Runnable() {
520 public void run() {
521 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
522 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
523
Winson Chung76828c82013-08-19 15:43:29 -0700524 // Get the list of workspace screens. We need to append to this list and
525 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
526 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800527 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700528 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700530 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700532 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700534 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 }
Winson Chung76828c82013-08-19 15:43:29 -0700536
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700538 Pair<Long, int[]> coords = findSpaceForItem(context,
539 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 1, 1);
541 long screenId = coords.first;
542 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700543
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 ItemInfo itemInfo;
545 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
546 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800547 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700549 } else {
550 throw new RuntimeException("Unexpected info type");
551 }
Winson Chung94d67682013-09-25 16:29:40 -0700552
Winson Chung64359a52013-07-08 17:17:08 -0700553 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700554 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700555 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700556 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700557 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700558 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700559 }
560 }
561
Winson Chung76828c82013-08-19 15:43:29 -0700562 // Update the workspace screens
563 updateWorkspaceScreenOrder(context, workspaceScreens);
564
Adam Cohen76a47a12014-02-05 11:47:43 -0800565 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700566 runOnMainThread(new Runnable() {
567 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800568 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700569 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700570 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
571 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700572 if (!addedShortcutsFinal.isEmpty()) {
573 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
574 long lastScreenId = info.screenId;
575 for (ItemInfo i : addedShortcutsFinal) {
576 if (i.screenId == lastScreenId) {
577 addAnimated.add(i);
578 } else {
579 addNotAnimated.add(i);
580 }
Winson Chung997a9232013-07-24 15:33:46 -0700581 }
582 }
Winson Chungd64d1762013-08-20 14:37:16 -0700583 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800584 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700585 }
Winson Chung64359a52013-07-08 17:17:08 -0700586 }
Winson Chung997a9232013-07-24 15:33:46 -0700587 });
588 }
Winson Chung64359a52013-07-08 17:17:08 -0700589 }
590 };
591 runOnWorkerThread(r);
592 }
593
Sunny Goyald33860f2015-04-23 16:02:20 -0700594 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700595 if (sWorkerThread.getThreadId() == Process.myTid()) {
596 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
597 "main thread");
598 }
599
600 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400601 synchronized (mDeferredBindRunnables) {
602 mDeferredBindRunnables.clear();
603 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700604
605 // Remove any queued UI runnables
606 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700607 // Unbind all the workspace items
608 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700609 }
610
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700612 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700613 // Ensure that we don't use the same workspace items data structure on the main thread
614 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700615 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700616 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700617 tmpItems.addAll(sBgWorkspaceItems);
618 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700619 }
620 Runnable r = new Runnable() {
621 @Override
622 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700623 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 item.unbind();
625 }
626 }
627 };
628 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700629 }
630
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 /**
632 * Adds an item to the DB if it was not created previously, or move it to a new
633 * <container, screen, cellX, cellY>
634 */
635 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 if (item.container == ItemInfo.NO_ID) {
638 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700639 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 } else {
641 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644 }
645
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 static void checkItemInfoLocked(
647 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
648 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
649 if (modelItem != null && item != modelItem) {
650 // check all the data is consistent
651 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
652 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
653 ShortcutInfo shortcut = (ShortcutInfo) item;
654 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
655 modelShortcut.intent.filterEquals(shortcut.intent) &&
656 modelShortcut.id == shortcut.id &&
657 modelShortcut.itemType == shortcut.itemType &&
658 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700659 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700660 modelShortcut.cellX == shortcut.cellX &&
661 modelShortcut.cellY == shortcut.cellY &&
662 modelShortcut.spanX == shortcut.spanX &&
663 modelShortcut.spanY == shortcut.spanY &&
664 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
665 (modelShortcut.dropPos != null &&
666 shortcut.dropPos != null &&
667 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
668 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
669 // For all intents and purposes, this is the same object
670 return;
671 }
672 }
673
674 // the modelItem needs to match up perfectly with item if our model is
675 // to be consistent with the database-- for now, just require
676 // modelItem == item or the equality check above
677 String msg = "item: " + ((item != null) ? item.toString() : "null") +
678 "modelItem: " +
679 ((modelItem != null) ? modelItem.toString() : "null") +
680 "Error: ItemInfo passed to checkItemInfo doesn't match original";
681 RuntimeException e = new RuntimeException(msg);
682 if (stackTrace != null) {
683 e.setStackTrace(stackTrace);
684 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800685 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700686 }
687 }
688
Michael Jurka816474f2012-06-25 14:49:02 -0700689 static void checkItemInfo(final ItemInfo item) {
690 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
691 final long itemId = item.id;
692 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700693 public void run() {
694 synchronized (sBgLock) {
695 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700696 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700697 }
698 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700699 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700700 }
701
Michael Jurkac9d95c52011-08-29 14:03:34 -0700702 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
703 final ItemInfo item, final String callingFunction) {
704 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700705 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706 final ContentResolver cr = context.getContentResolver();
707
Adam Cohen487f7dd2012-06-28 18:12:10 -0700708 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709 Runnable r = new Runnable() {
710 public void run() {
711 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700712 updateItemArrays(item, itemId, stackTrace);
713 }
714 };
715 runOnWorkerThread(r);
716 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700717
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
719 final ArrayList<ItemInfo> items, final String callingFunction) {
720 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
723 Runnable r = new Runnable() {
724 public void run() {
725 ArrayList<ContentProviderOperation> ops =
726 new ArrayList<ContentProviderOperation>();
727 int count = items.size();
728 for (int i = 0; i < count; i++) {
729 ItemInfo item = items.get(i);
730 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700731 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700733
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
735 updateItemArrays(item, itemId, stackTrace);
736
737 }
738 try {
739 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
740 } catch (Exception e) {
741 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700742 }
743 }
744 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700745 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700746 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700747
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
749 // Lock on mBgLock *after* the db operation
750 synchronized (sBgLock) {
751 checkItemInfoLocked(itemId, item, stackTrace);
752
753 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
754 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
755 // Item is in a folder, make sure this folder exists
756 if (!sBgFolders.containsKey(item.container)) {
757 // An items container is being set to a that of an item which is not in
758 // the list of Folders.
759 String msg = "item: " + item + " container being set to: " +
760 item.container + ", not in the list of folders";
761 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 }
763 }
764
765 // Items are added/removed from the corresponding FolderInfo elsewhere, such
766 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
767 // that are on the desktop, as appropriate
768 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800769 if (modelItem != null &&
770 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
771 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700772 switch (modelItem.itemType) {
773 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
774 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
775 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
776 if (!sBgWorkspaceItems.contains(modelItem)) {
777 sBgWorkspaceItems.add(modelItem);
778 }
779 break;
780 default:
781 break;
782 }
783 } else {
784 sBgWorkspaceItems.remove(modelItem);
785 }
786 }
787 }
788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700791 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700792 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 item.cellX = cellX;
796 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 // We store hotseat items in canonical form which is this orientation invariant position
799 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700802 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700804 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806
807 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
810 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800811 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700815 }
816
817 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
819 * cellX, cellY have already been updated on the ItemInfos.
820 */
821 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
822 final long container, final int screen) {
823
824 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
825 int count = items.size();
826
827 for (int i = 0; i < count; i++) {
828 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700829 item.container = container;
830
831 // We store hotseat items in canonical form which is this orientation invariant position
832 // in the hotseat
833 if (context instanceof Launcher && screen < 0 &&
834 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 item.cellY);
837 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 }
840
841 final ContentValues values = new ContentValues();
842 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
843 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
844 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800845 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700847
848 contentValues.add(values);
849 }
850 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
851 }
852
853 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700854 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800855 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700858 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800859 item.cellX = cellX;
860 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 item.spanX = spanX;
862 item.spanY = spanY;
863
864 // We store hotseat items in canonical form which is this orientation invariant position
865 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700869 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 }
Adam Cohend4844c32011-02-18 19:25:06 -0800872
Adam Cohend4844c32011-02-18 19:25:06 -0800873 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800874 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
876 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800877 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
879 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700880 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800881
Michael Jurka816474f2012-06-25 14:49:02 -0700882 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700883 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700884
885 /**
886 * Update an item to the database in a specified container.
887 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700888 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700889 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100890 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700891 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800892 }
893
Sunny Goyal756a28a2015-04-23 17:07:55 -0700894 private void assertWorkspaceLoaded() {
895 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
896 throw new RuntimeException("Trying to add shortcut while loader is running");
897 }
898 }
899
Adam Cohend4844c32011-02-18 19:25:06 -0800900 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700901 * Returns true if the shortcuts already exists on the workspace. This must be called after
902 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700904 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
905 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700906 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 if (intent.getComponent() != null) {
908 // If component is not null, an intent with null package will produce
909 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700910 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 intentWithPkg = intent.toUri(0);
913 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700915 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
916 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700917 }
918 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700919 intentWithPkg = intent.toUri(0);
920 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700921 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700922
923 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700924 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700925 if (item instanceof ShortcutInfo) {
926 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700927 Intent targetIntent = info.promisedIntent == null
928 ? info.intent : info.promisedIntent;
929 if (targetIntent != null && info.user.equals(user)) {
930 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700931 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
932 return true;
933 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700934 }
935 }
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700938 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700939 }
940
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 /**
942 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
943 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700944 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 final ContentResolver cr = context.getContentResolver();
946 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
947 "_id=? and (itemType=? or itemType=?)",
948 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700949 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700950
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 try {
952 if (c.moveToFirst()) {
953 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
954 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
955 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
956 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
957 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
958 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700959 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 FolderInfo folderInfo = null;
962 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700963 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
964 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700966 }
967
Sunny Goyala508e4f2015-05-21 09:33:57 -0700968 // Do not trim the folder label, as is was set by the user.
969 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 folderInfo.id = id;
971 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700972 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700973 folderInfo.cellX = c.getInt(cellXIndex);
974 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700975 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976
977 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 } finally {
980 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700981 }
982
983 return null;
984 }
985
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 /**
987 * Add an item to the database in a specified container. Sets the container, screen, cellX and
988 * cellY fields of the item. Also assigns an ID to the item.
989 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700990 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700991 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 item.cellX = cellX;
994 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 // We store hotseat items in canonical form which is this orientation invariant position
996 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003
1004 final ContentValues values = new ContentValues();
1005 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001006 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007
Michael Jurka414300a2013-08-27 15:42:35 +02001008 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001010
Jason Monk8e19cf22014-03-20 15:06:57 -04001011 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001012 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001014 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001017 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001018 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 sBgItemsIdMap.put(item.id, item);
1020 switch (item.itemType) {
1021 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1022 sBgFolders.put(item.id, (FolderInfo) item);
1023 // Fall through
1024 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1025 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1026 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1027 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1028 sBgWorkspaceItems.add(item);
1029 } else {
1030 if (!sBgFolders.containsKey(item.container)) {
1031 // Adding an item to a folder that doesn't exist.
1032 String msg = "adding item: " + item + " to a folder that " +
1033 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001034 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001036 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 break;
1038 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1039 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1040 break;
1041 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001042 }
1043 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001044 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001046 }
1047
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001049 * Creates a new unique child id, for a given cell span across all layouts.
1050 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001051 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001052 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001053 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001055 }
1056
Sunny Goyal34942622014-08-29 17:20:55 -07001057 private static ArrayList<ItemInfo> getItemsByPackageName(
1058 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001059 ItemInfoFilter filter = new ItemInfoFilter() {
1060 @Override
1061 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1062 return cn.getPackageName().equals(pn) && info.user.equals(user);
1063 }
1064 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001065 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001066 }
1067
1068 /**
1069 * Removes all the items from the database corresponding to the specified package.
1070 */
1071 static void deletePackageFromDatabase(Context context, final String pn,
1072 final UserHandleCompat user) {
1073 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001074 }
1075
1076 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001077 * Removes the specified item from the database
1078 * @param context
1079 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001080 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001081 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001082 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1083 items.add(item);
1084 deleteItemsFromDatabase(context, items);
1085 }
1086
1087 /**
1088 * Removes the specified items from the database
1089 * @param context
1090 * @param item
1091 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001092 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001094 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001095 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001096 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001097 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001098 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 // Lock on mBgLock *after* the db operation
1101 synchronized (sBgLock) {
1102 switch (item.itemType) {
1103 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1104 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001105 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001106 if (info.container == item.id) {
1107 // We are deleting a folder which still contains items that
1108 // think they are contained by that folder.
1109 String msg = "deleting a folder (" + item + ") which still " +
1110 "contains items (" + info + ")";
1111 Log.e(TAG, msg);
1112 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001114 sBgWorkspaceItems.remove(item);
1115 break;
1116 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1117 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1118 sBgWorkspaceItems.remove(item);
1119 break;
1120 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1121 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1122 break;
1123 }
1124 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001126 }
1127 }
Michael Jurka83df1882011-08-31 20:59:26 -07001128 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 }
1131
1132 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001133 * Update the order of the workspace screens in the database. The array list contains
1134 * a list of screen ids in the order that they should appear.
1135 */
Winson Chungc9168342013-06-26 14:54:55 -07001136 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001137 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 final ContentResolver cr = context.getContentResolver();
1139 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1140
1141 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001142 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 while (iter.hasNext()) {
1144 long id = iter.next();
1145 if (id < 0) {
1146 iter.remove();
1147 }
1148 }
1149
1150 Runnable r = new Runnable() {
1151 @Override
1152 public void run() {
Yura085c8532014-02-11 15:15:29 +00001153 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001155 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001156 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 for (int i = 0; i < count; i++) {
1158 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001159 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1161 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001162 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001163 }
Yura085c8532014-02-11 15:15:29 +00001164
1165 try {
1166 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1167 } catch (Exception ex) {
1168 throw new RuntimeException(ex);
1169 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001170
Winson Chungba9c37f2013-08-30 14:11:37 -07001171 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001172 sBgWorkspaceScreens.clear();
1173 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001174 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001175 }
1176 };
1177 runOnWorkerThread(r);
1178 }
1179
1180 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 * Remove the contents of the specified folder from the database
1182 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001183 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 final ContentResolver cr = context.getContentResolver();
1185
Michael Jurkac9d95c52011-08-29 14:03:34 -07001186 Runnable r = new Runnable() {
1187 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001188 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001190 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 sBgItemsIdMap.remove(info.id);
1192 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 sBgWorkspaceItems.remove(info);
1194 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001195
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001196 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001197 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001199 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 for (ItemInfo childInfo : info.contents) {
1201 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001203 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001204 }
1205 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
1208
1209 /**
1210 * Set this as the current Launcher activity object for the loader.
1211 */
1212 public void initialize(Callbacks callbacks) {
1213 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001214 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1215 // workspace to prevent leaking Launcher activities on orientation change.
1216 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 mCallbacks = new WeakReference<Callbacks>(callbacks);
1218 }
1219 }
1220
Kenny Guyed131872014-04-30 03:02:21 +01001221 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001222 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001223 int op = PackageUpdatedTask.OP_UPDATE;
1224 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1225 user));
1226 }
1227
1228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001230 int op = PackageUpdatedTask.OP_REMOVE;
1231 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1232 user));
1233 }
1234
1235 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001236 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001237 int op = PackageUpdatedTask.OP_ADD;
1238 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1239 user));
1240 }
1241
1242 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001243 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001244 boolean replacing) {
1245 if (!replacing) {
1246 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1247 user));
1248 if (mAppsCanBeOnRemoveableStorage) {
1249 // Only rebind if we support removable storage. It catches the
1250 // case where
1251 // apps on the external sd card need to be reloaded
1252 startLoaderFromBackground();
1253 }
1254 } else {
1255 // If we are replacing then just update the packages in the list
1256 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1257 packageNames, user));
1258 }
1259 }
1260
1261 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001262 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001263 boolean replacing) {
1264 if (!replacing) {
1265 enqueuePackageUpdated(new PackageUpdatedTask(
1266 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1267 user));
1268 }
Kenny Guyed131872014-04-30 03:02:21 +01001269 }
1270
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001271 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1273 * ACTION_PACKAGE_CHANGED.
1274 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001275 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001276 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001277 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001278
Joe Onorato36115782010-06-17 13:28:48 -04001279 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001280 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001281 // If we have changed locale we need to clear out the labels in all apps/workspace.
1282 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001283 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1284 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001285 Callbacks callbacks = getCallback();
1286 if (callbacks != null) {
1287 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001288 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001289 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1290 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1291 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001292 }
1293 }
1294
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001295 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001296 resetLoadedState(true, true);
1297
Reena Lee93f824a2011-09-23 17:20:28 -07001298 // Do this here because if the launcher activity is running it will be restarted.
1299 // If it's not running startLoaderFromBackground will merely tell it that it needs
1300 // to reload.
1301 startLoaderFromBackground();
1302 }
1303
Winson Chungf0c6ae02012-03-21 16:10:31 -07001304 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1305 synchronized (mLock) {
1306 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1307 // mWorkspaceLoaded to true later
1308 stopLoaderLocked();
1309 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1310 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1311 }
1312 }
1313
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001314 /**
1315 * When the launcher is in the background, it's possible for it to miss paired
1316 * configuration changes. So whenever we trigger the loader from the background
1317 * tell the launcher that it needs to re-run the loader when it comes back instead
1318 * of doing it now.
1319 */
1320 public void startLoaderFromBackground() {
1321 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001322 Callbacks callbacks = getCallback();
1323 if (callbacks != null) {
1324 // Only actually run the loader if they're not paused.
1325 if (!callbacks.setLoadOnResume()) {
1326 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001327 }
1328 }
1329 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001330 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001331 }
Joe Onorato36115782010-06-17 13:28:48 -04001332 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001333
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001334 /**
1335 * If there is already a loader task running, tell it to stop.
1336 */
1337 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001338 LoaderTask oldTask = mLoaderTask;
1339 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001340 oldTask.stopLocked();
1341 }
Reena Lee93f824a2011-09-23 17:20:28 -07001342 }
1343
Adam Cohen1a85c582014-09-30 09:48:49 -07001344 public boolean isCurrentCallbacks(Callbacks callbacks) {
1345 return (mCallbacks != null && mCallbacks.get() == callbacks);
1346 }
1347
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001348 public void startLoader(int synchronousBindPage) {
1349 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001350 }
1351
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001352 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001353 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1354 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001355 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 // Clear any deferred bind-runnables from the synchronized load process
1357 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001358 synchronized (mDeferredBindRunnables) {
1359 mDeferredBindRunnables.clear();
1360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361
Joe Onorato36115782010-06-17 13:28:48 -04001362 // Don't bother to start the thread if we know it's not going to do anything
1363 if (mCallbacks != null && mCallbacks.get() != null) {
1364 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001365 stopLoaderLocked();
1366 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001367 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001368 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1370 } else {
1371 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1372 sWorker.post(mLoaderTask);
1373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
1376 }
1377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 void bindRemainingSynchronousPages() {
1379 // Post the remaining side pages to be loaded
1380 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001381 Runnable[] deferredBindRunnables = null;
1382 synchronized (mDeferredBindRunnables) {
1383 deferredBindRunnables = mDeferredBindRunnables.toArray(
1384 new Runnable[mDeferredBindRunnables.size()]);
1385 mDeferredBindRunnables.clear();
1386 }
1387 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001388 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001390 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001391
1392 // Run all the bind complete runnables after workspace is bound.
1393 if (!mBindCompleteRunnables.isEmpty()) {
1394 synchronized (mBindCompleteRunnables) {
1395 for (final Runnable r : mBindCompleteRunnables) {
1396 runOnWorkerThread(r);
1397 }
1398 mBindCompleteRunnables.clear();
1399 }
1400 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 }
1402
Joe Onorato36115782010-06-17 13:28:48 -04001403 public void stopLoader() {
1404 synchronized (mLock) {
1405 if (mLoaderTask != null) {
1406 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 }
1408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001411 /**
1412 * Loads the workspace screen ids in an ordered list.
1413 */
Adam Cohen091440a2015-03-18 14:16:05 -07001414 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001415 final ContentResolver contentResolver = context.getContentResolver();
1416 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001417
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001418 // Get screens ordered by rank.
1419 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1420 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1421 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001422 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001423 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001424 while (sc.moveToNext()) {
1425 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001426 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001427 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001428 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1429 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001430 }
1431 }
1432 } finally {
1433 sc.close();
1434 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001435 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001436 }
1437
Michael Jurkac57b7a82011-08-09 22:02:20 -07001438 public boolean isAllAppsLoaded() {
1439 return mAllAppsLoaded;
1440 }
1441
Winson Chung36a62fe2012-05-06 18:04:42 -07001442 boolean isLoadingWorkspace() {
1443 synchronized (mLock) {
1444 if (mLoaderTask != null) {
1445 return mLoaderTask.isLoadingWorkspace();
1446 }
1447 }
1448 return false;
1449 }
1450
Joe Onorato36115782010-06-17 13:28:48 -04001451 /**
1452 * Runnable for the thread that loads the contents of the launcher:
1453 * - workspace icons
1454 * - widgets
1455 * - all apps icons
1456 */
1457 private class LoaderTask implements Runnable {
1458 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001459 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001460 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001461 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001462 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001464 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001465 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001466 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
1468
Winson Chung36a62fe2012-05-06 18:04:42 -07001469 boolean isLoadingWorkspace() {
1470 return mIsLoadingAndBindingWorkspace;
1471 }
1472
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001473 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001474 mIsLoadingAndBindingWorkspace = true;
1475
Joe Onorato36115782010-06-17 13:28:48 -04001476 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001477 if (DEBUG_LOADERS) {
1478 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001480
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001481 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001482 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001483 synchronized (LoaderTask.this) {
1484 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001485 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001486 }
1487 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 }
1489 }
1490
Joe Onorato36115782010-06-17 13:28:48 -04001491 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001492 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001493 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001494
Joe Onorato36115782010-06-17 13:28:48 -04001495 private void waitForIdle() {
1496 // Wait until the either we're stopped or the other threads are done.
1497 // This way we don't start loading all apps until the workspace has settled
1498 // down.
1499 synchronized (LoaderTask.this) {
1500 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 mHandler.postIdle(new Runnable() {
1503 public void run() {
1504 synchronized (LoaderTask.this) {
1505 mLoadAndBindStepFinished = true;
1506 if (DEBUG_LOADERS) {
1507 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001510 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001511 }
Joe Onorato36115782010-06-17 13:28:48 -04001512 });
1513
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001514 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001515 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001516 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1517 // wait no longer than 1sec at a time
1518 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001519 } catch (InterruptedException ex) {
1520 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001521 }
1522 }
Joe Onorato36115782010-06-17 13:28:48 -04001523 if (DEBUG_LOADERS) {
1524 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001525 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001526 + "ms for previous step to finish binding");
1527 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001528 }
Joe Onorato36115782010-06-17 13:28:48 -04001529 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001530
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001532 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 // Ensure that we have a valid page index to load synchronously
1534 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1535 "valid page index");
1536 }
1537 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1538 // Ensure that we don't try and bind a specified page when the pages have not been
1539 // loaded already (we should load everything asynchronously in that case)
1540 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1541 }
1542 synchronized (mLock) {
1543 if (mIsLoaderTaskRunning) {
1544 // Ensure that we are never running the background loading at this point since
1545 // we also touch the background collections
1546 throw new RuntimeException("Error! Background loading is already running");
1547 }
1548 }
1549
1550 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1551 // data structures, we can't allow any other thread to touch that data, but because
1552 // this call is synchronous, we can get away with not locking).
1553
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001554 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001555 // operations from the previous activity. We need to ensure that all queued operations
1556 // are executed before any synchronous binding work is done.
1557 mHandler.flush();
1558
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559 // Divide the set of loaded items into those that we are binding synchronously, and
1560 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001561 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001562 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1563 // arise from that.
1564 onlyBindAllApps();
1565 }
1566
Joe Onorato36115782010-06-17 13:28:48 -04001567 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001568 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001569 if (mStopped) {
1570 return;
1571 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001572 mIsLoaderTaskRunning = true;
1573 }
Joe Onorato36115782010-06-17 13:28:48 -04001574 // Optimize for end-user experience: if the Launcher is up and // running with the
1575 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1576 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001577 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001578 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001579 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001580
Joe Onorato36115782010-06-17 13:28:48 -04001581 if (mStopped) {
1582 break keep_running;
1583 }
1584
Joe Onorato36115782010-06-17 13:28:48 -04001585 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001586
1587 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001588 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1589 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591
Joe Onorato36115782010-06-17 13:28:48 -04001592 // Clear out this reference, otherwise we end up holding it until all of the
1593 // callback runnables are done.
1594 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595
Joe Onorato36115782010-06-17 13:28:48 -04001596 synchronized (mLock) {
1597 // If we are still the last one to be scheduled, remove ourselves.
1598 if (mLoaderTask == this) {
1599 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001601 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001602 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001603 }
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
1605
1606 public void stopLocked() {
1607 synchronized (LoaderTask.this) {
1608 mStopped = true;
1609 this.notify();
1610 }
1611 }
1612
1613 /**
1614 * Gets the callbacks object. If we've been stopped, or if the launcher object
1615 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1616 * object that was around when the deferred message was scheduled, and if there's
1617 * a new Callbacks object around then also return null. This will save us from
1618 * calling onto it with data that will be ignored.
1619 */
1620 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1621 synchronized (mLock) {
1622 if (mStopped) {
1623 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001624 }
Joe Onorato36115782010-06-17 13:28:48 -04001625
1626 if (mCallbacks == null) {
1627 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001628 }
Joe Onorato36115782010-06-17 13:28:48 -04001629
1630 final Callbacks callbacks = mCallbacks.get();
1631 if (callbacks != oldCallbacks) {
1632 return null;
1633 }
1634 if (callbacks == null) {
1635 Log.w(TAG, "no mCallbacks");
1636 return null;
1637 }
1638
1639 return callbacks;
1640 }
1641 }
1642
1643 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001644 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001645 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001646 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1647 final int countX = (int) profile.numColumns;
1648 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001649
Adam Cohendcd297f2013-06-18 13:13:40 -07001650 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001651 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001652 // Return early if we detect that an item is under the hotseat button
1653 if (mCallbacks == null ||
1654 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001655 Log.e(TAG, "Error loading shortcut into hotseat " + item
1656 + " into position (" + item.screenId + ":" + item.cellX + ","
1657 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001658 return false;
1659 }
1660
Dan Sandler295ae182013-12-10 16:05:47 -05001661 final ItemInfo[][] hotseatItems =
1662 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1663
Adam Cohen2e6da152015-05-06 11:42:25 -07001664 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001665 Log.e(TAG, "Error loading shortcut " + item
1666 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001667 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001668 + ")");
1669 return false;
1670 }
1671
Dan Sandler295ae182013-12-10 16:05:47 -05001672 if (hotseatItems != null) {
1673 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001674 Log.e(TAG, "Error loading shortcut into hotseat " + item
1675 + " into position (" + item.screenId + ":" + item.cellX + ","
1676 + item.cellY + ") occupied by "
1677 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1678 [(int) item.screenId][0]);
1679 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001680 } else {
1681 hotseatItems[(int) item.screenId][0] = item;
1682 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001683 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001684 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001685 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001686 items[(int) item.screenId][0] = item;
1687 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001688 return true;
1689 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001690 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1691 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001692 return true;
1693 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001694
Adam Cohendcd297f2013-06-18 13:13:40 -07001695 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001696 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001697 occupied.put(item.screenId, items);
1698 }
1699
Dan Sandler295ae182013-12-10 16:05:47 -05001700 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001701 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1702 item.cellX < 0 || item.cellY < 0 ||
1703 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1704 Log.e(TAG, "Error loading shortcut " + item
1705 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1706 + item.cellX + "," + item.cellY
1707 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1708 return false;
1709 }
1710
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001711 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001712 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1713 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001715 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001717 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001718 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001720 return false;
1721 }
1722 }
1723 }
1724 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1725 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001726 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001727 }
1728 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001729
Joe Onorato36115782010-06-17 13:28:48 -04001730 return true;
1731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732
Winson Chungba9c37f2013-08-30 14:11:37 -07001733 /** Clears all the sBg data structures */
1734 private void clearSBgDataStructures() {
1735 synchronized (sBgLock) {
1736 sBgWorkspaceItems.clear();
1737 sBgAppWidgets.clear();
1738 sBgFolders.clear();
1739 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001740 sBgWorkspaceScreens.clear();
1741 }
1742 }
1743
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001744 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001745 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746
Joe Onorato36115782010-06-17 13:28:48 -04001747 final Context context = mContext;
1748 final ContentResolver contentResolver = context.getContentResolver();
1749 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001750 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001751 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1752 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001753 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001754
Winson Chung892c74d2013-08-22 16:15:50 -07001755 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001756 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1757 int countX = (int) profile.numColumns;
1758 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001759
Dan Sandlerd5024042014-01-09 15:01:33 -05001760 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1761 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1762 LauncherAppState.getLauncherProvider().deleteDatabase();
1763 }
1764
1765 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1766 // append the user's Launcher2 shortcuts
1767 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1768 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1769 } else {
1770 // Make sure the default workspace is loaded
1771 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001772 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001773 }
Adam Cohene25af792013-06-06 23:08:25 -07001774
Winson Chung2abf94d2012-07-18 18:16:38 -07001775 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001776 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001777 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001778 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001779
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001781 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001782 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001783 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001784 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 // +1 for the hotseat (it can be larger than the workspace)
1787 // Load workspace in reverse order to ensure that latest items are loaded first (and
1788 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001789 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 try {
1792 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1793 final int intentIndex = c.getColumnIndexOrThrow
1794 (LauncherSettings.Favorites.INTENT);
1795 final int titleIndex = c.getColumnIndexOrThrow
1796 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 final int containerIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.CONTAINER);
1799 final int itemTypeIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.ITEM_TYPE);
1801 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001803 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1804 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 final int screenIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.SCREEN);
1807 final int cellXIndex = c.getColumnIndexOrThrow
1808 (LauncherSettings.Favorites.CELLX);
1809 final int cellYIndex = c.getColumnIndexOrThrow
1810 (LauncherSettings.Favorites.CELLY);
1811 final int spanXIndex = c.getColumnIndexOrThrow
1812 (LauncherSettings.Favorites.SPANX);
1813 final int spanYIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001815 final int rankIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001817 final int restoredIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001819 final int profileIdIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001821 final int optionsIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001823 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824
Sunny Goyal7f834d22015-04-21 10:10:23 -07001825 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1826 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1827 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1828 }
1829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 ShortcutInfo info;
1831 String intentDescription;
1832 LauncherAppWidgetInfo appWidgetInfo;
1833 int container;
1834 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001835 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001837 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 while (!mStopped && c.moveToNext()) {
1840 try {
1841 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001842 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001843 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001844 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 switch (itemType) {
1847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001849 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001851 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001852 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001853 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001854 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001855 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001856 if (user == null) {
1857 // User has been deleted remove the item.
1858 itemsToRemove.add(id);
1859 continue;
1860 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 try {
1862 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001863 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001864 if (cn != null && cn.getPackageName() != null) {
1865 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1866 cn.getPackageName(), user);
1867 boolean validComponent = validPkg &&
1868 launcherApps.isActivityEnabledForProfile(cn, user);
1869
1870 if (validComponent) {
1871 if (restored) {
1872 // no special handling necessary for this item
1873 restoredRows.add(id);
1874 restored = false;
1875 }
1876 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001877 intent = null;
1878 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1879 // We allow auto install apps to have their intent
1880 // updated after an install.
1881 intent = manager.getLaunchIntentForPackage(
1882 cn.getPackageName());
1883 if (intent != null) {
1884 ContentValues values = new ContentValues();
1885 values.put(LauncherSettings.Favorites.INTENT,
1886 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001887 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001888 }
1889 }
1890
1891 if (intent == null) {
1892 // The app is installed but the component is no
1893 // longer available.
1894 Launcher.addDumpLog(TAG,
1895 "Invalid component removed: " + cn, true);
1896 itemsToRemove.add(id);
1897 continue;
1898 } else {
1899 // no special handling necessary for this item
1900 restoredRows.add(id);
1901 restored = false;
1902 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001903 } else if (restored) {
1904 // Package is not yet available but might be
1905 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001906 Launcher.addDumpLog(TAG,
1907 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001908
1909 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1910 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001911 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001912 // App restore has started. Update the flag
1913 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1914 ContentValues values = new ContentValues();
1915 values.put(LauncherSettings.Favorites.RESTORED,
1916 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001917 updateItem(id, values);
1918 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1919 // This is a common app. Try to replace this.
1920 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1921 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1922 if (parser.findDefaultApp()) {
1923 // Default app found. Replace it.
1924 intent = parser.parsedIntent;
1925 cn = intent.getComponent();
1926 ContentValues values = parser.parsedValues;
1927 values.put(LauncherSettings.Favorites.RESTORED, 0);
1928 updateItem(id, values);
1929 restored = false;
1930 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001931
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001932 } else if (REMOVE_UNRESTORED_ICONS) {
1933 Launcher.addDumpLog(TAG,
1934 "Unrestored package removed: " + cn, true);
1935 itemsToRemove.add(id);
1936 continue;
1937 }
Sunny Goyal94485362014-09-18 16:13:58 -07001938 } else if (REMOVE_UNRESTORED_ICONS) {
1939 Launcher.addDumpLog(TAG,
1940 "Unrestored package removed: " + cn, true);
1941 itemsToRemove.add(id);
1942 continue;
1943 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001944 } else if (launcherApps.isAppEnabled(
1945 manager, cn.getPackageName(),
1946 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1947 // Package is present but not available.
1948 allowMissingTarget = true;
1949 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1950 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001951 // SdCard is not ready yet. Package might get available,
1952 // once it is ready.
1953 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1954 + " (check again later)", true);
1955 HashSet<String> pkgs = sPendingPackages.get(user);
1956 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001957 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001958 sPendingPackages.put(user, pkgs);
1959 }
1960 pkgs.add(cn.getPackageName());
1961 allowMissingTarget = true;
1962 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001963
1964 } else {
1965 // Do not wait for external media load anymore.
1966 // Log the invalid package, and remove it
1967 Launcher.addDumpLog(TAG,
1968 "Invalid package removed: " + cn, true);
1969 itemsToRemove.add(id);
1970 continue;
Winson Chungee055712013-07-30 14:46:24 -07001971 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001972 } else if (cn == null) {
1973 // For shortcuts with no component, keep them as they are
1974 restoredRows.add(id);
1975 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001976 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001978 Launcher.addDumpLog(TAG,
1979 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 continue;
1981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982
Sunny Goyal34b65272015-03-11 16:56:52 -07001983 boolean useLowResIcon = container >= 0 &&
1984 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1985
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001986 if (itemReplaced) {
1987 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001988 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001989 cursorIconInfo.iconIndex, titleIndex,
1990 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001991 } else {
1992 // Don't replace items for other profiles.
1993 itemsToRemove.add(id);
1994 continue;
1995 }
1996 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001997 if (user.equals(UserHandleCompat.myUserHandle())) {
1998 Launcher.addDumpLog(TAG,
1999 "constructing info for partially restored package",
2000 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002001 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002002 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002003 intent = getRestoredItemIntent(c, context, intent);
2004 } else {
2005 // Don't restore items for other profiles.
2006 itemsToRemove.add(id);
2007 continue;
2008 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002009 } else if (itemType ==
2010 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002011 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002012 cursorIconInfo.iconIndex, titleIndex,
2013 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002015 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 // App shortcuts that used to be automatically added to Launcher
2018 // didn't always have the correct intent flags set, so do that
2019 // here
2020 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002021 intent.getCategories() != null &&
2022 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002023 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 intent.addFlags(
2025 Intent.FLAG_ACTIVITY_NEW_TASK |
2026 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2027 }
Michael Jurka96879562012-03-22 05:54:33 -07002028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002029
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002031 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002034 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 info.cellX = c.getInt(cellXIndex);
2036 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002037 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002038 info.spanX = 1;
2039 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002040 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002041 if (info.promisedIntent != null) {
2042 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2043 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002044 info.isDisabled = disabledState;
2045 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2046 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2047 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002048
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002050 if (!checkItemPlacement(occupied, info)) {
2051 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 break;
2053 }
2054
Sunny Goyal756adbc2015-04-16 15:20:51 -07002055 if (restored) {
2056 ComponentName cn = info.getTargetComponent();
2057 if (cn != null) {
2058 Integer progress = installingPkgs.get(cn.getPackageName());
2059 if (progress != null) {
2060 info.setInstallProgress(progress);
2061 } else {
2062 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2063 }
2064 }
2065 }
2066
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 switch (container) {
2068 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2069 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2070 sBgWorkspaceItems.add(info);
2071 break;
2072 default:
2073 // Item is in a user folder
2074 FolderInfo folderInfo =
2075 findOrMakeFolder(sBgFolders, container);
2076 folderInfo.add(info);
2077 break;
2078 }
2079 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002080 } else {
2081 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 }
2083 break;
2084
2085 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2086 id = c.getLong(idIndex);
2087 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2088
Sunny Goyala508e4f2015-05-21 09:33:57 -07002089 // Do not trim the folder label, as is was set by the user.
2090 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002093 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 folderInfo.cellX = c.getInt(cellXIndex);
2095 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002096 folderInfo.spanX = 1;
2097 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002098 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099
Daniel Sandler8802e962010-05-26 16:28:16 -04002100 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002101 if (!checkItemPlacement(occupied, folderInfo)) {
2102 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002103 break;
2104 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002105
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2108 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2109 sBgWorkspaceItems.add(folderInfo);
2110 break;
Joe Onorato36115782010-06-17 13:28:48 -04002111 }
Joe Onorato17a89222011-02-08 17:26:11 -08002112
Chris Wrenf4d08112014-01-16 18:13:56 -05002113 if (restored) {
2114 // no special handling required for restored folders
2115 restoredRows.add(id);
2116 }
2117
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2119 sBgFolders.put(folderInfo.id, folderInfo);
2120 break;
2121
2122 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002123 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002125 boolean customWidget = itemType ==
2126 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2127
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002129 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002130 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002131 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002132 user = allUsers.get(serialNumber);
2133 if (user == null) {
2134 itemsToRemove.add(id);
2135 continue;
2136 }
2137
Sunny Goyalff572272014-07-23 13:58:07 -07002138 final ComponentName component =
2139 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002140
Sunny Goyal651077b2014-06-30 14:15:31 -07002141 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002142 final boolean isIdValid = (restoreStatus &
2143 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002144 final boolean wasProviderReady = (restoreStatus &
2145 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002146
Adam Cohen59400422014-03-05 18:07:04 -08002147 final LauncherAppWidgetProviderInfo provider =
2148 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002149 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002150 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002151
2152 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002153 if (!isSafeMode && !customWidget &&
2154 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002155 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002156 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002157
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002159 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 itemsToRemove.add(id);
2161 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002162 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002163 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2164 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002165
Sunny Goyal53f96722015-07-13 19:54:53 -07002166 // The provider is available. So the widget is either
2167 // available or not available. We do not need to track
2168 // any future restore updates.
2169 int status = restoreStatus &
2170 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002171 if (!wasProviderReady) {
2172 // If provider was not previously ready, update the
2173 // status and UI flag.
2174
2175 // Id would be valid only if the widget restore broadcast was received.
2176 if (isIdValid) {
2177 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2178 } else {
2179 status &= ~LauncherAppWidgetInfo
2180 .FLAG_PROVIDER_NOT_READY;
2181 }
2182 }
2183 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002184 } else {
2185 Log.v(TAG, "Widget restore pending id=" + id
2186 + " appWidgetId=" + appWidgetId
2187 + " status =" + restoreStatus);
2188 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002189 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002190 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002191 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002192
2193 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2194 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002195 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002196 // App restore has started. Update the flag
2197 appWidgetInfo.restoreStatus |=
2198 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002199 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002200 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002201 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002202 itemsToRemove.add(id);
2203 continue;
2204 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002205
2206 appWidgetInfo.installProgress =
2207 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002208 }
Sunny Goyalff572272014-07-23 13:58:07 -07002209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 appWidgetInfo.cellX = c.getInt(cellXIndex);
2213 appWidgetInfo.cellY = c.getInt(cellYIndex);
2214 appWidgetInfo.spanX = c.getInt(spanXIndex);
2215 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002216 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002217
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2219 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2220 Log.e(TAG, "Widget found where container != " +
2221 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2222 continue;
2223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002225 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002227 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2228 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 break;
2230 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002231
Adam Cohen59400422014-03-05 18:07:04 -08002232 if (!customWidget) {
2233 String providerName =
2234 appWidgetInfo.providerName.flattenToString();
2235 if (!providerName.equals(savedProvider) ||
2236 (appWidgetInfo.restoreStatus != restoreStatus)) {
2237 ContentValues values = new ContentValues();
2238 values.put(
2239 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2240 providerName);
2241 values.put(LauncherSettings.Favorites.RESTORED,
2242 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002243 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002244 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002245 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2247 sBgAppWidgets.add(appWidgetInfo);
2248 }
Joe Onorato36115782010-06-17 13:28:48 -04002249 break;
2250 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002252 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002253 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 }
2255 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002256 if (c != null) {
2257 c.close();
2258 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 }
2260
Winson Chungba9c37f2013-08-30 14:11:37 -07002261 // Break early if we've stopped loading
2262 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002263 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002264 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002265 }
2266
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002269 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2270 Utilities.createDbSelectionQuery(
2271 LauncherSettings.Favorites._ID, itemsToRemove), null);
2272 if (DEBUG_LOADERS) {
2273 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2274 LauncherSettings.Favorites._ID, itemsToRemove));
2275 }
2276
2277 // Remove any empty folder
2278 for (long folderId : LauncherAppState.getLauncherProvider()
2279 .deleteEmptyFolders()) {
2280 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2281 sBgFolders.remove(folderId);
2282 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002283 }
2284 }
2285
Chris Wrenf4d08112014-01-16 18:13:56 -05002286 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002287 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002288 ContentValues values = new ContentValues();
2289 values.put(LauncherSettings.Favorites.RESTORED, 0);
2290 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2291 Utilities.createDbSelectionQuery(
2292 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002293 }
2294
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002295 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2296 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002297 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002298 null, sWorker);
2299 }
2300
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002301 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002302
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002303 // Remove any empty screens
2304 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002305 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002306 long screenId = item.screenId;
2307 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2308 unusedScreens.contains(screenId)) {
2309 unusedScreens.remove(screenId);
2310 }
2311 }
2312
2313 // If there are any empty screens remove them, and update.
2314 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002315 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 }
2318
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 if (DEBUG_LOADERS) {
2320 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2321 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002323 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002325
Sunny Goyale2df0622015-04-24 11:27:00 -07002326 for (int i = 0; i < nScreens; i++) {
2327 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002328 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 line += " | ";
2330 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002331 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002332 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002333 if (x < screen.length && y < screen[x].length) {
2334 line += (screen[x][y] != null) ? "#" : ".";
2335 } else {
2336 line += "!";
2337 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 }
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 }
Joe Onorato36115782010-06-17 13:28:48 -04002343 }
Adam Cohene25af792013-06-06 23:08:25 -07002344 }
2345
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002346 /**
2347 * Partially updates the item without any notification. Must be called on the worker thread.
2348 */
2349 private void updateItem(long itemId, ContentValues update) {
2350 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002351 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002352 update,
2353 BaseColumns._ID + "= ?",
2354 new String[]{Long.toString(itemId)});
2355 }
2356
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 /** Filters the set of items who are directly or indirectly (via another container) on the
2358 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002359 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 ArrayList<ItemInfo> allWorkspaceItems,
2361 ArrayList<ItemInfo> currentScreenItems,
2362 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002363 // Purge any null ItemInfos
2364 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2365 while (iter.hasNext()) {
2366 ItemInfo i = iter.next();
2367 if (i == null) {
2368 iter.remove();
2369 }
2370 }
2371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 // Order the set of items by their containers first, this allows use to walk through the
2373 // list sequentially, build up a list of containers that are in the specified screen,
2374 // as well as all items in those containers.
2375 Set<Long> itemsOnScreen = new HashSet<Long>();
2376 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2377 @Override
2378 public int compare(ItemInfo lhs, ItemInfo rhs) {
2379 return (int) (lhs.container - rhs.container);
2380 }
2381 });
2382 for (ItemInfo info : allWorkspaceItems) {
2383 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002384 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 currentScreenItems.add(info);
2386 itemsOnScreen.add(info.id);
2387 } else {
2388 otherScreenItems.add(info);
2389 }
2390 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2391 currentScreenItems.add(info);
2392 itemsOnScreen.add(info.id);
2393 } else {
2394 if (itemsOnScreen.contains(info.container)) {
2395 currentScreenItems.add(info);
2396 itemsOnScreen.add(info.id);
2397 } else {
2398 otherScreenItems.add(info);
2399 }
2400 }
2401 }
2402 }
2403
2404 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002405 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 ArrayList<LauncherAppWidgetInfo> appWidgets,
2407 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2408 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409
2410 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002411 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002413 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002414 currentScreenWidgets.add(widget);
2415 } else {
2416 otherScreenWidgets.add(widget);
2417 }
2418 }
2419 }
2420
2421 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002422 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002423 LongArrayMap<ItemInfo> itemsIdMap,
2424 LongArrayMap<FolderInfo> folders,
2425 LongArrayMap<FolderInfo> currentScreenFolders,
2426 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427
Sunny Goyale2df0622015-04-24 11:27:00 -07002428 int total = folders.size();
2429 for (int i = 0; i < total; i++) {
2430 long id = folders.keyAt(i);
2431 FolderInfo folder = folders.valueAt(i);
2432
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002434 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002436 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 currentScreenFolders.put(id, folder);
2438 } else {
2439 otherScreenFolders.put(id, folder);
2440 }
2441 }
2442 }
2443
2444 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2445 * right) */
2446 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002447 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002448 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 // XXX: review this
2450 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002451 @Override
2452 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002453 int cellCountX = (int) profile.numColumns;
2454 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002455 int screenOffset = cellCountX * cellCountY;
2456 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002457 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002458 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002459 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002460 rhs.cellY * cellCountX + rhs.cellX);
2461 return (int) (lr - rr);
2462 }
2463 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002465
Adam Cohendcd297f2013-06-18 13:13:40 -07002466 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2467 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 final Runnable r = new Runnable() {
2469 @Override
2470 public void run() {
2471 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2472 if (callbacks != null) {
2473 callbacks.bindScreens(orderedScreens);
2474 }
2475 }
2476 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002477 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 }
2479
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2481 final ArrayList<ItemInfo> workspaceItems,
2482 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002483 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002485
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 final boolean postOnMainThread = (deferredBindRunnables != null);
2487
2488 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002489 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002491 final int start = i;
2492 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 final Runnable r = new Runnable() {
2494 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002496 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002498 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2499 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002500 }
2501 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 };
2503 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002504 synchronized (deferredBindRunnables) {
2505 deferredBindRunnables.add(r);
2506 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002508 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 }
Joe Onorato36115782010-06-17 13:28:48 -04002510 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511
2512 // Bind the folders
2513 if (!folders.isEmpty()) {
2514 final Runnable r = new Runnable() {
2515 public void run() {
2516 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2517 if (callbacks != null) {
2518 callbacks.bindFolders(folders);
2519 }
2520 }
2521 };
2522 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002523 synchronized (deferredBindRunnables) {
2524 deferredBindRunnables.add(r);
2525 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002527 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 }
2529 }
2530
2531 // Bind the widgets, one at a time
2532 N = appWidgets.size();
2533 for (int i = 0; i < N; i++) {
2534 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2535 final Runnable r = new Runnable() {
2536 public void run() {
2537 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2538 if (callbacks != null) {
2539 callbacks.bindAppWidget(widget);
2540 }
2541 }
2542 };
2543 if (postOnMainThread) {
2544 deferredBindRunnables.add(r);
2545 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002546 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 }
2548 }
2549 }
2550
2551 /**
2552 * Binds all loaded data to actual views on the main thread.
2553 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002554 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002555 final long t = SystemClock.uptimeMillis();
2556 Runnable r;
2557
2558 // Don't use these two variables in any of the callback runnables.
2559 // Otherwise we hold a reference to them.
2560 final Callbacks oldCallbacks = mCallbacks.get();
2561 if (oldCallbacks == null) {
2562 // This launcher has exited and nobody bothered to tell us. Just bail.
2563 Log.w(TAG, "LoaderTask running with no launcher");
2564 return;
2565 }
2566
Winson Chung9b9fb962013-11-15 15:39:34 -08002567 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2569 ArrayList<LauncherAppWidgetInfo> appWidgets =
2570 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002571 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002572
2573 final LongArrayMap<FolderInfo> folders;
2574 final LongArrayMap<ItemInfo> itemsIdMap;
2575
Winson Chung2abf94d2012-07-18 18:16:38 -07002576 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577 workspaceItems.addAll(sBgWorkspaceItems);
2578 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002579 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002580
2581 folders = sBgFolders.clone();
2582 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 }
2584
Derek Prothro7aff3992013-12-10 14:00:37 -05002585 final boolean isLoadingSynchronously =
2586 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002587 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002588 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002589 if (currScreen >= orderedScreenIds.size()) {
2590 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002591 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002592 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002593 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002594 final long currentScreenId = currentScreen < 0
2595 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002596
2597 // Load all the items that are on the current page first (and in the process, unbind
2598 // all the existing workspace items before we call startBinding() below.
2599 unbindWorkspaceItemsOnMainThread();
2600
2601 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002602 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2603 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2604 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2605 new ArrayList<LauncherAppWidgetInfo>();
2606 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2607 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002608 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2609 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610
Winson Chung9b9fb962013-11-15 15:39:34 -08002611 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002612 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002613 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002614 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002615 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002616 otherFolders);
2617 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2618 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2619
2620 // Tell the workspace that we're about to start binding items
2621 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002622 public void run() {
2623 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2624 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002626 }
2627 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002629 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630
Adam Cohendcd297f2013-06-18 13:13:40 -07002631 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2632
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 // Load items on the current page
2634 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2635 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002636 if (isLoadingSynchronously) {
2637 r = new Runnable() {
2638 public void run() {
2639 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002640 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002641 callbacks.onPageBoundSynchronously(currentScreen);
2642 }
2643 }
2644 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002645 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002646 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002647
Winson Chung4a2afa32012-07-19 14:53:05 -07002648 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2649 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002650 synchronized (mDeferredBindRunnables) {
2651 mDeferredBindRunnables.clear();
2652 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002653 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002654 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002655
2656 // Tell the workspace that we're done binding items
2657 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002658 public void run() {
2659 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2660 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002661 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002662 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663
Winson Chung98e030b2012-05-07 16:01:11 -07002664 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002665 if (DEBUG_LOADERS) {
2666 Log.d(TAG, "bound workspace in "
2667 + (SystemClock.uptimeMillis()-t) + "ms");
2668 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002669
2670 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002672 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002673 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002674 synchronized (mDeferredBindRunnables) {
2675 mDeferredBindRunnables.add(r);
2676 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002677 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002678 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002679 }
Joe Onorato36115782010-06-17 13:28:48 -04002680 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002681
Joe Onorato36115782010-06-17 13:28:48 -04002682 private void loadAndBindAllApps() {
2683 if (DEBUG_LOADERS) {
2684 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2685 }
2686 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002687 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002688 synchronized (LoaderTask.this) {
2689 if (mStopped) {
2690 return;
2691 }
2692 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002693 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002694 synchronized (LoaderTask.this) {
2695 if (mStopped) {
2696 return;
2697 }
2698 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002699 }
Joe Onorato36115782010-06-17 13:28:48 -04002700 } else {
2701 onlyBindAllApps();
2702 }
2703 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002704
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002705 private void updateIconCache() {
2706 // Ignore packages which have a promise icon.
2707 HashSet<String> packagesToIgnore = new HashSet<>();
2708 synchronized (sBgLock) {
2709 for (ItemInfo info : sBgItemsIdMap) {
2710 if (info instanceof ShortcutInfo) {
2711 ShortcutInfo si = (ShortcutInfo) info;
2712 if (si.isPromise() && si.getTargetComponent() != null) {
2713 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2714 }
2715 } else if (info instanceof LauncherAppWidgetInfo) {
2716 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2717 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2718 packagesToIgnore.add(lawi.providerName.getPackageName());
2719 }
2720 }
2721 }
2722 }
2723 mIconCache.updateDbIcons(packagesToIgnore);
2724 }
2725
Joe Onorato36115782010-06-17 13:28:48 -04002726 private void onlyBindAllApps() {
2727 final Callbacks oldCallbacks = mCallbacks.get();
2728 if (oldCallbacks == null) {
2729 // This launcher has exited and nobody bothered to tell us. Just bail.
2730 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2731 return;
2732 }
2733
2734 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002735 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002736 final ArrayList<AppInfo> list
2737 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002738 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002739 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002740 public void run() {
2741 final long t = SystemClock.uptimeMillis();
2742 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2743 if (callbacks != null) {
2744 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002745 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002746 }
2747 if (DEBUG_LOADERS) {
2748 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2749 + (SystemClock.uptimeMillis()-t) + "ms");
2750 }
2751 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002752 };
2753 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002754 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002755 r.run();
2756 } else {
2757 mHandler.post(r);
2758 }
Joe Onorato36115782010-06-17 13:28:48 -04002759 }
2760
Winson Chung64359a52013-07-08 17:17:08 -07002761 private void loadAllApps() {
2762 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002763
Joe Onorato36115782010-06-17 13:28:48 -04002764 final Callbacks oldCallbacks = mCallbacks.get();
2765 if (oldCallbacks == null) {
2766 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002767 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002768 return;
2769 }
2770
Kenny Guyed131872014-04-30 03:02:21 +01002771 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2772
Winson Chung64359a52013-07-08 17:17:08 -07002773 // Clear the list of apps
2774 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002775 for (UserHandleCompat user : profiles) {
2776 // Query for the set of apps
2777 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002778 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002779 if (DEBUG_LOADERS) {
2780 Log.d(TAG, "getActivityList took "
2781 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2782 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2783 }
2784 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002785 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002786 if (apps == null || apps.isEmpty()) {
2787 return;
2788 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002789
Kenny Guyed131872014-04-30 03:02:21 +01002790 // Create the ApplicationInfos
2791 for (int i = 0; i < apps.size(); i++) {
2792 LauncherActivityInfoCompat app = apps.get(i);
2793 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002794 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002795 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002796
Sunny Goyal756a28a2015-04-23 17:07:55 -07002797 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2798 if (heuristic != null) {
2799 runAfterBindCompletes(new Runnable() {
2800
2801 @Override
2802 public void run() {
2803 heuristic.processUserApps(apps);
2804 }
2805 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002806 }
Winson Chung64359a52013-07-08 17:17:08 -07002807 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002808 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002809 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2810 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002811
2812 // Post callback on main thread
2813 mHandler.post(new Runnable() {
2814 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002815
Winson Chung64359a52013-07-08 17:17:08 -07002816 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002817 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002818 if (callbacks != null) {
2819 callbacks.bindAllApplications(added);
2820 if (DEBUG_LOADERS) {
2821 Log.d(TAG, "bound " + added.size() + " apps in "
2822 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2823 }
2824 } else {
2825 Log.i(TAG, "not binding apps: no Launcher activity");
2826 }
2827 }
2828 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002829 // Cleanup any data stored for a deleted user.
2830 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002831
Hyunyoung Song219d0482015-05-07 12:51:42 -07002832 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2833 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002834 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002835 Log.d(TAG, "Icons processed in "
2836 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002837 }
2838 }
2839
2840 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002841 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002842 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002843 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2844 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2845 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2846 }
Joe Onorato36115782010-06-17 13:28:48 -04002847 }
2848 }
2849
Sunny Goyal75b0f552015-05-20 21:57:06 -07002850 /**
2851 * Called when the icons for packages have been updated in the icon cache.
2852 */
2853 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2854 final Callbacks callbacks = getCallback();
2855 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2856 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2857
2858 // If any package icon has changed (app was updated while launcher was dead),
2859 // update the corresponding shortcuts.
2860 synchronized (sBgLock) {
2861 for (ItemInfo info : sBgItemsIdMap) {
2862 if (info instanceof ShortcutInfo && user.equals(info.user)
2863 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2864 ShortcutInfo si = (ShortcutInfo) info;
2865 ComponentName cn = si.getTargetComponent();
2866 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2867 si.updateIcon(mIconCache);
2868 updatedShortcuts.add(si);
2869 }
2870 }
2871 }
2872 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2873 }
2874
2875 if (!updatedShortcuts.isEmpty()) {
2876 final UserHandleCompat userFinal = user;
2877 mHandler.post(new Runnable() {
2878
2879 public void run() {
2880 Callbacks cb = getCallback();
2881 if (cb != null && callbacks == cb) {
2882 cb.bindShortcutsChanged(updatedShortcuts,
2883 new ArrayList<ShortcutInfo>(), userFinal);
2884 }
2885 }
2886 });
2887 }
2888
2889 if (!updatedApps.isEmpty()) {
2890 mHandler.post(new Runnable() {
2891
2892 public void run() {
2893 Callbacks cb = getCallback();
2894 if (cb != null && callbacks == cb) {
2895 cb.bindAppsUpdated(updatedApps);
2896 }
2897 }
2898 });
2899 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002900
2901 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2902 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002903 }
2904
Joe Onorato36115782010-06-17 13:28:48 -04002905 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002906 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002907 }
2908
Adam Cohen091440a2015-03-18 14:16:05 -07002909 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002910
2911 @Override
2912 public void onReceive(Context context, Intent intent) {
2913 synchronized (sBgLock) {
2914 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2915 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002916 final PackageManager manager = context.getPackageManager();
2917 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2918 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002919 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2920 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002921 packagesRemoved.clear();
2922 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002923 for (String pkg : entry.getValue()) {
2924 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002925 boolean packageOnSdcard = launcherApps.isAppEnabled(
2926 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2927 if (packageOnSdcard) {
2928 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2929 packagesUnavailable.add(pkg);
2930 } else {
2931 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2932 packagesRemoved.add(pkg);
2933 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002934 }
2935 }
2936 if (!packagesRemoved.isEmpty()) {
2937 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2938 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2939 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 if (!packagesUnavailable.isEmpty()) {
2941 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2942 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2943 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002944 }
Sunny Goyal34942622014-08-29 17:20:55 -07002945 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002946 }
2947 }
2948 }
2949
Joe Onorato36115782010-06-17 13:28:48 -04002950 private class PackageUpdatedTask implements Runnable {
2951 int mOp;
2952 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002953 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002954
2955 public static final int OP_NONE = 0;
2956 public static final int OP_ADD = 1;
2957 public static final int OP_UPDATE = 2;
2958 public static final int OP_REMOVE = 3; // uninstlled
2959 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2960
2961
Kenny Guyed131872014-04-30 03:02:21 +01002962 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002963 mOp = op;
2964 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002965 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002966 }
2967
2968 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002969 if (!mHasLoaderCompletedOnce) {
2970 // Loader has not yet run.
2971 return;
2972 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002973 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002974
2975 final String[] packages = mPackages;
2976 final int N = packages.length;
2977 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002978 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002979 for (int i=0; i<N; i++) {
2980 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002981 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002982 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002983 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002984
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002985 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2986 if (heuristic != null) {
2987 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002988 }
Joe Onorato36115782010-06-17 13:28:48 -04002989 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002990 }
Joe Onorato36115782010-06-17 13:28:48 -04002991 case OP_UPDATE:
2992 for (int i=0; i<N; i++) {
2993 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002994 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002995 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002996 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002997 }
2998 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002999 case OP_REMOVE: {
3000 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3001 if (heuristic != null) {
3002 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003003 }
Joe Onorato36115782010-06-17 13:28:48 -04003004 for (int i=0; i<N; i++) {
3005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003006 mIconCache.removeIconsForPkg(packages[i], mUser);
3007 }
3008 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003009 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003010 case OP_UNAVAILABLE:
3011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3013 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003014 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003015 }
3016 break;
3017 }
3018
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003019 ArrayList<AppInfo> added = null;
3020 ArrayList<AppInfo> modified = null;
3021 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003022
Adam Cohen487f7dd2012-06-28 18:12:10 -07003023 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003024 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003025 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003027 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003028 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003029 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003030 }
Winson Chung5d55f332012-07-16 20:45:03 -07003031 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003032 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003033 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003034 }
3035
Sunny Goyale0f58d72014-11-10 18:05:31 -08003036 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003037 if (callbacks == null) {
3038 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3039 return;
3040 }
3041
Sunny Goyal4390ace2014-10-13 11:33:11 -07003042 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3043 new HashMap<ComponentName, AppInfo>();
3044
Joe Onorato36115782010-06-17 13:28:48 -04003045 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003046 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003047 for (AppInfo ai : added) {
3048 addedOrUpdatedApps.put(ai.componentName, ai);
3049 }
Joe Onorato36115782010-06-17 13:28:48 -04003050 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003051
Joe Onorato36115782010-06-17 13:28:48 -04003052 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003053 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003054 for (AppInfo ai : modified) {
3055 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003056 }
3057
Joe Onorato36115782010-06-17 13:28:48 -04003058 mHandler.post(new Runnable() {
3059 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003060 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003061 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003062 callbacks.bindAppsUpdated(modifiedFinal);
3063 }
3064 }
3065 });
3066 }
Winson Chung83892cc2013-05-01 16:53:33 -07003067
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003069 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003070 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3071 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3072 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3073
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003074 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003075 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003076 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003077 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3078 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 boolean infoUpdated = false;
3080 boolean shortcutUpdated = false;
3081
3082 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003083 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003085 Bitmap icon = Utilities.createIconBitmap(
3086 si.iconResource.packageName,
3087 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003088 if (icon != null) {
3089 si.setIcon(icon);
3090 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003092 }
3093 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094
3095 ComponentName cn = si.getTargetComponent();
3096 if (cn != null && packageSet.contains(cn.getPackageName())) {
3097 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3098
3099 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3101 // Auto install icon
3102 PackageManager pm = context.getPackageManager();
3103 ResolveInfo matched = pm.resolveActivity(
3104 new Intent(Intent.ACTION_MAIN)
3105 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3106 PackageManager.MATCH_DEFAULT_ONLY);
3107 if (matched == null) {
3108 // Try to find the best match activity.
3109 Intent intent = pm.getLaunchIntentForPackage(
3110 cn.getPackageName());
3111 if (intent != null) {
3112 cn = intent.getComponent();
3113 appInfo = addedOrUpdatedApps.get(cn);
3114 }
3115
3116 if ((intent == null) || (appInfo == null)) {
3117 removedShortcuts.add(si);
3118 continue;
3119 }
3120 si.promisedIntent = intent;
3121 }
3122 }
3123
3124 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003125 if (appInfo != null) {
3126 si.flags = appInfo.flags;
3127 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003128
Sunny Goyal756adbc2015-04-16 15:20:51 -07003129 si.intent = si.promisedIntent;
3130 si.promisedIntent = null;
3131 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 infoUpdated = true;
3133 si.updateIcon(mIconCache);
3134 }
3135
3136 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3137 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3138 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003139 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003140 si.contentDescription = appInfo.contentDescription;
3141 infoUpdated = true;
3142 }
3143
3144 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3145 // Since package was just updated, the target must be available now.
3146 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3147 shortcutUpdated = true;
3148 }
3149 }
3150
3151 if (infoUpdated || shortcutUpdated) {
3152 updatedShortcuts.add(si);
3153 }
3154 if (infoUpdated) {
3155 updateItemInDatabase(context, si);
3156 }
3157 } else if (info instanceof LauncherAppWidgetInfo) {
3158 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3159 if (mUser.equals(widgetInfo.user)
3160 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3161 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003162 widgetInfo.restoreStatus &=
3163 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3164 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165 widgets.add(widgetInfo);
3166 updateItemInDatabase(context, widgetInfo);
3167 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003168 }
3169 }
3170 }
3171
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3173 mHandler.post(new Runnable() {
3174
3175 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003176 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 if (callbacks == cb && cb != null) {
3178 callbacks.bindShortcutsChanged(
3179 updatedShortcuts, removedShortcuts, mUser);
3180 }
3181 }
3182 });
3183 if (!removedShortcuts.isEmpty()) {
3184 deleteItemsFromDatabase(context, removedShortcuts);
3185 }
3186 }
3187 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003188 mHandler.post(new Runnable() {
3189 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003190 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003191 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003192 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003193 }
3194 }
3195 });
3196 }
3197 }
3198
Winson Chungdf95eb12013-10-16 14:57:07 -07003199 final ArrayList<String> removedPackageNames =
3200 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003201 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003202 // Mark all packages in the broadcast to be removed
3203 removedPackageNames.addAll(Arrays.asList(packages));
3204 } else if (mOp == OP_UPDATE) {
3205 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003206 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003207 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003208 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003209 }
3210 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003211 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003212
Winson Chungdf95eb12013-10-16 14:57:07 -07003213 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003214 final int removeReason;
3215 if (mOp == OP_UNAVAILABLE) {
3216 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3217 } else {
3218 // Remove all the components associated with this package
3219 for (String pn : removedPackageNames) {
3220 deletePackageFromDatabase(context, pn, mUser);
3221 }
3222 // Remove all the specific components
3223 for (AppInfo a : removedApps) {
3224 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3225 deleteItemsFromDatabase(context, infos);
3226 }
3227 removeReason = 0;
3228 }
3229
Winson Chungdf95eb12013-10-16 14:57:07 -07003230 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003231 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003232 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003233 mHandler.post(new Runnable() {
3234 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003235 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003236 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003237 callbacks.bindComponentsRemoved(
3238 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003239 }
3240 }
3241 });
Joe Onoratobe386092009-11-17 17:32:16 -08003242 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003243
3244 // onProvidersChanged method (API >= 17) already refreshed the widget list
3245 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3246
Adam Cohen4caf2982013-08-20 18:54:31 -07003247 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003248 mHandler.post(new Runnable() {
3249 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003250 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003251 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003252 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003253 }
3254 }
3255 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 }
3257 }
3258
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003259 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3260 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003261 ArrayList<LauncherAppWidgetProviderInfo> results =
3262 new ArrayList<LauncherAppWidgetProviderInfo>();
3263 try {
3264 synchronized (sBgLock) {
3265 if (sBgWidgetProviders == null || refresh) {
3266 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3267 = new HashMap<>();
3268 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3269 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003270
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003271 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3272 for (AppWidgetProviderInfo pInfo : widgets) {
3273 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3274 UserHandleCompat user = wm.getUser(info);
3275 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3276 }
Robin Lee26ace122015-03-16 19:41:43 +00003277
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003278 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3279 for (CustomAppWidget widget : customWidgets) {
3280 info = new LauncherAppWidgetProviderInfo(context, widget);
3281 UserHandleCompat user = wm.getUser(info);
3282 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3283 }
3284 // Replace the global list at the very end, so that if there is an exception,
3285 // previously loaded provider list is used.
3286 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003287 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003288 results.addAll(sBgWidgetProviders.values());
3289 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003290 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003291 } catch (Exception e) {
3292 if (e.getCause() instanceof TransactionTooLargeException) {
3293 // the returned value may be incomplete and will not be refreshed until the next
3294 // time Launcher starts.
3295 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3296 // onResume is called to refresh the widget provider list.
3297 synchronized (sBgLock) {
3298 if (sBgWidgetProviders != null) {
3299 results.addAll(sBgWidgetProviders.values());
3300 }
3301 return results;
3302 }
3303 } else {
3304 throw e;
3305 }
Adam Cohen59400422014-03-05 18:07:04 -08003306 }
3307 }
3308
Robin Lee26ace122015-03-16 19:41:43 +00003309 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3310 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003311 synchronized (sBgLock) {
3312 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003313 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003314 }
Robin Lee26ace122015-03-16 19:41:43 +00003315 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003316 }
3317 }
3318
Hyunyoung Song227239e2015-05-04 18:17:35 -07003319 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3320 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003321
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003322 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003323 @Override
3324 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003325 updateWidgetsModel(context, refresh);
3326 final WidgetsModel model = mBgWidgetsModel.clone();
3327
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003328 mHandler.post(new Runnable() {
3329 @Override
3330 public void run() {
3331 Callbacks cb = getCallback();
3332 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003333 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003334 }
3335 }
3336 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003337 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003338 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3339 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003340 }
3341 });
3342 }
3343
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003344 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003345 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003346 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003347 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003348 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003349 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003350 PackageManager packageManager = context.getPackageManager();
3351 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003352 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003353 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3354 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003355 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003356 }
3357
Adam Cohen091440a2015-03-18 14:16:05 -07003358 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003359 UserHandleCompat user) {
3360 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3361 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003362 }
Adam Cohen556f6132014-01-15 15:18:08 -08003363
Kenny Guyed131872014-04-30 03:02:21 +01003364 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3365 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003366 if (cn == null) {
3367 return false;
3368 }
Kenny Guyed131872014-04-30 03:02:21 +01003369 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3370 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003371 return false;
3372 }
Kenny Guyed131872014-04-30 03:02:21 +01003373 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003374 }
3375
Adam Cohena28b78e2014-05-20 17:03:04 -07003376 public static boolean isValidPackage(Context context, String packageName,
3377 UserHandleCompat user) {
3378 if (packageName == null) {
3379 return false;
3380 }
3381 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3382 return launcherApps.isPackageEnabledForProfile(packageName, user);
3383 }
3384
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003386 * Make an ShortcutInfo object for a restored application or shortcut item that points
3387 * to a package that is not yet installed on the system.
3388 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003389 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003390 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003391 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003392 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003393
3394 Bitmap icon = iconInfo.loadIcon(c, info, context);
3395 // the fallback icon
3396 if (icon == null) {
3397 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3398 } else {
3399 info.setIcon(icon);
3400 }
Sunny Goyal34942622014-08-29 17:20:55 -07003401
3402 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003403 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003404 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003405 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003406 }
Sunny Goyal34942622014-08-29 17:20:55 -07003407 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3408 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003409 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003410 }
Sunny Goyal34942622014-08-29 17:20:55 -07003411 } else {
3412 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3413 }
3414
Winson Chung82b016c2015-05-08 17:00:10 -07003415 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003416 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003417 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003418 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003419 return info;
3420 }
3421
3422 /**
3423 * Make an Intent object for a restored application or shortcut item that points
3424 * to the market page for the item.
3425 */
Adam Cohen091440a2015-03-18 14:16:05 -07003426 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003427 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003428 return getMarketIntent(componentName.getPackageName());
3429 }
3430
3431 static Intent getMarketIntent(String packageName) {
3432 return new Intent(Intent.ACTION_VIEW)
3433 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003434 .scheme("market")
3435 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003436 .appendQueryParameter("id", packageName)
3437 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003438 }
3439
3440 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003441 * Make an ShortcutInfo object for a shortcut that is an application.
3442 *
3443 * If c is not null, then it will be used to fill in missing data like the title and icon.
3444 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003445 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003446 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003447 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003448 if (user == null) {
3449 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003450 return null;
3451 }
3452
Kenny Guyed131872014-04-30 03:02:21 +01003453 ComponentName componentName = intent.getComponent();
3454 if (componentName == null) {
3455 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3456 return null;
3457 }
3458
3459 Intent newIntent = new Intent(intent.getAction(), null);
3460 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3461 newIntent.setComponent(componentName);
3462 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003463 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003464 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3465 return null;
3466 }
3467
3468 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003469 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003470 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3471 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3472 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003473 }
3474
Joe Onorato56d82912010-03-07 14:32:10 -05003475 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003476 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003477 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003478 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003479
Joe Onorato56d82912010-03-07 14:32:10 -05003480 // fall back to the class name of the activity
3481 if (info.title == null) {
3482 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003483 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003486 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003487 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003488 if (lai != null) {
3489 info.flags = AppInfo.initFlags(lai);
3490 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003492 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003493
Sunny Goyale2df0622015-04-24 11:27:00 -07003494 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003495 ItemInfoFilter f) {
3496 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3497 for (ItemInfo i : infos) {
3498 if (i instanceof ShortcutInfo) {
3499 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003500 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003501 if (cn != null && f.filterItem(null, info, cn)) {
3502 filtered.add(info);
3503 }
3504 } else if (i instanceof FolderInfo) {
3505 FolderInfo info = (FolderInfo) i;
3506 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003507 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003508 if (cn != null && f.filterItem(info, s, cn)) {
3509 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003510 }
3511 }
Winson Chung64359a52013-07-08 17:17:08 -07003512 } else if (i instanceof LauncherAppWidgetInfo) {
3513 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3514 ComponentName cn = info.providerName;
3515 if (cn != null && f.filterItem(null, info, cn)) {
3516 filtered.add(info);
3517 }
Winson Chung8a435102012-08-30 17:16:53 -07003518 }
3519 }
Winson Chung64359a52013-07-08 17:17:08 -07003520 return new ArrayList<ItemInfo>(filtered);
3521 }
3522
Adam Cohen091440a2015-03-18 14:16:05 -07003523 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003524 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003525 ItemInfoFilter filter = new ItemInfoFilter() {
3526 @Override
3527 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003528 if (info.user == null) {
3529 return cn.equals(cname);
3530 } else {
3531 return cn.equals(cname) && info.user.equals(user);
3532 }
Winson Chung64359a52013-07-08 17:17:08 -07003533 }
3534 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003535 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003536 }
3537
Sunny Goyal1a745e82014-10-02 15:58:31 -07003538 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003539 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 */
Adam Cohen091440a2015-03-18 14:16:05 -07003541 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003542 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003543 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003544 // Non-app shortcuts are only supported for current user.
3545 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003548 // TODO: If there's an explicit component and we can't install that, delete it.
3549
Winson Chung82b016c2015-05-08 17:00:10 -07003550 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003551
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003552 Bitmap icon = iconInfo.loadIcon(c, info, context);
3553 // the fallback icon
3554 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003555 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003556 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003557 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003558 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 return info;
3560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561
Sunny Goyal2350bc92014-10-14 16:42:54 -07003562 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003563 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3564 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3565 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3566
Adam Cohend9198822011-11-22 16:42:47 -08003567 if (intent == null) {
3568 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3569 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3570 return null;
3571 }
3572
Joe Onorato0589f0f2010-02-08 13:44:00 -08003573 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003574 boolean customIcon = false;
3575 ShortcutIconResource iconResource = null;
3576
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003577 if (bitmap instanceof Bitmap) {
3578 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003579 customIcon = true;
3580 } else {
3581 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003582 if (extra instanceof ShortcutIconResource) {
3583 iconResource = (ShortcutIconResource) extra;
3584 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003585 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 }
3587 }
3588
Michael Jurkac9d95c52011-08-29 14:03:34 -07003589 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003590
Kenny Guyed131872014-04-30 03:02:21 +01003591 // Only support intents for current user for now. Intents sent from other
3592 // users wouldn't get here without intent forwarding anyway.
3593 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003594 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003595 icon = mIconCache.getDefaultIcon(info.user);
3596 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003597 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003598 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003599
Winson Chung82b016c2015-05-08 17:00:10 -07003600 info.title = Utilities.trim(name);
3601 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003602 info.intent = intent;
3603 info.customIcon = customIcon;
3604 info.iconResource = iconResource;
3605
3606 return info;
3607 }
3608
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003610 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 * or make a new one.
3612 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003613 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 // See if a placeholder was created for us already
3615 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003616 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003618 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 folders.put(id, folderInfo);
3620 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003621 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003622 }
3623
Joe Onoratobe386092009-11-17 17:32:16 -08003624
Sunny Goyal651077b2014-06-30 14:15:31 -07003625 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3626 return (provider != null) && (provider.provider != null)
3627 && (provider.provider.getPackageName() != null);
3628 }
3629
Joe Onoratobe386092009-11-17 17:32:16 -08003630 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003631 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003632 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3633 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3634 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3635 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003636 if (mLoaderTask != null) {
3637 mLoaderTask.dumpState();
3638 } else {
3639 Log.d(TAG, "mLoaderTask=null");
3640 }
Joe Onoratobe386092009-11-17 17:32:16 -08003641 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003642
3643 public Callbacks getCallback() {
3644 return mCallbacks != null ? mCallbacks.get() : null;
3645 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003646
3647 /**
3648 * @return {@link FolderInfo} if its already loaded.
3649 */
3650 public FolderInfo findFolderById(Long folderId) {
3651 synchronized (sBgLock) {
3652 return sBgFolders.get(folderId);
3653 }
3654 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003655
3656 /**
3657 * @return the looper for the worker thread which can be used to start background tasks.
3658 */
3659 public static Looper getWorkerLooper() {
3660 return sWorkerThread.getLooper();
3661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003662}