blob: 7e75f97936e3d4aaf54d5e2c4070942d8a190873 [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;
Sunny Goyale5bb7052015-07-27 14:36:07 -070030import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070037import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale5bb7052015-07-27 14:36:07 -070059import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
143 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700145 // The lock that must be acquired before referencing any static bg data structures. Unlike
146 // other locks, this one can generally be held long-term because we never expect any of these
147 // static data structures to be referenced outside of the worker thread except on the first
148 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700149 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700153 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
156 // created by LauncherModel that are directly on the home screen (however, no widgets or
157 // shortcuts within folders).
158 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
161 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 new ArrayList<LauncherAppWidgetInfo>();
163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700165 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700166
Adam Cohendcd297f2013-06-18 13:13:40 -0700167 // sBgWorkspaceScreens is the ordered set of workspace screens.
168 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
169
Adam Cohen59400422014-03-05 18:07:04 -0800170 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000171 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
187 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700188 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
189 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700190 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700192 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800193 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700196 public void bindAppsAdded(ArrayList<Long> newScreens,
197 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700198 ArrayList<ItemInfo> addAnimated,
199 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
202 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
203 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700204 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700205 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700206 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700207 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100208 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700209 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700210 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700211 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chung64359a52013-07-08 17:17:08 -0700214 public interface ItemInfoFilter {
215 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
216 }
217
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800219 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400220
Winson Chungee055712013-07-30 14:46:24 -0700221 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700222 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400223 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
224 // resource string.
225 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
226 ProviderInfo providerInfo =
227 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
228 ProviderInfo redirectProvider =
229 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700230
231 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400232 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 if (mOldContentProviderExists) {
235 Log.d(TAG, "Old launcher provider exists.");
236 } else {
237 Log.d(TAG, "Old launcher provider does not exist.");
238 }
239
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100241 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700242 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 mIconCache = iconCache;
244
Kenny Guyed131872014-04-30 03:02:21 +0100245 mLauncherApps = LauncherAppsCompat.getInstance(context);
246 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 }
248
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700249 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
250 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 if (sWorkerThread.getThreadId() == Process.myTid()) {
253 // If we are on the worker thread, post onto the main handler
254 mHandler.post(r);
255 } else {
256 r.run();
257 }
258 }
259
260 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
261 * posted on the worker thread handler. */
262 private static void runOnWorkerThread(Runnable r) {
263 if (sWorkerThread.getThreadId() == Process.myTid()) {
264 r.run();
265 } else {
266 // If we are not on the worker thread, then post to the worker handler
267 sWorker.post(r);
268 }
269 }
270
Sunny Goyal756a28a2015-04-23 17:07:55 -0700271 /**
272 * Runs the specified runnable after the loader is complete
273 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700274 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700275 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
276 synchronized (mBindCompleteRunnables) {
277 mBindCompleteRunnables.add(r);
278 }
279 } else {
280 runOnWorkerThread(r);
281 }
282 }
283
Winson Chunge43a1e72014-01-15 10:33:02 -0800284 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
285 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800286 }
287
Sunny Goyal756adbc2015-04-16 15:20:51 -0700288 public void setPackageState(final PackageInstallInfo installInfo) {
289 Runnable updateRunnable = new Runnable() {
290
291 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500292 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 synchronized (sBgLock) {
294 final HashSet<ItemInfo> updates = new HashSet<>();
295
296 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
297 // Ignore install success events as they are handled by Package add events.
298 return;
299 }
300
Sunny Goyale2df0622015-04-24 11:27:00 -0700301 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700302 if (info instanceof ShortcutInfo) {
303 ShortcutInfo si = (ShortcutInfo) info;
304 ComponentName cn = si.getTargetComponent();
305 if (si.isPromise() && (cn != null)
306 && installInfo.packageName.equals(cn.getPackageName())) {
307 si.setInstallProgress(installInfo.progress);
308
309 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
310 // Mark this info as broken.
311 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
312 }
313 updates.add(si);
314 }
315 }
316 }
317
318 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
319 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
320 widget.installProgress = installInfo.progress;
321 updates.add(widget);
322 }
323 }
324
325 if (!updates.isEmpty()) {
326 // Push changes to the callback.
327 Runnable r = new Runnable() {
328 public void run() {
329 Callbacks callbacks = getCallback();
330 if (callbacks != null) {
331 callbacks.bindRestoreItemsChange(updates);
332 }
333 }
334 };
335 mHandler.post(r);
336 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500337 }
338 }
339 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500341 }
342
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 /**
344 * Updates the icons and label of all pending icons for the provided package name.
345 */
346 public void updateSessionDisplayInfo(final String packageName) {
347 Runnable updateRunnable = new Runnable() {
348
349 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700350 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700351 synchronized (sBgLock) {
352 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
353 final UserHandleCompat user = UserHandleCompat.myUserHandle();
354
Sunny Goyale2df0622015-04-24 11:27:00 -0700355 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700356 if (info instanceof ShortcutInfo) {
357 ShortcutInfo si = (ShortcutInfo) info;
358 ComponentName cn = si.getTargetComponent();
359 if (si.isPromise() && (cn != null)
360 && packageName.equals(cn.getPackageName())) {
361 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
362 // For auto install apps update the icon as well as label.
363 mIconCache.getTitleAndIcon(si,
364 si.promisedIntent, user,
365 si.shouldUseLowResIcon());
366 } else {
367 // Only update the icon for restored apps.
368 si.updateIcon(mIconCache);
369 }
370 updates.add(si);
371 }
372 }
373 }
374
375 if (!updates.isEmpty()) {
376 // Push changes to the callback.
377 Runnable r = new Runnable() {
378 public void run() {
379 Callbacks callbacks = getCallback();
380 if (callbacks != null) {
381 callbacks.bindShortcutsChanged(updates,
382 new ArrayList<ShortcutInfo>(), user);
383 }
384 }
385 };
386 mHandler.post(r);
387 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700388 }
389 }
390 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700391 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700392 }
393
Adam Cohen76a47a12014-02-05 11:47:43 -0800394 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800395 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800396
397 if (allAppsApps == null) {
398 throw new RuntimeException("allAppsApps must not be null");
399 }
400 if (allAppsApps.isEmpty()) {
401 return;
402 }
403
404 // Process the newly added applications and add them to the database first
405 Runnable r = new Runnable() {
406 public void run() {
407 runOnMainThread(new Runnable() {
408 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800409 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500411 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800412 }
413 }
414 });
415 }
416 };
417 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700418 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800419
Sunny Goyala9116722015-04-29 13:55:58 -0700420 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 int[] xy, int spanX, int spanY) {
422 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700423 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
424 final int xCount = (int) profile.numColumns;
425 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 boolean[][] occupied = new boolean[xCount][yCount];
427 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700428 for (ItemInfo r : occupiedPos) {
429 int right = r.cellX + r.spanX;
430 int bottom = r.cellY + r.spanY;
431 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
432 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 occupied[x][y] = true;
434 }
435 }
436 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800437 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700438 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439 }
440
441 /**
442 * Find a position on the screen for the given size or adds a new screen.
443 * @return screenId and the coordinates for the item.
444 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700445 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 ArrayList<Long> workspaceScreens,
448 ArrayList<Long> addedWorkspaceScreensFinal,
449 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700450 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451
Sunny Goyala9116722015-04-29 13:55:58 -0700452 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700453 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700454 synchronized (sBgLock) {
455 for (ItemInfo info : sBgItemsIdMap) {
456 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
457 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
458 if (items == null) {
459 items = new ArrayList<>();
460 screenItems.put(info.screenId, items);
461 }
462 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
466
467 // Find appropriate space for the item.
468 long screenId = 0;
469 int[] cordinates = new int[2];
470 boolean found = false;
471
472 int screenCount = workspaceScreens.size();
473 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700474 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800475 if (preferredScreenIndex < screenCount) {
476 screenId = workspaceScreens.get(preferredScreenIndex);
477 found = findNextAvailableIconSpaceInScreen(
478 screenItems.get(screenId), cordinates, spanX, spanY);
479 }
480
481 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700482 // Search on any of the screens starting from the first screen.
483 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484 screenId = workspaceScreens.get(screen);
485 if (findNextAvailableIconSpaceInScreen(
486 screenItems.get(screenId), cordinates, spanX, spanY)) {
487 // We found a space for it
488 found = true;
489 break;
490 }
491 }
492 }
493
494 if (!found) {
495 // Still no position found. Add a new screen to the end.
496 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
497
498 // Save the screen id for binding in the workspace
499 workspaceScreens.add(screenId);
500 addedWorkspaceScreensFinal.add(screenId);
501
502 // If we still can't find an empty space, then God help us all!!!
503 if (!findNextAvailableIconSpaceInScreen(
504 screenItems.get(screenId), cordinates, spanX, spanY)) {
505 throw new RuntimeException("Can't find space to add the item");
506 }
507 }
508 return Pair.create(screenId, cordinates);
509 }
510
511 /**
512 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800513 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700515 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800517 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700518 return;
Winson Chung997a9232013-07-24 15:33:46 -0700519 }
Winson Chung64359a52013-07-08 17:17:08 -0700520 // Process the newly added applications and add them to the database first
521 Runnable r = new Runnable() {
522 public void run() {
523 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
524 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
525
Winson Chung76828c82013-08-19 15:43:29 -0700526 // Get the list of workspace screens. We need to append to this list and
527 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
528 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800529 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700530 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700532 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700534 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700536 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 }
Winson Chung76828c82013-08-19 15:43:29 -0700538
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700540 Pair<Long, int[]> coords = findSpaceForItem(context,
541 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800542 1, 1);
543 long screenId = coords.first;
544 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700545
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 ItemInfo itemInfo;
547 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
548 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800549 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700551 } else {
552 throw new RuntimeException("Unexpected info type");
553 }
Winson Chung94d67682013-09-25 16:29:40 -0700554
Winson Chung64359a52013-07-08 17:17:08 -0700555 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700556 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700557 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700558 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700559 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700560 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700561 }
562 }
563
Winson Chung76828c82013-08-19 15:43:29 -0700564 // Update the workspace screens
565 updateWorkspaceScreenOrder(context, workspaceScreens);
566
Adam Cohen76a47a12014-02-05 11:47:43 -0800567 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700568 runOnMainThread(new Runnable() {
569 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800570 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700571 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700572 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
573 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700574 if (!addedShortcutsFinal.isEmpty()) {
575 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
576 long lastScreenId = info.screenId;
577 for (ItemInfo i : addedShortcutsFinal) {
578 if (i.screenId == lastScreenId) {
579 addAnimated.add(i);
580 } else {
581 addNotAnimated.add(i);
582 }
Winson Chung997a9232013-07-24 15:33:46 -0700583 }
584 }
Winson Chungd64d1762013-08-20 14:37:16 -0700585 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800586 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700587 }
Winson Chung64359a52013-07-08 17:17:08 -0700588 }
Winson Chung997a9232013-07-24 15:33:46 -0700589 });
590 }
Winson Chung64359a52013-07-08 17:17:08 -0700591 }
592 };
593 runOnWorkerThread(r);
594 }
595
Sunny Goyald33860f2015-04-23 16:02:20 -0700596 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700597 if (sWorkerThread.getThreadId() == Process.myTid()) {
598 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
599 "main thread");
600 }
601
602 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400603 synchronized (mDeferredBindRunnables) {
604 mDeferredBindRunnables.clear();
605 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700606
607 // Remove any queued UI runnables
608 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700609 // Unbind all the workspace items
610 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700611 }
612
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700613 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700614 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700615 // Ensure that we don't use the same workspace items data structure on the main thread
616 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700617 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700618 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700619 tmpItems.addAll(sBgWorkspaceItems);
620 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700621 }
622 Runnable r = new Runnable() {
623 @Override
624 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700625 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 item.unbind();
627 }
628 }
629 };
630 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700631 }
632
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 /**
634 * Adds an item to the DB if it was not created previously, or move it to a new
635 * <container, screen, cellX, cellY>
636 */
637 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 if (item.container == ItemInfo.NO_ID) {
640 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700641 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 } else {
643 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
646 }
647
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700648 static void checkItemInfoLocked(
649 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
650 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
651 if (modelItem != null && item != modelItem) {
652 // check all the data is consistent
653 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
654 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
655 ShortcutInfo shortcut = (ShortcutInfo) item;
656 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
657 modelShortcut.intent.filterEquals(shortcut.intent) &&
658 modelShortcut.id == shortcut.id &&
659 modelShortcut.itemType == shortcut.itemType &&
660 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700662 modelShortcut.cellX == shortcut.cellX &&
663 modelShortcut.cellY == shortcut.cellY &&
664 modelShortcut.spanX == shortcut.spanX &&
665 modelShortcut.spanY == shortcut.spanY &&
666 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
667 (modelShortcut.dropPos != null &&
668 shortcut.dropPos != null &&
669 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
670 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
671 // For all intents and purposes, this is the same object
672 return;
673 }
674 }
675
676 // the modelItem needs to match up perfectly with item if our model is
677 // to be consistent with the database-- for now, just require
678 // modelItem == item or the equality check above
679 String msg = "item: " + ((item != null) ? item.toString() : "null") +
680 "modelItem: " +
681 ((modelItem != null) ? modelItem.toString() : "null") +
682 "Error: ItemInfo passed to checkItemInfo doesn't match original";
683 RuntimeException e = new RuntimeException(msg);
684 if (stackTrace != null) {
685 e.setStackTrace(stackTrace);
686 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800687 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700688 }
689 }
690
Michael Jurka816474f2012-06-25 14:49:02 -0700691 static void checkItemInfo(final ItemInfo item) {
692 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
693 final long itemId = item.id;
694 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700695 public void run() {
696 synchronized (sBgLock) {
697 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700698 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700699 }
700 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700701 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700702 }
703
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
705 final ItemInfo item, final String callingFunction) {
706 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700707 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700708 final ContentResolver cr = context.getContentResolver();
709
Adam Cohen487f7dd2012-06-28 18:12:10 -0700710 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700711 Runnable r = new Runnable() {
712 public void run() {
713 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 updateItemArrays(item, itemId, stackTrace);
715 }
716 };
717 runOnWorkerThread(r);
718 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700720 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
721 final ArrayList<ItemInfo> items, final String callingFunction) {
722 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700723
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700724 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
725 Runnable r = new Runnable() {
726 public void run() {
727 ArrayList<ContentProviderOperation> ops =
728 new ArrayList<ContentProviderOperation>();
729 int count = items.size();
730 for (int i = 0; i < count; i++) {
731 ItemInfo item = items.get(i);
732 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700733 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700735
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
737 updateItemArrays(item, itemId, stackTrace);
738
739 }
740 try {
741 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
742 } catch (Exception e) {
743 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 }
745 }
746 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700747 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700748 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700749
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
751 // Lock on mBgLock *after* the db operation
752 synchronized (sBgLock) {
753 checkItemInfoLocked(itemId, item, stackTrace);
754
755 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
756 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
757 // Item is in a folder, make sure this folder exists
758 if (!sBgFolders.containsKey(item.container)) {
759 // An items container is being set to a that of an item which is not in
760 // the list of Folders.
761 String msg = "item: " + item + " container being set to: " +
762 item.container + ", not in the list of folders";
763 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 }
765 }
766
767 // Items are added/removed from the corresponding FolderInfo elsewhere, such
768 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
769 // that are on the desktop, as appropriate
770 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800771 if (modelItem != null &&
772 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
773 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774 switch (modelItem.itemType) {
775 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
776 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
777 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
778 if (!sBgWorkspaceItems.contains(modelItem)) {
779 sBgWorkspaceItems.add(modelItem);
780 }
781 break;
782 default:
783 break;
784 }
785 } else {
786 sBgWorkspaceItems.remove(modelItem);
787 }
788 }
789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700793 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700794 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 item.cellX = cellX;
798 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700799
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 // We store hotseat items in canonical form which is this orientation invariant position
801 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700802 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700804 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
809 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700811 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
812 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800813 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700814 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700817 }
818
819 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
821 * cellX, cellY have already been updated on the ItemInfos.
822 */
823 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
824 final long container, final int screen) {
825
826 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
827 int count = items.size();
828
829 for (int i = 0; i < count; i++) {
830 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 item.container = container;
832
833 // We store hotseat items in canonical form which is this orientation invariant position
834 // in the hotseat
835 if (context instanceof Launcher && screen < 0 &&
836 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700837 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700838 item.cellY);
839 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700841 }
842
843 final ContentValues values = new ContentValues();
844 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
845 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
846 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800847 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700849
850 contentValues.add(values);
851 }
852 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
853 }
854
855 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800857 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700859 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700860 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800861 item.cellX = cellX;
862 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 item.spanX = spanX;
864 item.spanY = spanY;
865
866 // We store hotseat items in canonical form which is this orientation invariant position
867 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700869 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 }
Adam Cohend4844c32011-02-18 19:25:06 -0800874
Adam Cohend4844c32011-02-18 19:25:06 -0800875 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800876 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700877 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
878 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800879 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700880 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
881 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700882 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800883
Michael Jurka816474f2012-06-25 14:49:02 -0700884 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700885 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886
887 /**
888 * Update an item to the database in a specified container.
889 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700890 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700891 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100892 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700893 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800894 }
895
Sunny Goyal756a28a2015-04-23 17:07:55 -0700896 private void assertWorkspaceLoaded() {
897 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
898 throw new RuntimeException("Trying to add shortcut while loader is running");
899 }
900 }
901
Adam Cohend4844c32011-02-18 19:25:06 -0800902 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700903 * Returns true if the shortcuts already exists on the workspace. This must be called after
904 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700906 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
907 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700909 if (intent.getComponent() != null) {
910 // If component is not null, an intent with null package will produce
911 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700912 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700913 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700914 intentWithPkg = intent.toUri(0);
915 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700916 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700917 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
918 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700919 }
920 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700921 intentWithPkg = intent.toUri(0);
922 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700923 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700924
925 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700926 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 if (item instanceof ShortcutInfo) {
928 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700929 Intent targetIntent = info.promisedIntent == null
930 ? info.intent : info.promisedIntent;
931 if (targetIntent != null && info.user.equals(user)) {
932 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700933 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
934 return true;
935 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700936 }
937 }
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700940 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700941 }
942
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 /**
944 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
945 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700946 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 final ContentResolver cr = context.getContentResolver();
948 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
949 "_id=? and (itemType=? or itemType=?)",
950 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700951 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700952
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 try {
954 if (c.moveToFirst()) {
955 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
956 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
957 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
958 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
959 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
960 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700961 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 FolderInfo folderInfo = null;
964 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700965 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
966 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700968 }
969
Sunny Goyala508e4f2015-05-21 09:33:57 -0700970 // Do not trim the folder label, as is was set by the user.
971 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 folderInfo.id = id;
973 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700975 folderInfo.cellX = c.getInt(cellXIndex);
976 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700977 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978
979 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 } finally {
982 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700983 }
984
985 return null;
986 }
987
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 /**
989 * Add an item to the database in a specified container. Sets the container, screen, cellX and
990 * cellY fields of the item. Also assigns an ID to the item.
991 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700992 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700993 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 item.cellX = cellX;
996 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 // We store hotseat items in canonical form which is this orientation invariant position
998 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005
1006 final ContentValues values = new ContentValues();
1007 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001008 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009
Michael Jurka414300a2013-08-27 15:42:35 +02001010 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001011 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001012
Jason Monk8e19cf22014-03-20 15:06:57 -04001013 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001014 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001016 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001019 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001020 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 sBgItemsIdMap.put(item.id, item);
1022 switch (item.itemType) {
1023 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1024 sBgFolders.put(item.id, (FolderInfo) item);
1025 // Fall through
1026 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1027 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1028 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1029 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1030 sBgWorkspaceItems.add(item);
1031 } else {
1032 if (!sBgFolders.containsKey(item.container)) {
1033 // Adding an item to a folder that doesn't exist.
1034 String msg = "adding item: " + item + " to a folder that " +
1035 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001036 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001038 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 break;
1040 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1041 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1042 break;
1043 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001044 }
1045 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001046 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001048 }
1049
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001051 * Creates a new unique child id, for a given cell span across all layouts.
1052 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001053 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001057 }
1058
Sunny Goyal34942622014-08-29 17:20:55 -07001059 private static ArrayList<ItemInfo> getItemsByPackageName(
1060 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001061 ItemInfoFilter filter = new ItemInfoFilter() {
1062 @Override
1063 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1064 return cn.getPackageName().equals(pn) && info.user.equals(user);
1065 }
1066 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001067 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001068 }
1069
1070 /**
1071 * Removes all the items from the database corresponding to the specified package.
1072 */
1073 static void deletePackageFromDatabase(Context context, final String pn,
1074 final UserHandleCompat user) {
1075 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001076 }
1077
1078 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001079 * Removes the specified item from the database
1080 * @param context
1081 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001083 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001084 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1085 items.add(item);
1086 deleteItemsFromDatabase(context, items);
1087 }
1088
1089 /**
1090 * Removes the specified items from the database
1091 * @param context
1092 * @param item
1093 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001094 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001096 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001097 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001098 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001099 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001101
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001102 // Lock on mBgLock *after* the db operation
1103 synchronized (sBgLock) {
1104 switch (item.itemType) {
1105 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1106 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001107 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001108 if (info.container == item.id) {
1109 // We are deleting a folder which still contains items that
1110 // think they are contained by that folder.
1111 String msg = "deleting a folder (" + item + ") which still " +
1112 "contains items (" + info + ")";
1113 Log.e(TAG, msg);
1114 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001116 sBgWorkspaceItems.remove(item);
1117 break;
1118 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1119 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1120 sBgWorkspaceItems.remove(item);
1121 break;
1122 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1123 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1124 break;
1125 }
1126 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001128 }
1129 }
Michael Jurka83df1882011-08-31 20:59:26 -07001130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 * Update the order of the workspace screens in the database. The array list contains
1136 * a list of screen ids in the order that they should appear.
1137 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001138 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001139 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001140 final ContentResolver cr = context.getContentResolver();
1141 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1142
1143 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001144 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 while (iter.hasNext()) {
1146 long id = iter.next();
1147 if (id < 0) {
1148 iter.remove();
1149 }
1150 }
1151
1152 Runnable r = new Runnable() {
1153 @Override
1154 public void run() {
Yura085c8532014-02-11 15:15:29 +00001155 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001156 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001157 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001158 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001159 for (int i = 0; i < count; i++) {
1160 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001161 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1163 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001164 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001165 }
Yura085c8532014-02-11 15:15:29 +00001166
1167 try {
1168 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1169 } catch (Exception ex) {
1170 throw new RuntimeException(ex);
1171 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001172
Winson Chungba9c37f2013-08-30 14:11:37 -07001173 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001174 sBgWorkspaceScreens.clear();
1175 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001176 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001177 }
1178 };
1179 runOnWorkerThread(r);
1180 }
1181
1182 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 * Remove the contents of the specified folder from the database
1184 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001185 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 final ContentResolver cr = context.getContentResolver();
1187
Michael Jurkac9d95c52011-08-29 14:03:34 -07001188 Runnable r = new Runnable() {
1189 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001190 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001192 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 sBgItemsIdMap.remove(info.id);
1194 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 sBgWorkspaceItems.remove(info);
1196 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001197
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001198 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001199 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001201 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 for (ItemInfo childInfo : info.contents) {
1203 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001205 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001206 }
1207 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 }
1210
1211 /**
1212 * Set this as the current Launcher activity object for the loader.
1213 */
1214 public void initialize(Callbacks callbacks) {
1215 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001216 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1217 // workspace to prevent leaking Launcher activities on orientation change.
1218 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 mCallbacks = new WeakReference<Callbacks>(callbacks);
1220 }
1221 }
1222
Kenny Guyed131872014-04-30 03:02:21 +01001223 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001224 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001225 int op = PackageUpdatedTask.OP_UPDATE;
1226 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1227 user));
1228 }
1229
1230 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001231 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001232 int op = PackageUpdatedTask.OP_REMOVE;
1233 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1234 user));
1235 }
1236
1237 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001238 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001239 int op = PackageUpdatedTask.OP_ADD;
1240 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1241 user));
1242 }
1243
1244 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001245 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001246 boolean replacing) {
1247 if (!replacing) {
1248 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1249 user));
1250 if (mAppsCanBeOnRemoveableStorage) {
1251 // Only rebind if we support removable storage. It catches the
1252 // case where
1253 // apps on the external sd card need to be reloaded
1254 startLoaderFromBackground();
1255 }
1256 } else {
1257 // If we are replacing then just update the packages in the list
1258 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1259 packageNames, user));
1260 }
1261 }
1262
1263 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001264 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001265 boolean replacing) {
1266 if (!replacing) {
1267 enqueuePackageUpdated(new PackageUpdatedTask(
1268 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1269 user));
1270 }
Kenny Guyed131872014-04-30 03:02:21 +01001271 }
1272
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001273 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001274 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1275 * ACTION_PACKAGE_CHANGED.
1276 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001277 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001278 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001279 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001280
Joe Onorato36115782010-06-17 13:28:48 -04001281 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001282 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001283 // If we have changed locale we need to clear out the labels in all apps/workspace.
1284 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001285 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1286 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001287 Callbacks callbacks = getCallback();
1288 if (callbacks != null) {
1289 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001290 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001291 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1292 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1293 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001294 }
1295 }
1296
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001297 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001298 resetLoadedState(true, true);
1299
Reena Lee93f824a2011-09-23 17:20:28 -07001300 // Do this here because if the launcher activity is running it will be restarted.
1301 // If it's not running startLoaderFromBackground will merely tell it that it needs
1302 // to reload.
1303 startLoaderFromBackground();
1304 }
1305
Winson Chungf0c6ae02012-03-21 16:10:31 -07001306 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1307 synchronized (mLock) {
1308 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1309 // mWorkspaceLoaded to true later
1310 stopLoaderLocked();
1311 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1312 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1313 }
1314 }
1315
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001316 /**
1317 * When the launcher is in the background, it's possible for it to miss paired
1318 * configuration changes. So whenever we trigger the loader from the background
1319 * tell the launcher that it needs to re-run the loader when it comes back instead
1320 * of doing it now.
1321 */
1322 public void startLoaderFromBackground() {
1323 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001324 Callbacks callbacks = getCallback();
1325 if (callbacks != null) {
1326 // Only actually run the loader if they're not paused.
1327 if (!callbacks.setLoadOnResume()) {
1328 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001329 }
1330 }
1331 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001332 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001333 }
Joe Onorato36115782010-06-17 13:28:48 -04001334 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001335
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001336 /**
1337 * If there is already a loader task running, tell it to stop.
1338 */
1339 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001340 LoaderTask oldTask = mLoaderTask;
1341 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001342 oldTask.stopLocked();
1343 }
Reena Lee93f824a2011-09-23 17:20:28 -07001344 }
1345
Adam Cohen1a85c582014-09-30 09:48:49 -07001346 public boolean isCurrentCallbacks(Callbacks callbacks) {
1347 return (mCallbacks != null && mCallbacks.get() == callbacks);
1348 }
1349
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001350 public void startLoader(int synchronousBindPage) {
1351 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001352 }
1353
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001354 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001355 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1356 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001357 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 // Clear any deferred bind-runnables from the synchronized load process
1359 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001360 synchronized (mDeferredBindRunnables) {
1361 mDeferredBindRunnables.clear();
1362 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363
Joe Onorato36115782010-06-17 13:28:48 -04001364 // Don't bother to start the thread if we know it's not going to do anything
1365 if (mCallbacks != null && mCallbacks.get() != null) {
1366 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001367 stopLoaderLocked();
1368 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001369 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001370 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1372 } else {
1373 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1374 sWorker.post(mLoaderTask);
1375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378 }
1379
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 void bindRemainingSynchronousPages() {
1381 // Post the remaining side pages to be loaded
1382 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001383 Runnable[] deferredBindRunnables = null;
1384 synchronized (mDeferredBindRunnables) {
1385 deferredBindRunnables = mDeferredBindRunnables.toArray(
1386 new Runnable[mDeferredBindRunnables.size()]);
1387 mDeferredBindRunnables.clear();
1388 }
1389 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001390 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001393
1394 // Run all the bind complete runnables after workspace is bound.
1395 if (!mBindCompleteRunnables.isEmpty()) {
1396 synchronized (mBindCompleteRunnables) {
1397 for (final Runnable r : mBindCompleteRunnables) {
1398 runOnWorkerThread(r);
1399 }
1400 mBindCompleteRunnables.clear();
1401 }
1402 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001403 }
1404
Joe Onorato36115782010-06-17 13:28:48 -04001405 public void stopLoader() {
1406 synchronized (mLock) {
1407 if (mLoaderTask != null) {
1408 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001409 }
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001413 /**
1414 * Loads the workspace screen ids in an ordered list.
1415 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001416 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001417 final ContentResolver contentResolver = context.getContentResolver();
1418 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001419
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001420 // Get screens ordered by rank.
1421 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1422 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1423 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001424 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001425 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001426 while (sc.moveToNext()) {
1427 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001428 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001429 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1431 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001432 }
1433 }
1434 } finally {
1435 sc.close();
1436 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001437 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001438 }
1439
Michael Jurkac57b7a82011-08-09 22:02:20 -07001440 public boolean isAllAppsLoaded() {
1441 return mAllAppsLoaded;
1442 }
1443
Winson Chung36a62fe2012-05-06 18:04:42 -07001444 boolean isLoadingWorkspace() {
1445 synchronized (mLock) {
1446 if (mLoaderTask != null) {
1447 return mLoaderTask.isLoadingWorkspace();
1448 }
1449 }
1450 return false;
1451 }
1452
Joe Onorato36115782010-06-17 13:28:48 -04001453 /**
1454 * Runnable for the thread that loads the contents of the launcher:
1455 * - workspace icons
1456 * - widgets
1457 * - all apps icons
1458 */
1459 private class LoaderTask implements Runnable {
1460 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001461 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001462 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001463 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001464 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001466 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001467 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001468 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 }
1470
Winson Chung36a62fe2012-05-06 18:04:42 -07001471 boolean isLoadingWorkspace() {
1472 return mIsLoadingAndBindingWorkspace;
1473 }
1474
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001475 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001476 mIsLoadingAndBindingWorkspace = true;
1477
Joe Onorato36115782010-06-17 13:28:48 -04001478 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001479 if (DEBUG_LOADERS) {
1480 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001482
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001483 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001484 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001485 synchronized (LoaderTask.this) {
1486 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001487 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001488 }
1489 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
1491 }
1492
Joe Onorato36115782010-06-17 13:28:48 -04001493 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001494 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001495 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001496
Joe Onorato36115782010-06-17 13:28:48 -04001497 private void waitForIdle() {
1498 // Wait until the either we're stopped or the other threads are done.
1499 // This way we don't start loading all apps until the workspace has settled
1500 // down.
1501 synchronized (LoaderTask.this) {
1502 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001503
Joe Onorato36115782010-06-17 13:28:48 -04001504 mHandler.postIdle(new Runnable() {
1505 public void run() {
1506 synchronized (LoaderTask.this) {
1507 mLoadAndBindStepFinished = true;
1508 if (DEBUG_LOADERS) {
1509 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001510 }
Joe Onorato36115782010-06-17 13:28:48 -04001511 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001512 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514 });
1515
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001516 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001517 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001518 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1519 // wait no longer than 1sec at a time
1520 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001521 } catch (InterruptedException ex) {
1522 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001523 }
1524 }
Joe Onorato36115782010-06-17 13:28:48 -04001525 if (DEBUG_LOADERS) {
1526 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001527 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001528 + "ms for previous step to finish binding");
1529 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001532
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001534 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001535 // Ensure that we have a valid page index to load synchronously
1536 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1537 "valid page index");
1538 }
1539 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1540 // Ensure that we don't try and bind a specified page when the pages have not been
1541 // loaded already (we should load everything asynchronously in that case)
1542 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1543 }
1544 synchronized (mLock) {
1545 if (mIsLoaderTaskRunning) {
1546 // Ensure that we are never running the background loading at this point since
1547 // we also touch the background collections
1548 throw new RuntimeException("Error! Background loading is already running");
1549 }
1550 }
1551
1552 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1553 // data structures, we can't allow any other thread to touch that data, but because
1554 // this call is synchronous, we can get away with not locking).
1555
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001556 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001557 // operations from the previous activity. We need to ensure that all queued operations
1558 // are executed before any synchronous binding work is done.
1559 mHandler.flush();
1560
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561 // Divide the set of loaded items into those that we are binding synchronously, and
1562 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001563 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001564 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1565 // arise from that.
1566 onlyBindAllApps();
1567 }
1568
Joe Onorato36115782010-06-17 13:28:48 -04001569 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001570 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001571 if (mStopped) {
1572 return;
1573 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574 mIsLoaderTaskRunning = true;
1575 }
Joe Onorato36115782010-06-17 13:28:48 -04001576 // Optimize for end-user experience: if the Launcher is up and // running with the
1577 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1578 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001579 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001580 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001581 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 if (mStopped) {
1584 break keep_running;
1585 }
1586
Joe Onorato36115782010-06-17 13:28:48 -04001587 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001588
1589 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001590 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1591 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001592 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001593
Joe Onorato36115782010-06-17 13:28:48 -04001594 // Clear out this reference, otherwise we end up holding it until all of the
1595 // callback runnables are done.
1596 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597
Joe Onorato36115782010-06-17 13:28:48 -04001598 synchronized (mLock) {
1599 // If we are still the last one to be scheduled, remove ourselves.
1600 if (mLoaderTask == this) {
1601 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001603 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001604 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
Joe Onorato36115782010-06-17 13:28:48 -04001606 }
1607
1608 public void stopLocked() {
1609 synchronized (LoaderTask.this) {
1610 mStopped = true;
1611 this.notify();
1612 }
1613 }
1614
1615 /**
1616 * Gets the callbacks object. If we've been stopped, or if the launcher object
1617 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1618 * object that was around when the deferred message was scheduled, and if there's
1619 * a new Callbacks object around then also return null. This will save us from
1620 * calling onto it with data that will be ignored.
1621 */
1622 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1623 synchronized (mLock) {
1624 if (mStopped) {
1625 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001626 }
Joe Onorato36115782010-06-17 13:28:48 -04001627
1628 if (mCallbacks == null) {
1629 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001630 }
Joe Onorato36115782010-06-17 13:28:48 -04001631
1632 final Callbacks callbacks = mCallbacks.get();
1633 if (callbacks != oldCallbacks) {
1634 return null;
1635 }
1636 if (callbacks == null) {
1637 Log.w(TAG, "no mCallbacks");
1638 return null;
1639 }
1640
1641 return callbacks;
1642 }
1643 }
1644
1645 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001646 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001647 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001648 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1649 final int countX = (int) profile.numColumns;
1650 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001651
Adam Cohendcd297f2013-06-18 13:13:40 -07001652 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001653 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001654 // Return early if we detect that an item is under the hotseat button
1655 if (mCallbacks == null ||
1656 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001657 Log.e(TAG, "Error loading shortcut into hotseat " + item
1658 + " into position (" + item.screenId + ":" + item.cellX + ","
1659 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001660 return false;
1661 }
1662
Dan Sandler295ae182013-12-10 16:05:47 -05001663 final ItemInfo[][] hotseatItems =
1664 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1665
Adam Cohen2e6da152015-05-06 11:42:25 -07001666 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001667 Log.e(TAG, "Error loading shortcut " + item
1668 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001669 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001670 + ")");
1671 return false;
1672 }
1673
Dan Sandler295ae182013-12-10 16:05:47 -05001674 if (hotseatItems != null) {
1675 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001676 Log.e(TAG, "Error loading shortcut into hotseat " + item
1677 + " into position (" + item.screenId + ":" + item.cellX + ","
1678 + item.cellY + ") occupied by "
1679 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1680 [(int) item.screenId][0]);
1681 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001682 } else {
1683 hotseatItems[(int) item.screenId][0] = item;
1684 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001685 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001686 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001687 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001688 items[(int) item.screenId][0] = item;
1689 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001690 return true;
1691 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001692 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1693 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001694 return true;
1695 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001696
Adam Cohendcd297f2013-06-18 13:13:40 -07001697 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001698 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001699 occupied.put(item.screenId, items);
1700 }
1701
Dan Sandler295ae182013-12-10 16:05:47 -05001702 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001703 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1704 item.cellX < 0 || item.cellY < 0 ||
1705 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1706 Log.e(TAG, "Error loading shortcut " + item
1707 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1708 + item.cellX + "," + item.cellY
1709 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1710 return false;
1711 }
1712
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001713 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001714 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1715 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001717 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001718 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001719 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001720 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001722 return false;
1723 }
1724 }
1725 }
1726 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1727 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001728 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001729 }
1730 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001731
Joe Onorato36115782010-06-17 13:28:48 -04001732 return true;
1733 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734
Winson Chungba9c37f2013-08-30 14:11:37 -07001735 /** Clears all the sBg data structures */
1736 private void clearSBgDataStructures() {
1737 synchronized (sBgLock) {
1738 sBgWorkspaceItems.clear();
1739 sBgAppWidgets.clear();
1740 sBgFolders.clear();
1741 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001742 sBgWorkspaceScreens.clear();
1743 }
1744 }
1745
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001746 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001747 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748
Joe Onorato36115782010-06-17 13:28:48 -04001749 final Context context = mContext;
1750 final ContentResolver contentResolver = context.getContentResolver();
1751 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001752 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001753 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1754 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001755 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001756
Winson Chung892c74d2013-08-22 16:15:50 -07001757 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001758 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1759 int countX = (int) profile.numColumns;
1760 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001761
Sunny Goyale5bb7052015-07-27 14:36:07 -07001762
1763 if (MigrateFromRestoreTask.shouldRunTask(mContext)) {
1764 try {
1765 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1766 // Clear the flags before starting the task, so that we do not run the task
1767 // again, in case there was an uncaught error.
1768 MigrateFromRestoreTask.clearFlags(mContext);
1769 task.execute();
1770 } catch (Exception e) {
1771 Log.e(TAG, "Error during grid migration", e);
1772
1773 // Clear workspace.
1774 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1775 }
1776 }
1777
Dan Sandlerd5024042014-01-09 15:01:33 -05001778 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1779 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1780 LauncherAppState.getLauncherProvider().deleteDatabase();
1781 }
1782
1783 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1784 // append the user's Launcher2 shortcuts
1785 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1786 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1787 } else {
1788 // Make sure the default workspace is loaded
1789 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001790 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001791 }
Adam Cohene25af792013-06-06 23:08:25 -07001792
Winson Chung2abf94d2012-07-18 18:16:38 -07001793 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001794 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001795 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001796 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001797
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001799 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001800 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001801 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001802 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001803
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001804 // +1 for the hotseat (it can be larger than the workspace)
1805 // Load workspace in reverse order to ensure that latest items are loaded first (and
1806 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001807 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 try {
1810 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1811 final int intentIndex = c.getColumnIndexOrThrow
1812 (LauncherSettings.Favorites.INTENT);
1813 final int titleIndex = c.getColumnIndexOrThrow
1814 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815 final int containerIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.CONTAINER);
1817 final int itemTypeIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.ITEM_TYPE);
1819 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001821 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 final int screenIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.SCREEN);
1825 final int cellXIndex = c.getColumnIndexOrThrow
1826 (LauncherSettings.Favorites.CELLX);
1827 final int cellYIndex = c.getColumnIndexOrThrow
1828 (LauncherSettings.Favorites.CELLY);
1829 final int spanXIndex = c.getColumnIndexOrThrow
1830 (LauncherSettings.Favorites.SPANX);
1831 final int spanYIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001833 final int rankIndex = c.getColumnIndexOrThrow(
1834 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001835 final int restoredIndex = c.getColumnIndexOrThrow(
1836 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001837 final int profileIdIndex = c.getColumnIndexOrThrow(
1838 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001839 final int optionsIndex = c.getColumnIndexOrThrow(
1840 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001841 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842
Sunny Goyal7f834d22015-04-21 10:10:23 -07001843 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1844 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1845 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1846 }
1847
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 ShortcutInfo info;
1849 String intentDescription;
1850 LauncherAppWidgetInfo appWidgetInfo;
1851 int container;
1852 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001853 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001855 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001856
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 while (!mStopped && c.moveToNext()) {
1858 try {
1859 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001860 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001861 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001862 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 switch (itemType) {
1865 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1866 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001867 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001869 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001870 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001871 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001872 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001873 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001874 if (user == null) {
1875 // User has been deleted remove the item.
1876 itemsToRemove.add(id);
1877 continue;
1878 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879 try {
1880 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001881 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001882 if (cn != null && cn.getPackageName() != null) {
1883 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1884 cn.getPackageName(), user);
1885 boolean validComponent = validPkg &&
1886 launcherApps.isActivityEnabledForProfile(cn, user);
1887
1888 if (validComponent) {
1889 if (restored) {
1890 // no special handling necessary for this item
1891 restoredRows.add(id);
1892 restored = false;
1893 }
1894 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001895 intent = null;
1896 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1897 // We allow auto install apps to have their intent
1898 // updated after an install.
1899 intent = manager.getLaunchIntentForPackage(
1900 cn.getPackageName());
1901 if (intent != null) {
1902 ContentValues values = new ContentValues();
1903 values.put(LauncherSettings.Favorites.INTENT,
1904 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001905 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001906 }
1907 }
1908
1909 if (intent == null) {
1910 // The app is installed but the component is no
1911 // longer available.
1912 Launcher.addDumpLog(TAG,
1913 "Invalid component removed: " + cn, true);
1914 itemsToRemove.add(id);
1915 continue;
1916 } else {
1917 // no special handling necessary for this item
1918 restoredRows.add(id);
1919 restored = false;
1920 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001921 } else if (restored) {
1922 // Package is not yet available but might be
1923 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001924 Launcher.addDumpLog(TAG,
1925 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001926
1927 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1928 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001929 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001930 // App restore has started. Update the flag
1931 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1932 ContentValues values = new ContentValues();
1933 values.put(LauncherSettings.Favorites.RESTORED,
1934 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001935 updateItem(id, values);
1936 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1937 // This is a common app. Try to replace this.
1938 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1939 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1940 if (parser.findDefaultApp()) {
1941 // Default app found. Replace it.
1942 intent = parser.parsedIntent;
1943 cn = intent.getComponent();
1944 ContentValues values = parser.parsedValues;
1945 values.put(LauncherSettings.Favorites.RESTORED, 0);
1946 updateItem(id, values);
1947 restored = false;
1948 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001949
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001950 } else if (REMOVE_UNRESTORED_ICONS) {
1951 Launcher.addDumpLog(TAG,
1952 "Unrestored package removed: " + cn, true);
1953 itemsToRemove.add(id);
1954 continue;
1955 }
Sunny Goyal94485362014-09-18 16:13:58 -07001956 } else if (REMOVE_UNRESTORED_ICONS) {
1957 Launcher.addDumpLog(TAG,
1958 "Unrestored package removed: " + cn, true);
1959 itemsToRemove.add(id);
1960 continue;
1961 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001962 } else if (launcherApps.isAppEnabled(
1963 manager, cn.getPackageName(),
1964 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1965 // Package is present but not available.
1966 allowMissingTarget = true;
1967 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1968 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001969 // SdCard is not ready yet. Package might get available,
1970 // once it is ready.
1971 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1972 + " (check again later)", true);
1973 HashSet<String> pkgs = sPendingPackages.get(user);
1974 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001975 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001976 sPendingPackages.put(user, pkgs);
1977 }
1978 pkgs.add(cn.getPackageName());
1979 allowMissingTarget = true;
1980 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001981
1982 } else {
1983 // Do not wait for external media load anymore.
1984 // Log the invalid package, and remove it
1985 Launcher.addDumpLog(TAG,
1986 "Invalid package removed: " + cn, true);
1987 itemsToRemove.add(id);
1988 continue;
Winson Chungee055712013-07-30 14:46:24 -07001989 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001990 } else if (cn == null) {
1991 // For shortcuts with no component, keep them as they are
1992 restoredRows.add(id);
1993 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001994 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001995 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001996 Launcher.addDumpLog(TAG,
1997 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 continue;
1999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000
Sunny Goyal34b65272015-03-11 16:56:52 -07002001 boolean useLowResIcon = container >= 0 &&
2002 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2003
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002004 if (itemReplaced) {
2005 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002006 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002007 cursorIconInfo.iconIndex, titleIndex,
2008 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002009 } else {
2010 // Don't replace items for other profiles.
2011 itemsToRemove.add(id);
2012 continue;
2013 }
2014 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002015 if (user.equals(UserHandleCompat.myUserHandle())) {
2016 Launcher.addDumpLog(TAG,
2017 "constructing info for partially restored package",
2018 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002019 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002020 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002021 intent = getRestoredItemIntent(c, context, intent);
2022 } else {
2023 // Don't restore items for other profiles.
2024 itemsToRemove.add(id);
2025 continue;
2026 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002027 } else if (itemType ==
2028 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002029 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002030 cursorIconInfo.iconIndex, titleIndex,
2031 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002033 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002034
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 // App shortcuts that used to be automatically added to Launcher
2036 // didn't always have the correct intent flags set, so do that
2037 // here
2038 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002039 intent.getCategories() != null &&
2040 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002041 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 intent.addFlags(
2043 Intent.FLAG_ACTIVITY_NEW_TASK |
2044 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2045 }
Michael Jurka96879562012-03-22 05:54:33 -07002046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002049 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002052 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 info.cellX = c.getInt(cellXIndex);
2054 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002055 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002056 info.spanX = 1;
2057 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002058 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002059 if (info.promisedIntent != null) {
2060 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2061 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002062 info.isDisabled = disabledState;
2063 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2064 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2065 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002066
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002068 if (!checkItemPlacement(occupied, info)) {
2069 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 break;
2071 }
2072
Sunny Goyal756adbc2015-04-16 15:20:51 -07002073 if (restored) {
2074 ComponentName cn = info.getTargetComponent();
2075 if (cn != null) {
2076 Integer progress = installingPkgs.get(cn.getPackageName());
2077 if (progress != null) {
2078 info.setInstallProgress(progress);
2079 } else {
2080 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2081 }
2082 }
2083 }
2084
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 switch (container) {
2086 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2087 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2088 sBgWorkspaceItems.add(info);
2089 break;
2090 default:
2091 // Item is in a user folder
2092 FolderInfo folderInfo =
2093 findOrMakeFolder(sBgFolders, container);
2094 folderInfo.add(info);
2095 break;
2096 }
2097 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002098 } else {
2099 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 }
2101 break;
2102
2103 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2104 id = c.getLong(idIndex);
2105 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2106
Sunny Goyala508e4f2015-05-21 09:33:57 -07002107 // Do not trim the folder label, as is was set by the user.
2108 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002111 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 folderInfo.cellX = c.getInt(cellXIndex);
2113 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002114 folderInfo.spanX = 1;
2115 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002116 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117
Daniel Sandler8802e962010-05-26 16:28:16 -04002118 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002119 if (!checkItemPlacement(occupied, folderInfo)) {
2120 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002121 break;
2122 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002123
Joe Onorato9c1289c2009-08-17 11:03:03 -04002124 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2126 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2127 sBgWorkspaceItems.add(folderInfo);
2128 break;
Joe Onorato36115782010-06-17 13:28:48 -04002129 }
Joe Onorato17a89222011-02-08 17:26:11 -08002130
Chris Wrenf4d08112014-01-16 18:13:56 -05002131 if (restored) {
2132 // no special handling required for restored folders
2133 restoredRows.add(id);
2134 }
2135
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2137 sBgFolders.put(folderInfo.id, folderInfo);
2138 break;
2139
2140 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002141 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002143 boolean customWidget = itemType ==
2144 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2145
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002146 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002147 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002148 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002149 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002150 user = allUsers.get(serialNumber);
2151 if (user == null) {
2152 itemsToRemove.add(id);
2153 continue;
2154 }
2155
Sunny Goyalff572272014-07-23 13:58:07 -07002156 final ComponentName component =
2157 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002158
Sunny Goyal651077b2014-06-30 14:15:31 -07002159 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002160 final boolean isIdValid = (restoreStatus &
2161 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002162 final boolean wasProviderReady = (restoreStatus &
2163 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002164
Adam Cohen59400422014-03-05 18:07:04 -08002165 final LauncherAppWidgetProviderInfo provider =
2166 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002167 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002168 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002169
2170 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002171 if (!isSafeMode && !customWidget &&
2172 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002173 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002174 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002175
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002177 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 itemsToRemove.add(id);
2179 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002180 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2182 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002183
Sunny Goyal53f96722015-07-13 19:54:53 -07002184 // The provider is available. So the widget is either
2185 // available or not available. We do not need to track
2186 // any future restore updates.
2187 int status = restoreStatus &
2188 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002189 if (!wasProviderReady) {
2190 // If provider was not previously ready, update the
2191 // status and UI flag.
2192
2193 // Id would be valid only if the widget restore broadcast was received.
2194 if (isIdValid) {
2195 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2196 } else {
2197 status &= ~LauncherAppWidgetInfo
2198 .FLAG_PROVIDER_NOT_READY;
2199 }
2200 }
2201 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002202 } else {
2203 Log.v(TAG, "Widget restore pending id=" + id
2204 + " appWidgetId=" + appWidgetId
2205 + " status =" + restoreStatus);
2206 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002207 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002208 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002209 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002210
2211 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2212 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002213 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002214 // App restore has started. Update the flag
2215 appWidgetInfo.restoreStatus |=
2216 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002217 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002218 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002219 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002220 itemsToRemove.add(id);
2221 continue;
2222 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002223
2224 appWidgetInfo.installProgress =
2225 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002226 }
Sunny Goyalff572272014-07-23 13:58:07 -07002227
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 appWidgetInfo.cellX = c.getInt(cellXIndex);
2231 appWidgetInfo.cellY = c.getInt(cellYIndex);
2232 appWidgetInfo.spanX = c.getInt(spanXIndex);
2233 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002234 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2237 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2238 Log.e(TAG, "Widget found where container != " +
2239 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2240 continue;
2241 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002243 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002245 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2246 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 break;
2248 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002249
Adam Cohen59400422014-03-05 18:07:04 -08002250 if (!customWidget) {
2251 String providerName =
2252 appWidgetInfo.providerName.flattenToString();
2253 if (!providerName.equals(savedProvider) ||
2254 (appWidgetInfo.restoreStatus != restoreStatus)) {
2255 ContentValues values = new ContentValues();
2256 values.put(
2257 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2258 providerName);
2259 values.put(LauncherSettings.Favorites.RESTORED,
2260 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002261 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002262 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002263 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2265 sBgAppWidgets.add(appWidgetInfo);
2266 }
Joe Onorato36115782010-06-17 13:28:48 -04002267 break;
2268 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002270 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002271 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 }
2273 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002274 if (c != null) {
2275 c.close();
2276 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 }
2278
Winson Chungba9c37f2013-08-30 14:11:37 -07002279 // Break early if we've stopped loading
2280 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002281 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002282 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002283 }
2284
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002287 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2288 Utilities.createDbSelectionQuery(
2289 LauncherSettings.Favorites._ID, itemsToRemove), null);
2290 if (DEBUG_LOADERS) {
2291 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2292 LauncherSettings.Favorites._ID, itemsToRemove));
2293 }
2294
2295 // Remove any empty folder
2296 for (long folderId : LauncherAppState.getLauncherProvider()
2297 .deleteEmptyFolders()) {
2298 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2299 sBgFolders.remove(folderId);
2300 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002301 }
2302 }
2303
Chris Wrenf4d08112014-01-16 18:13:56 -05002304 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002305 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002306 ContentValues values = new ContentValues();
2307 values.put(LauncherSettings.Favorites.RESTORED, 0);
2308 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2309 Utilities.createDbSelectionQuery(
2310 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002311 }
2312
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002313 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2314 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002315 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002316 null, sWorker);
2317 }
2318
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002319 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002320
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002321 // Remove any empty screens
2322 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002323 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002324 long screenId = item.screenId;
2325 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2326 unusedScreens.contains(screenId)) {
2327 unusedScreens.remove(screenId);
2328 }
2329 }
2330
2331 // If there are any empty screens remove them, and update.
2332 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002333 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002335 }
2336
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 if (DEBUG_LOADERS) {
2338 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2339 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002341 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002342 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002343
Sunny Goyale2df0622015-04-24 11:27:00 -07002344 for (int i = 0; i < nScreens; i++) {
2345 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002346 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 line += " | ";
2348 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002349 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002350 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002351 if (x < screen.length && y < screen[x].length) {
2352 line += (screen[x][y] != null) ? "#" : ".";
2353 } else {
2354 line += "!";
2355 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 }
Joe Onorato36115782010-06-17 13:28:48 -04002357 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002360 }
Joe Onorato36115782010-06-17 13:28:48 -04002361 }
Adam Cohene25af792013-06-06 23:08:25 -07002362 }
2363
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002364 /**
2365 * Partially updates the item without any notification. Must be called on the worker thread.
2366 */
2367 private void updateItem(long itemId, ContentValues update) {
2368 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002369 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002370 update,
2371 BaseColumns._ID + "= ?",
2372 new String[]{Long.toString(itemId)});
2373 }
2374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 /** Filters the set of items who are directly or indirectly (via another container) on the
2376 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 ArrayList<ItemInfo> allWorkspaceItems,
2379 ArrayList<ItemInfo> currentScreenItems,
2380 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002381 // Purge any null ItemInfos
2382 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2383 while (iter.hasNext()) {
2384 ItemInfo i = iter.next();
2385 if (i == null) {
2386 iter.remove();
2387 }
2388 }
2389
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 // Order the set of items by their containers first, this allows use to walk through the
2391 // list sequentially, build up a list of containers that are in the specified screen,
2392 // as well as all items in those containers.
2393 Set<Long> itemsOnScreen = new HashSet<Long>();
2394 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2395 @Override
2396 public int compare(ItemInfo lhs, ItemInfo rhs) {
2397 return (int) (lhs.container - rhs.container);
2398 }
2399 });
2400 for (ItemInfo info : allWorkspaceItems) {
2401 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002402 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002403 currentScreenItems.add(info);
2404 itemsOnScreen.add(info.id);
2405 } else {
2406 otherScreenItems.add(info);
2407 }
2408 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2409 currentScreenItems.add(info);
2410 itemsOnScreen.add(info.id);
2411 } else {
2412 if (itemsOnScreen.contains(info.container)) {
2413 currentScreenItems.add(info);
2414 itemsOnScreen.add(info.id);
2415 } else {
2416 otherScreenItems.add(info);
2417 }
2418 }
2419 }
2420 }
2421
2422 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002423 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 ArrayList<LauncherAppWidgetInfo> appWidgets,
2425 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2426 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427
2428 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002429 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002431 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002432 currentScreenWidgets.add(widget);
2433 } else {
2434 otherScreenWidgets.add(widget);
2435 }
2436 }
2437 }
2438
2439 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002440 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002441 LongArrayMap<ItemInfo> itemsIdMap,
2442 LongArrayMap<FolderInfo> folders,
2443 LongArrayMap<FolderInfo> currentScreenFolders,
2444 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445
Sunny Goyale2df0622015-04-24 11:27:00 -07002446 int total = folders.size();
2447 for (int i = 0; i < total; i++) {
2448 long id = folders.keyAt(i);
2449 FolderInfo folder = folders.valueAt(i);
2450
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002452 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002454 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 currentScreenFolders.put(id, folder);
2456 } else {
2457 otherScreenFolders.put(id, folder);
2458 }
2459 }
2460 }
2461
2462 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2463 * right) */
2464 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002465 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002466 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 // XXX: review this
2468 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002469 @Override
2470 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002471 int cellCountX = (int) profile.numColumns;
2472 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002473 int screenOffset = cellCountX * cellCountY;
2474 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002476 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002478 rhs.cellY * cellCountX + rhs.cellX);
2479 return (int) (lr - rr);
2480 }
2481 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002483
Adam Cohendcd297f2013-06-18 13:13:40 -07002484 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2485 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 final Runnable r = new Runnable() {
2487 @Override
2488 public void run() {
2489 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2490 if (callbacks != null) {
2491 callbacks.bindScreens(orderedScreens);
2492 }
2493 }
2494 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002495 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002496 }
2497
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2499 final ArrayList<ItemInfo> workspaceItems,
2500 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002501 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002503
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 final boolean postOnMainThread = (deferredBindRunnables != null);
2505
2506 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002507 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002509 final int start = i;
2510 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 final Runnable r = new Runnable() {
2512 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002514 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002516 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2517 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 }
2519 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 };
2521 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002522 synchronized (deferredBindRunnables) {
2523 deferredBindRunnables.add(r);
2524 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002526 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527 }
Joe Onorato36115782010-06-17 13:28:48 -04002528 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529
2530 // Bind the folders
2531 if (!folders.isEmpty()) {
2532 final Runnable r = new Runnable() {
2533 public void run() {
2534 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2535 if (callbacks != null) {
2536 callbacks.bindFolders(folders);
2537 }
2538 }
2539 };
2540 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002541 synchronized (deferredBindRunnables) {
2542 deferredBindRunnables.add(r);
2543 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002545 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 }
2547 }
2548
2549 // Bind the widgets, one at a time
2550 N = appWidgets.size();
2551 for (int i = 0; i < N; i++) {
2552 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2553 final Runnable r = new Runnable() {
2554 public void run() {
2555 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2556 if (callbacks != null) {
2557 callbacks.bindAppWidget(widget);
2558 }
2559 }
2560 };
2561 if (postOnMainThread) {
2562 deferredBindRunnables.add(r);
2563 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002564 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565 }
2566 }
2567 }
2568
2569 /**
2570 * Binds all loaded data to actual views on the main thread.
2571 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002572 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 final long t = SystemClock.uptimeMillis();
2574 Runnable r;
2575
2576 // Don't use these two variables in any of the callback runnables.
2577 // Otherwise we hold a reference to them.
2578 final Callbacks oldCallbacks = mCallbacks.get();
2579 if (oldCallbacks == null) {
2580 // This launcher has exited and nobody bothered to tell us. Just bail.
2581 Log.w(TAG, "LoaderTask running with no launcher");
2582 return;
2583 }
2584
Winson Chung9b9fb962013-11-15 15:39:34 -08002585 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002586 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2587 ArrayList<LauncherAppWidgetInfo> appWidgets =
2588 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002589 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002590
2591 final LongArrayMap<FolderInfo> folders;
2592 final LongArrayMap<ItemInfo> itemsIdMap;
2593
Winson Chung2abf94d2012-07-18 18:16:38 -07002594 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 workspaceItems.addAll(sBgWorkspaceItems);
2596 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002597 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002598
2599 folders = sBgFolders.clone();
2600 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002601 }
2602
Derek Prothro7aff3992013-12-10 14:00:37 -05002603 final boolean isLoadingSynchronously =
2604 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002605 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002606 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002607 if (currScreen >= orderedScreenIds.size()) {
2608 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002609 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002610 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002611 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002612 final long currentScreenId = currentScreen < 0
2613 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002614
2615 // Load all the items that are on the current page first (and in the process, unbind
2616 // all the existing workspace items before we call startBinding() below.
2617 unbindWorkspaceItemsOnMainThread();
2618
2619 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002620 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2621 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2622 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2623 new ArrayList<LauncherAppWidgetInfo>();
2624 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2625 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002626 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2627 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628
Winson Chung9b9fb962013-11-15 15:39:34 -08002629 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002631 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002633 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002634 otherFolders);
2635 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2636 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2637
2638 // Tell the workspace that we're about to start binding items
2639 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002640 public void run() {
2641 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2642 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002644 }
2645 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002646 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002647 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648
Adam Cohendcd297f2013-06-18 13:13:40 -07002649 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2650
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002651 // Load items on the current page
2652 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2653 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002654 if (isLoadingSynchronously) {
2655 r = new Runnable() {
2656 public void run() {
2657 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002658 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002659 callbacks.onPageBoundSynchronously(currentScreen);
2660 }
2661 }
2662 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002663 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002664 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002665
Winson Chung4a2afa32012-07-19 14:53:05 -07002666 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2667 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002668 synchronized (mDeferredBindRunnables) {
2669 mDeferredBindRunnables.clear();
2670 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002671 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002672 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002673
2674 // Tell the workspace that we're done binding items
2675 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002676 public void run() {
2677 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2678 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002679 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002680 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002681
Winson Chung98e030b2012-05-07 16:01:11 -07002682 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002683 if (DEBUG_LOADERS) {
2684 Log.d(TAG, "bound workspace in "
2685 + (SystemClock.uptimeMillis()-t) + "ms");
2686 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002687
2688 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002689 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002690 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002691 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002692 synchronized (mDeferredBindRunnables) {
2693 mDeferredBindRunnables.add(r);
2694 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002695 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002696 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002697 }
Joe Onorato36115782010-06-17 13:28:48 -04002698 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002699
Joe Onorato36115782010-06-17 13:28:48 -04002700 private void loadAndBindAllApps() {
2701 if (DEBUG_LOADERS) {
2702 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2703 }
2704 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002705 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002706 synchronized (LoaderTask.this) {
2707 if (mStopped) {
2708 return;
2709 }
2710 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002711 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002712 synchronized (LoaderTask.this) {
2713 if (mStopped) {
2714 return;
2715 }
2716 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002717 }
Joe Onorato36115782010-06-17 13:28:48 -04002718 } else {
2719 onlyBindAllApps();
2720 }
2721 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002722
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002723 private void updateIconCache() {
2724 // Ignore packages which have a promise icon.
2725 HashSet<String> packagesToIgnore = new HashSet<>();
2726 synchronized (sBgLock) {
2727 for (ItemInfo info : sBgItemsIdMap) {
2728 if (info instanceof ShortcutInfo) {
2729 ShortcutInfo si = (ShortcutInfo) info;
2730 if (si.isPromise() && si.getTargetComponent() != null) {
2731 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2732 }
2733 } else if (info instanceof LauncherAppWidgetInfo) {
2734 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2735 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2736 packagesToIgnore.add(lawi.providerName.getPackageName());
2737 }
2738 }
2739 }
2740 }
2741 mIconCache.updateDbIcons(packagesToIgnore);
2742 }
2743
Joe Onorato36115782010-06-17 13:28:48 -04002744 private void onlyBindAllApps() {
2745 final Callbacks oldCallbacks = mCallbacks.get();
2746 if (oldCallbacks == null) {
2747 // This launcher has exited and nobody bothered to tell us. Just bail.
2748 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2749 return;
2750 }
2751
2752 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002753 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002754 final ArrayList<AppInfo> list
2755 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002756 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002757 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002758 public void run() {
2759 final long t = SystemClock.uptimeMillis();
2760 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2761 if (callbacks != null) {
2762 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002763 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002764 }
2765 if (DEBUG_LOADERS) {
2766 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2767 + (SystemClock.uptimeMillis()-t) + "ms");
2768 }
2769 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002770 };
2771 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002772 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002773 r.run();
2774 } else {
2775 mHandler.post(r);
2776 }
Joe Onorato36115782010-06-17 13:28:48 -04002777 }
2778
Winson Chung64359a52013-07-08 17:17:08 -07002779 private void loadAllApps() {
2780 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002781
Joe Onorato36115782010-06-17 13:28:48 -04002782 final Callbacks oldCallbacks = mCallbacks.get();
2783 if (oldCallbacks == null) {
2784 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002785 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002786 return;
2787 }
2788
Kenny Guyed131872014-04-30 03:02:21 +01002789 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2790
Winson Chung64359a52013-07-08 17:17:08 -07002791 // Clear the list of apps
2792 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002793 for (UserHandleCompat user : profiles) {
2794 // Query for the set of apps
2795 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002796 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002797 if (DEBUG_LOADERS) {
2798 Log.d(TAG, "getActivityList took "
2799 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2800 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2801 }
2802 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002803 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002804 if (apps == null || apps.isEmpty()) {
2805 return;
2806 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002807
Kenny Guyed131872014-04-30 03:02:21 +01002808 // Create the ApplicationInfos
2809 for (int i = 0; i < apps.size(); i++) {
2810 LauncherActivityInfoCompat app = apps.get(i);
2811 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002812 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002813 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002814
Sunny Goyal756a28a2015-04-23 17:07:55 -07002815 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2816 if (heuristic != null) {
2817 runAfterBindCompletes(new Runnable() {
2818
2819 @Override
2820 public void run() {
2821 heuristic.processUserApps(apps);
2822 }
2823 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002824 }
Winson Chung64359a52013-07-08 17:17:08 -07002825 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002826 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002827 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2828 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002829
2830 // Post callback on main thread
2831 mHandler.post(new Runnable() {
2832 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002833
Winson Chung64359a52013-07-08 17:17:08 -07002834 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002835 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002836 if (callbacks != null) {
2837 callbacks.bindAllApplications(added);
2838 if (DEBUG_LOADERS) {
2839 Log.d(TAG, "bound " + added.size() + " apps in "
2840 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2841 }
2842 } else {
2843 Log.i(TAG, "not binding apps: no Launcher activity");
2844 }
2845 }
2846 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002847 // Cleanup any data stored for a deleted user.
2848 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002849
Hyunyoung Song219d0482015-05-07 12:51:42 -07002850 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2851 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002852 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002853 Log.d(TAG, "Icons processed in "
2854 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002855 }
2856 }
2857
2858 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002859 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002860 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002861 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2862 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2863 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2864 }
Joe Onorato36115782010-06-17 13:28:48 -04002865 }
2866 }
2867
Sunny Goyal75b0f552015-05-20 21:57:06 -07002868 /**
2869 * Called when the icons for packages have been updated in the icon cache.
2870 */
2871 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2872 final Callbacks callbacks = getCallback();
2873 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2874 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2875
2876 // If any package icon has changed (app was updated while launcher was dead),
2877 // update the corresponding shortcuts.
2878 synchronized (sBgLock) {
2879 for (ItemInfo info : sBgItemsIdMap) {
2880 if (info instanceof ShortcutInfo && user.equals(info.user)
2881 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2882 ShortcutInfo si = (ShortcutInfo) info;
2883 ComponentName cn = si.getTargetComponent();
2884 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2885 si.updateIcon(mIconCache);
2886 updatedShortcuts.add(si);
2887 }
2888 }
2889 }
2890 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2891 }
2892
2893 if (!updatedShortcuts.isEmpty()) {
2894 final UserHandleCompat userFinal = user;
2895 mHandler.post(new Runnable() {
2896
2897 public void run() {
2898 Callbacks cb = getCallback();
2899 if (cb != null && callbacks == cb) {
2900 cb.bindShortcutsChanged(updatedShortcuts,
2901 new ArrayList<ShortcutInfo>(), userFinal);
2902 }
2903 }
2904 });
2905 }
2906
2907 if (!updatedApps.isEmpty()) {
2908 mHandler.post(new Runnable() {
2909
2910 public void run() {
2911 Callbacks cb = getCallback();
2912 if (cb != null && callbacks == cb) {
2913 cb.bindAppsUpdated(updatedApps);
2914 }
2915 }
2916 });
2917 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002918
2919 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2920 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002921 }
2922
Joe Onorato36115782010-06-17 13:28:48 -04002923 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002924 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002925 }
2926
Adam Cohen091440a2015-03-18 14:16:05 -07002927 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928
2929 @Override
2930 public void onReceive(Context context, Intent intent) {
2931 synchronized (sBgLock) {
2932 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2933 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 final PackageManager manager = context.getPackageManager();
2935 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2936 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002937 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2938 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002939 packagesRemoved.clear();
2940 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002941 for (String pkg : entry.getValue()) {
2942 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002943 boolean packageOnSdcard = launcherApps.isAppEnabled(
2944 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2945 if (packageOnSdcard) {
2946 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2947 packagesUnavailable.add(pkg);
2948 } else {
2949 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2950 packagesRemoved.add(pkg);
2951 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002952 }
2953 }
2954 if (!packagesRemoved.isEmpty()) {
2955 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2956 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2957 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002958 if (!packagesUnavailable.isEmpty()) {
2959 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2960 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2961 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002962 }
Sunny Goyal34942622014-08-29 17:20:55 -07002963 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002964 }
2965 }
2966 }
2967
Joe Onorato36115782010-06-17 13:28:48 -04002968 private class PackageUpdatedTask implements Runnable {
2969 int mOp;
2970 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002971 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002972
2973 public static final int OP_NONE = 0;
2974 public static final int OP_ADD = 1;
2975 public static final int OP_UPDATE = 2;
2976 public static final int OP_REMOVE = 3; // uninstlled
2977 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2978
2979
Kenny Guyed131872014-04-30 03:02:21 +01002980 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002981 mOp = op;
2982 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002983 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002984 }
2985
2986 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002987 if (!mHasLoaderCompletedOnce) {
2988 // Loader has not yet run.
2989 return;
2990 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002991 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002992
2993 final String[] packages = mPackages;
2994 final int N = packages.length;
2995 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002996 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002997 for (int i=0; i<N; i++) {
2998 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002999 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003000 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003001 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003002
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003003 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3004 if (heuristic != null) {
3005 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003006 }
Joe Onorato36115782010-06-17 13:28:48 -04003007 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003008 }
Joe Onorato36115782010-06-17 13:28:48 -04003009 case OP_UPDATE:
3010 for (int i=0; i<N; i++) {
3011 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003012 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003013 mBgAllAppsList.updatePackage(context, 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;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003017 case OP_REMOVE: {
3018 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3019 if (heuristic != null) {
3020 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003021 }
Joe Onorato36115782010-06-17 13:28:48 -04003022 for (int i=0; i<N; i++) {
3023 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003024 mIconCache.removeIconsForPkg(packages[i], mUser);
3025 }
3026 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003027 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003028 case OP_UNAVAILABLE:
3029 for (int i=0; i<N; i++) {
3030 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3031 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003032 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003033 }
3034 break;
3035 }
3036
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003037 ArrayList<AppInfo> added = null;
3038 ArrayList<AppInfo> modified = null;
3039 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003040
Adam Cohen487f7dd2012-06-28 18:12:10 -07003041 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003042 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003043 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003044 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003045 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003046 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003047 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
Winson Chung5d55f332012-07-16 20:45:03 -07003049 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003050 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003051 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003052 }
3053
Sunny Goyale0f58d72014-11-10 18:05:31 -08003054 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003055 if (callbacks == null) {
3056 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3057 return;
3058 }
3059
Sunny Goyal4390ace2014-10-13 11:33:11 -07003060 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3061 new HashMap<ComponentName, AppInfo>();
3062
Joe Onorato36115782010-06-17 13:28:48 -04003063 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003064 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003065 for (AppInfo ai : added) {
3066 addedOrUpdatedApps.put(ai.componentName, ai);
3067 }
Joe Onorato36115782010-06-17 13:28:48 -04003068 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003069
Joe Onorato36115782010-06-17 13:28:48 -04003070 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003071 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003072 for (AppInfo ai : modified) {
3073 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003074 }
3075
Joe Onorato36115782010-06-17 13:28:48 -04003076 mHandler.post(new Runnable() {
3077 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003078 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003079 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003080 callbacks.bindAppsUpdated(modifiedFinal);
3081 }
3082 }
3083 });
3084 }
Winson Chung83892cc2013-05-01 16:53:33 -07003085
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003087 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3089 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3090 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3091
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003092 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003093 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003094 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003095 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3096 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003097 boolean infoUpdated = false;
3098 boolean shortcutUpdated = false;
3099
3100 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003101 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003103 Bitmap icon = Utilities.createIconBitmap(
3104 si.iconResource.packageName,
3105 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003106 if (icon != null) {
3107 si.setIcon(icon);
3108 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003110 }
3111 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003112
3113 ComponentName cn = si.getTargetComponent();
3114 if (cn != null && packageSet.contains(cn.getPackageName())) {
3115 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3116
3117 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3119 // Auto install icon
3120 PackageManager pm = context.getPackageManager();
3121 ResolveInfo matched = pm.resolveActivity(
3122 new Intent(Intent.ACTION_MAIN)
3123 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3124 PackageManager.MATCH_DEFAULT_ONLY);
3125 if (matched == null) {
3126 // Try to find the best match activity.
3127 Intent intent = pm.getLaunchIntentForPackage(
3128 cn.getPackageName());
3129 if (intent != null) {
3130 cn = intent.getComponent();
3131 appInfo = addedOrUpdatedApps.get(cn);
3132 }
3133
3134 if ((intent == null) || (appInfo == null)) {
3135 removedShortcuts.add(si);
3136 continue;
3137 }
3138 si.promisedIntent = intent;
3139 }
3140 }
3141
3142 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003143 if (appInfo != null) {
3144 si.flags = appInfo.flags;
3145 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146
Sunny Goyal756adbc2015-04-16 15:20:51 -07003147 si.intent = si.promisedIntent;
3148 si.promisedIntent = null;
3149 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003150 infoUpdated = true;
3151 si.updateIcon(mIconCache);
3152 }
3153
3154 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3155 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3156 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003157 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003158 si.contentDescription = appInfo.contentDescription;
3159 infoUpdated = true;
3160 }
3161
3162 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3163 // Since package was just updated, the target must be available now.
3164 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3165 shortcutUpdated = true;
3166 }
3167 }
3168
3169 if (infoUpdated || shortcutUpdated) {
3170 updatedShortcuts.add(si);
3171 }
3172 if (infoUpdated) {
3173 updateItemInDatabase(context, si);
3174 }
3175 } else if (info instanceof LauncherAppWidgetInfo) {
3176 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3177 if (mUser.equals(widgetInfo.user)
3178 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3179 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003180 widgetInfo.restoreStatus &=
3181 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3182 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003183 widgets.add(widgetInfo);
3184 updateItemInDatabase(context, widgetInfo);
3185 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003186 }
3187 }
3188 }
3189
Sunny Goyal4390ace2014-10-13 11:33:11 -07003190 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3191 mHandler.post(new Runnable() {
3192
3193 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003194 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003195 if (callbacks == cb && cb != null) {
3196 callbacks.bindShortcutsChanged(
3197 updatedShortcuts, removedShortcuts, mUser);
3198 }
3199 }
3200 });
3201 if (!removedShortcuts.isEmpty()) {
3202 deleteItemsFromDatabase(context, removedShortcuts);
3203 }
3204 }
3205 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003206 mHandler.post(new Runnable() {
3207 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003208 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003210 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003211 }
3212 }
3213 });
3214 }
3215 }
3216
Winson Chungdf95eb12013-10-16 14:57:07 -07003217 final ArrayList<String> removedPackageNames =
3218 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003219 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 // Mark all packages in the broadcast to be removed
3221 removedPackageNames.addAll(Arrays.asList(packages));
3222 } else if (mOp == OP_UPDATE) {
3223 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003224 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003225 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003226 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003227 }
3228 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003229 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003230
Winson Chungdf95eb12013-10-16 14:57:07 -07003231 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003232 final int removeReason;
3233 if (mOp == OP_UNAVAILABLE) {
3234 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3235 } else {
3236 // Remove all the components associated with this package
3237 for (String pn : removedPackageNames) {
3238 deletePackageFromDatabase(context, pn, mUser);
3239 }
3240 // Remove all the specific components
3241 for (AppInfo a : removedApps) {
3242 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3243 deleteItemsFromDatabase(context, infos);
3244 }
3245 removeReason = 0;
3246 }
3247
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003249 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003251 mHandler.post(new Runnable() {
3252 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003253 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003254 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003255 callbacks.bindComponentsRemoved(
3256 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003257 }
3258 }
3259 });
Joe Onoratobe386092009-11-17 17:32:16 -08003260 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003261
3262 // onProvidersChanged method (API >= 17) already refreshed the widget list
3263 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3264
Adam Cohen4caf2982013-08-20 18:54:31 -07003265 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003266 mHandler.post(new Runnable() {
3267 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003268 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003269 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003270 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003271 }
3272 }
3273 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274 }
3275 }
3276
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003277 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3278 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003279 ArrayList<LauncherAppWidgetProviderInfo> results =
3280 new ArrayList<LauncherAppWidgetProviderInfo>();
3281 try {
3282 synchronized (sBgLock) {
3283 if (sBgWidgetProviders == null || refresh) {
3284 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3285 = new HashMap<>();
3286 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3287 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003288
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003289 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3290 for (AppWidgetProviderInfo pInfo : widgets) {
3291 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3292 UserHandleCompat user = wm.getUser(info);
3293 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3294 }
Robin Lee26ace122015-03-16 19:41:43 +00003295
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003296 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3297 for (CustomAppWidget widget : customWidgets) {
3298 info = new LauncherAppWidgetProviderInfo(context, widget);
3299 UserHandleCompat user = wm.getUser(info);
3300 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3301 }
3302 // Replace the global list at the very end, so that if there is an exception,
3303 // previously loaded provider list is used.
3304 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003305 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003306 results.addAll(sBgWidgetProviders.values());
3307 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003308 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003309 } catch (Exception e) {
3310 if (e.getCause() instanceof TransactionTooLargeException) {
3311 // the returned value may be incomplete and will not be refreshed until the next
3312 // time Launcher starts.
3313 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3314 // onResume is called to refresh the widget provider list.
3315 synchronized (sBgLock) {
3316 if (sBgWidgetProviders != null) {
3317 results.addAll(sBgWidgetProviders.values());
3318 }
3319 return results;
3320 }
3321 } else {
3322 throw e;
3323 }
Adam Cohen59400422014-03-05 18:07:04 -08003324 }
3325 }
3326
Robin Lee26ace122015-03-16 19:41:43 +00003327 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3328 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003329 synchronized (sBgLock) {
3330 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003331 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003332 }
Robin Lee26ace122015-03-16 19:41:43 +00003333 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003334 }
3335 }
3336
Hyunyoung Song227239e2015-05-04 18:17:35 -07003337 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3338 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003339
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003340 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 @Override
3342 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003343 updateWidgetsModel(context, refresh);
3344 final WidgetsModel model = mBgWidgetsModel.clone();
3345
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003346 mHandler.post(new Runnable() {
3347 @Override
3348 public void run() {
3349 Callbacks cb = getCallback();
3350 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003351 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003352 }
3353 }
3354 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003355 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003356 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3357 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003358 }
3359 });
3360 }
3361
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003362 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003363 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003364 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003365 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003366 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003367 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003368 PackageManager packageManager = context.getPackageManager();
3369 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003370 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3372 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003373 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 }
3375
Adam Cohen091440a2015-03-18 14:16:05 -07003376 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003377 UserHandleCompat user) {
3378 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3379 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003380 }
Adam Cohen556f6132014-01-15 15:18:08 -08003381
Kenny Guyed131872014-04-30 03:02:21 +01003382 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3383 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003384 if (cn == null) {
3385 return false;
3386 }
Kenny Guyed131872014-04-30 03:02:21 +01003387 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3388 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003389 return false;
3390 }
Kenny Guyed131872014-04-30 03:02:21 +01003391 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003392 }
3393
Adam Cohena28b78e2014-05-20 17:03:04 -07003394 public static boolean isValidPackage(Context context, String packageName,
3395 UserHandleCompat user) {
3396 if (packageName == null) {
3397 return false;
3398 }
3399 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3400 return launcherApps.isPackageEnabledForProfile(packageName, user);
3401 }
3402
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003404 * Make an ShortcutInfo object for a restored application or shortcut item that points
3405 * to a package that is not yet installed on the system.
3406 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003407 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003408 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003409 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003410 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003411
3412 Bitmap icon = iconInfo.loadIcon(c, info, context);
3413 // the fallback icon
3414 if (icon == null) {
3415 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3416 } else {
3417 info.setIcon(icon);
3418 }
Sunny Goyal34942622014-08-29 17:20:55 -07003419
3420 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003421 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003422 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003423 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003424 }
Sunny Goyal34942622014-08-29 17:20:55 -07003425 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3426 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003427 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003428 }
Sunny Goyal34942622014-08-29 17:20:55 -07003429 } else {
3430 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3431 }
3432
Winson Chung82b016c2015-05-08 17:00:10 -07003433 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003434 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003435 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003436 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003437 return info;
3438 }
3439
3440 /**
3441 * Make an Intent object for a restored application or shortcut item that points
3442 * to the market page for the item.
3443 */
Adam Cohen091440a2015-03-18 14:16:05 -07003444 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003445 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003446 return getMarketIntent(componentName.getPackageName());
3447 }
3448
3449 static Intent getMarketIntent(String packageName) {
3450 return new Intent(Intent.ACTION_VIEW)
3451 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003452 .scheme("market")
3453 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003454 .appendQueryParameter("id", packageName)
3455 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003456 }
3457
3458 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003459 * Make an ShortcutInfo object for a shortcut that is an application.
3460 *
3461 * If c is not null, then it will be used to fill in missing data like the title and icon.
3462 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003463 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003464 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003465 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003466 if (user == null) {
3467 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003468 return null;
3469 }
3470
Kenny Guyed131872014-04-30 03:02:21 +01003471 ComponentName componentName = intent.getComponent();
3472 if (componentName == null) {
3473 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3474 return null;
3475 }
3476
3477 Intent newIntent = new Intent(intent.getAction(), null);
3478 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3479 newIntent.setComponent(componentName);
3480 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003481 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003482 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3483 return null;
3484 }
3485
3486 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003487 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003488 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3489 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3490 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003491 }
3492
Joe Onorato56d82912010-03-07 14:32:10 -05003493 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003494 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003495 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003496 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003497
Joe Onorato56d82912010-03-07 14:32:10 -05003498 // fall back to the class name of the activity
3499 if (info.title == null) {
3500 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003501 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003502
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003504 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003505 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003506 if (lai != null) {
3507 info.flags = AppInfo.initFlags(lai);
3508 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003510 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003511
Sunny Goyale2df0622015-04-24 11:27:00 -07003512 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003513 ItemInfoFilter f) {
3514 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3515 for (ItemInfo i : infos) {
3516 if (i instanceof ShortcutInfo) {
3517 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003518 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003519 if (cn != null && f.filterItem(null, info, cn)) {
3520 filtered.add(info);
3521 }
3522 } else if (i instanceof FolderInfo) {
3523 FolderInfo info = (FolderInfo) i;
3524 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003525 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003526 if (cn != null && f.filterItem(info, s, cn)) {
3527 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003528 }
3529 }
Winson Chung64359a52013-07-08 17:17:08 -07003530 } else if (i instanceof LauncherAppWidgetInfo) {
3531 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3532 ComponentName cn = info.providerName;
3533 if (cn != null && f.filterItem(null, info, cn)) {
3534 filtered.add(info);
3535 }
Winson Chung8a435102012-08-30 17:16:53 -07003536 }
3537 }
Winson Chung64359a52013-07-08 17:17:08 -07003538 return new ArrayList<ItemInfo>(filtered);
3539 }
3540
Adam Cohen091440a2015-03-18 14:16:05 -07003541 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003542 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003543 ItemInfoFilter filter = new ItemInfoFilter() {
3544 @Override
3545 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003546 if (info.user == null) {
3547 return cn.equals(cname);
3548 } else {
3549 return cn.equals(cname) && info.user.equals(user);
3550 }
Winson Chung64359a52013-07-08 17:17:08 -07003551 }
3552 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003553 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003554 }
3555
Sunny Goyal1a745e82014-10-02 15:58:31 -07003556 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003557 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558 */
Adam Cohen091440a2015-03-18 14:16:05 -07003559 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003560 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003561 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003562 // Non-app shortcuts are only supported for current user.
3563 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003565
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003566 // TODO: If there's an explicit component and we can't install that, delete it.
3567
Winson Chung82b016c2015-05-08 17:00:10 -07003568 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003569
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003570 Bitmap icon = iconInfo.loadIcon(c, info, context);
3571 // the fallback icon
3572 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003573 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003574 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003575 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003576 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 return info;
3578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003579
Sunny Goyal2350bc92014-10-14 16:42:54 -07003580 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003581 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3582 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3583 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3584
Adam Cohend9198822011-11-22 16:42:47 -08003585 if (intent == null) {
3586 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3587 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3588 return null;
3589 }
3590
Joe Onorato0589f0f2010-02-08 13:44:00 -08003591 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003592 boolean customIcon = false;
3593 ShortcutIconResource iconResource = null;
3594
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003595 if (bitmap instanceof Bitmap) {
3596 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003597 customIcon = true;
3598 } else {
3599 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003600 if (extra instanceof ShortcutIconResource) {
3601 iconResource = (ShortcutIconResource) extra;
3602 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003603 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003604 }
3605 }
3606
Michael Jurkac9d95c52011-08-29 14:03:34 -07003607 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003608
Kenny Guyed131872014-04-30 03:02:21 +01003609 // Only support intents for current user for now. Intents sent from other
3610 // users wouldn't get here without intent forwarding anyway.
3611 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003612 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003613 icon = mIconCache.getDefaultIcon(info.user);
3614 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003615 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003616 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003617
Winson Chung82b016c2015-05-08 17:00:10 -07003618 info.title = Utilities.trim(name);
3619 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003620 info.intent = intent;
3621 info.customIcon = customIcon;
3622 info.iconResource = iconResource;
3623
3624 return info;
3625 }
3626
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003628 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 * or make a new one.
3630 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003631 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 // See if a placeholder was created for us already
3633 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003634 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003635 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003636 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 folders.put(id, folderInfo);
3638 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003639 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003640 }
3641
Joe Onoratobe386092009-11-17 17:32:16 -08003642
Sunny Goyal651077b2014-06-30 14:15:31 -07003643 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3644 return (provider != null) && (provider.provider != null)
3645 && (provider.provider.getPackageName() != null);
3646 }
3647
Joe Onoratobe386092009-11-17 17:32:16 -08003648 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003649 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003650 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3651 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3652 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3653 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003654 if (mLoaderTask != null) {
3655 mLoaderTask.dumpState();
3656 } else {
3657 Log.d(TAG, "mLoaderTask=null");
3658 }
Joe Onoratobe386092009-11-17 17:32:16 -08003659 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003660
3661 public Callbacks getCallback() {
3662 return mCallbacks != null ? mCallbacks.get() : null;
3663 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003664
3665 /**
3666 * @return {@link FolderInfo} if its already loaded.
3667 */
3668 public FolderInfo findFolderById(Long folderId) {
3669 synchronized (sBgLock) {
3670 return sBgFolders.get(folderId);
3671 }
3672 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003673
3674 /**
3675 * @return the looper for the worker thread which can be used to start background tasks.
3676 */
3677 public static Looper getWorkerLooper() {
3678 return sWorkerThread.getLooper();
3679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680}