blob: 1b9260290d30a4aa6f2e77d172ebe164e0f2f713 [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;
23import android.content.ContentProviderClient;
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
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;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070039import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080040import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040041import android.os.Handler;
42import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070043import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040048import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080049import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080051import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070052import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010053
Sunny Goyalffe83f12014-08-14 17:39:34 -070054import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010055import com.android.launcher3.compat.LauncherActivityInfoCompat;
56import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070057import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070058import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010059import com.android.launcher3.compat.UserHandleCompat;
60import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
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;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700151 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700163 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 // sBgWorkspaceScreens is the ordered set of workspace screens.
166 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
167
Adam Cohen59400422014-03-05 18:07:04 -0800168 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000169 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Reena Lee99a73f32011-10-24 17:27:37 -0700179 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700180
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);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400244 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700245 Configuration config = res.getConfiguration();
246 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100247 mLauncherApps = LauncherAppsCompat.getInstance(context);
248 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 }
250
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
252 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700254 if (sWorkerThread.getThreadId() == Process.myTid()) {
255 // If we are on the worker thread, post onto the main handler
256 mHandler.post(r);
257 } else {
258 r.run();
259 }
260 }
261
262 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
263 * posted on the worker thread handler. */
264 private static void runOnWorkerThread(Runnable r) {
265 if (sWorkerThread.getThreadId() == Process.myTid()) {
266 r.run();
267 } else {
268 // If we are not on the worker thread, then post to the worker handler
269 sWorker.post(r);
270 }
271 }
272
Sunny Goyal756a28a2015-04-23 17:07:55 -0700273 /**
274 * Runs the specified runnable after the loader is complete
275 */
276 private void runAfterBindCompletes(Runnable r) {
277 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
278 synchronized (mBindCompleteRunnables) {
279 mBindCompleteRunnables.add(r);
280 }
281 } else {
282 runOnWorkerThread(r);
283 }
284 }
285
Winson Chunge43a1e72014-01-15 10:33:02 -0800286 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
287 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800288 }
289
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 public void setPackageState(final PackageInstallInfo installInfo) {
291 Runnable updateRunnable = new Runnable() {
292
293 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700295 synchronized (sBgLock) {
296 final HashSet<ItemInfo> updates = new HashSet<>();
297
298 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
299 // Ignore install success events as they are handled by Package add events.
300 return;
301 }
302
Sunny Goyale2df0622015-04-24 11:27:00 -0700303 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700304 if (info instanceof ShortcutInfo) {
305 ShortcutInfo si = (ShortcutInfo) info;
306 ComponentName cn = si.getTargetComponent();
307 if (si.isPromise() && (cn != null)
308 && installInfo.packageName.equals(cn.getPackageName())) {
309 si.setInstallProgress(installInfo.progress);
310
311 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
312 // Mark this info as broken.
313 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
314 }
315 updates.add(si);
316 }
317 }
318 }
319
320 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
321 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
322 widget.installProgress = installInfo.progress;
323 updates.add(widget);
324 }
325 }
326
327 if (!updates.isEmpty()) {
328 // Push changes to the callback.
329 Runnable r = new Runnable() {
330 public void run() {
331 Callbacks callbacks = getCallback();
332 if (callbacks != null) {
333 callbacks.bindRestoreItemsChange(updates);
334 }
335 }
336 };
337 mHandler.post(r);
338 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500339 }
340 }
341 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500343 }
344
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 /**
346 * Updates the icons and label of all pending icons for the provided package name.
347 */
348 public void updateSessionDisplayInfo(final String packageName) {
349 Runnable updateRunnable = new Runnable() {
350
351 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700352 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700353 synchronized (sBgLock) {
354 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
355 final UserHandleCompat user = UserHandleCompat.myUserHandle();
356
Sunny Goyale2df0622015-04-24 11:27:00 -0700357 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700358 if (info instanceof ShortcutInfo) {
359 ShortcutInfo si = (ShortcutInfo) info;
360 ComponentName cn = si.getTargetComponent();
361 if (si.isPromise() && (cn != null)
362 && packageName.equals(cn.getPackageName())) {
363 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
364 // For auto install apps update the icon as well as label.
365 mIconCache.getTitleAndIcon(si,
366 si.promisedIntent, user,
367 si.shouldUseLowResIcon());
368 } else {
369 // Only update the icon for restored apps.
370 si.updateIcon(mIconCache);
371 }
372 updates.add(si);
373 }
374 }
375 }
376
377 if (!updates.isEmpty()) {
378 // Push changes to the callback.
379 Runnable r = new Runnable() {
380 public void run() {
381 Callbacks callbacks = getCallback();
382 if (callbacks != null) {
383 callbacks.bindShortcutsChanged(updates,
384 new ArrayList<ShortcutInfo>(), user);
385 }
386 }
387 };
388 mHandler.post(r);
389 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700390 }
391 }
392 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700393 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700394 }
395
Adam Cohen76a47a12014-02-05 11:47:43 -0800396 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398
399 if (allAppsApps == null) {
400 throw new RuntimeException("allAppsApps must not be null");
401 }
402 if (allAppsApps.isEmpty()) {
403 return;
404 }
405
406 // Process the newly added applications and add them to the database first
407 Runnable r = new Runnable() {
408 public void run() {
409 runOnMainThread(new Runnable() {
410 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800411 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800412 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500413 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800414 }
415 }
416 });
417 }
418 };
419 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700420 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800421
Sunny Goyala9116722015-04-29 13:55:58 -0700422 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423 int[] xy, int spanX, int spanY) {
424 LauncherAppState app = LauncherAppState.getInstance();
425 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
426 final int xCount = (int) grid.numColumns;
427 final int yCount = (int) grid.numRows;
428 boolean[][] occupied = new boolean[xCount][yCount];
429 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700430 for (ItemInfo r : occupiedPos) {
431 int right = r.cellX + r.spanX;
432 int bottom = r.cellY + r.spanY;
433 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
434 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 occupied[x][y] = true;
436 }
437 }
438 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800439 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700440 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800441 }
442
443 /**
444 * Find a position on the screen for the given size or adds a new screen.
445 * @return screenId and the coordinates for the item.
446 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700447 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 ArrayList<Long> workspaceScreens,
450 ArrayList<Long> addedWorkspaceScreensFinal,
451 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700452 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453
Sunny Goyala9116722015-04-29 13:55:58 -0700454 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700455 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700456 synchronized (sBgLock) {
457 for (ItemInfo info : sBgItemsIdMap) {
458 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
459 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
460 if (items == null) {
461 items = new ArrayList<>();
462 screenItems.put(info.screenId, items);
463 }
464 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467 }
468
469 // Find appropriate space for the item.
470 long screenId = 0;
471 int[] cordinates = new int[2];
472 boolean found = false;
473
474 int screenCount = workspaceScreens.size();
475 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700476 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800477 if (preferredScreenIndex < screenCount) {
478 screenId = workspaceScreens.get(preferredScreenIndex);
479 found = findNextAvailableIconSpaceInScreen(
480 screenItems.get(screenId), cordinates, spanX, spanY);
481 }
482
483 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700484 // Search on any of the screens starting from the first screen.
485 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 screenId = workspaceScreens.get(screen);
487 if (findNextAvailableIconSpaceInScreen(
488 screenItems.get(screenId), cordinates, spanX, spanY)) {
489 // We found a space for it
490 found = true;
491 break;
492 }
493 }
494 }
495
496 if (!found) {
497 // Still no position found. Add a new screen to the end.
498 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
499
500 // Save the screen id for binding in the workspace
501 workspaceScreens.add(screenId);
502 addedWorkspaceScreensFinal.add(screenId);
503
504 // If we still can't find an empty space, then God help us all!!!
505 if (!findNextAvailableIconSpaceInScreen(
506 screenItems.get(screenId), cordinates, spanX, spanY)) {
507 throw new RuntimeException("Can't find space to add the item");
508 }
509 }
510 return Pair.create(screenId, cordinates);
511 }
512
513 /**
514 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700516 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700517 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800519 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700520 return;
Winson Chung997a9232013-07-24 15:33:46 -0700521 }
Winson Chung64359a52013-07-08 17:17:08 -0700522 // Process the newly added applications and add them to the database first
523 Runnable r = new Runnable() {
524 public void run() {
525 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
526 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
527
Winson Chung76828c82013-08-19 15:43:29 -0700528 // Get the list of workspace screens. We need to append to this list and
529 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
530 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800531 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700532 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700534 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700536 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700538 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 }
Winson Chung76828c82013-08-19 15:43:29 -0700540
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700542 Pair<Long, int[]> coords = findSpaceForItem(context,
543 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800544 1, 1);
545 long screenId = coords.first;
546 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700547
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 ItemInfo itemInfo;
549 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
550 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800551 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700552 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700553 } else {
554 throw new RuntimeException("Unexpected info type");
555 }
Winson Chung94d67682013-09-25 16:29:40 -0700556
Winson Chung64359a52013-07-08 17:17:08 -0700557 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700558 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700559 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700560 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700561 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700562 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700563 }
564 }
565
Winson Chung76828c82013-08-19 15:43:29 -0700566 // Update the workspace screens
567 updateWorkspaceScreenOrder(context, workspaceScreens);
568
Adam Cohen76a47a12014-02-05 11:47:43 -0800569 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700570 runOnMainThread(new Runnable() {
571 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800572 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700573 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700574 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
575 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700576 if (!addedShortcutsFinal.isEmpty()) {
577 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
578 long lastScreenId = info.screenId;
579 for (ItemInfo i : addedShortcutsFinal) {
580 if (i.screenId == lastScreenId) {
581 addAnimated.add(i);
582 } else {
583 addNotAnimated.add(i);
584 }
Winson Chung997a9232013-07-24 15:33:46 -0700585 }
586 }
Winson Chungd64d1762013-08-20 14:37:16 -0700587 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800588 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700589 }
Winson Chung64359a52013-07-08 17:17:08 -0700590 }
Winson Chung997a9232013-07-24 15:33:46 -0700591 });
592 }
Winson Chung64359a52013-07-08 17:17:08 -0700593 }
594 };
595 runOnWorkerThread(r);
596 }
597
Sunny Goyald33860f2015-04-23 16:02:20 -0700598 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700599 if (sWorkerThread.getThreadId() == Process.myTid()) {
600 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
601 "main thread");
602 }
603
604 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400605 synchronized (mDeferredBindRunnables) {
606 mDeferredBindRunnables.clear();
607 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700608
609 // Remove any queued UI runnables
610 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700611 // Unbind all the workspace items
612 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700613 }
614
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700615 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700616 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700617 // Ensure that we don't use the same workspace items data structure on the main thread
618 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700619 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700620 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700621 tmpItems.addAll(sBgWorkspaceItems);
622 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700623 }
624 Runnable r = new Runnable() {
625 @Override
626 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700627 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 item.unbind();
629 }
630 }
631 };
632 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700633 }
634
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 /**
636 * Adds an item to the DB if it was not created previously, or move it to a new
637 * <container, screen, cellX, cellY>
638 */
639 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 if (item.container == ItemInfo.NO_ID) {
642 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700643 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 } else {
645 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
648 }
649
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700650 static void checkItemInfoLocked(
651 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
652 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
653 if (modelItem != null && item != modelItem) {
654 // check all the data is consistent
655 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
656 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
657 ShortcutInfo shortcut = (ShortcutInfo) item;
658 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
659 modelShortcut.intent.filterEquals(shortcut.intent) &&
660 modelShortcut.id == shortcut.id &&
661 modelShortcut.itemType == shortcut.itemType &&
662 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700663 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 modelShortcut.cellX == shortcut.cellX &&
665 modelShortcut.cellY == shortcut.cellY &&
666 modelShortcut.spanX == shortcut.spanX &&
667 modelShortcut.spanY == shortcut.spanY &&
668 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
669 (modelShortcut.dropPos != null &&
670 shortcut.dropPos != null &&
671 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
672 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
673 // For all intents and purposes, this is the same object
674 return;
675 }
676 }
677
678 // the modelItem needs to match up perfectly with item if our model is
679 // to be consistent with the database-- for now, just require
680 // modelItem == item or the equality check above
681 String msg = "item: " + ((item != null) ? item.toString() : "null") +
682 "modelItem: " +
683 ((modelItem != null) ? modelItem.toString() : "null") +
684 "Error: ItemInfo passed to checkItemInfo doesn't match original";
685 RuntimeException e = new RuntimeException(msg);
686 if (stackTrace != null) {
687 e.setStackTrace(stackTrace);
688 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800689 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700690 }
691 }
692
Michael Jurka816474f2012-06-25 14:49:02 -0700693 static void checkItemInfo(final ItemInfo item) {
694 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
695 final long itemId = item.id;
696 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700697 public void run() {
698 synchronized (sBgLock) {
699 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700700 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700701 }
702 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700703 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700704 }
705
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
707 final ItemInfo item, final String callingFunction) {
708 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700709 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700710 final ContentResolver cr = context.getContentResolver();
711
Adam Cohen487f7dd2012-06-28 18:12:10 -0700712 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713 Runnable r = new Runnable() {
714 public void run() {
715 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 updateItemArrays(item, itemId, stackTrace);
717 }
718 };
719 runOnWorkerThread(r);
720 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
723 final ArrayList<ItemInfo> items, final String callingFunction) {
724 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700725
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
727 Runnable r = new Runnable() {
728 public void run() {
729 ArrayList<ContentProviderOperation> ops =
730 new ArrayList<ContentProviderOperation>();
731 int count = items.size();
732 for (int i = 0; i < count; i++) {
733 ItemInfo item = items.get(i);
734 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700735 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700737
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
739 updateItemArrays(item, itemId, stackTrace);
740
741 }
742 try {
743 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
744 } catch (Exception e) {
745 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700746 }
747 }
748 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700749 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700750 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700751
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
753 // Lock on mBgLock *after* the db operation
754 synchronized (sBgLock) {
755 checkItemInfoLocked(itemId, item, stackTrace);
756
757 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
758 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
759 // Item is in a folder, make sure this folder exists
760 if (!sBgFolders.containsKey(item.container)) {
761 // An items container is being set to a that of an item which is not in
762 // the list of Folders.
763 String msg = "item: " + item + " container being set to: " +
764 item.container + ", not in the list of folders";
765 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 }
767 }
768
769 // Items are added/removed from the corresponding FolderInfo elsewhere, such
770 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
771 // that are on the desktop, as appropriate
772 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800773 if (modelItem != null &&
774 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
775 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776 switch (modelItem.itemType) {
777 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
778 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
779 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
780 if (!sBgWorkspaceItems.contains(modelItem)) {
781 sBgWorkspaceItems.add(modelItem);
782 }
783 break;
784 default:
785 break;
786 }
787 } else {
788 sBgWorkspaceItems.remove(modelItem);
789 }
790 }
791 }
792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700795 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700796 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 item.cellX = cellX;
800 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700801
Winson Chung3d503fb2011-07-13 17:25:49 -0700802 // We store hotseat items in canonical form which is this orientation invariant position
803 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700804 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810
811 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700813 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
814 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800815 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700819 }
820
821 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700822 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
823 * cellX, cellY have already been updated on the ItemInfos.
824 */
825 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
826 final long container, final int screen) {
827
828 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
829 int count = items.size();
830
831 for (int i = 0; i < count; i++) {
832 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700833 item.container = container;
834
835 // We store hotseat items in canonical form which is this orientation invariant position
836 // in the hotseat
837 if (context instanceof Launcher && screen < 0 &&
838 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700840 item.cellY);
841 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700843 }
844
845 final ContentValues values = new ContentValues();
846 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
847 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
848 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800849 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700851
852 contentValues.add(values);
853 }
854 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
855 }
856
857 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800859 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700862 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800863 item.cellX = cellX;
864 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700865 item.spanX = spanX;
866 item.spanY = spanY;
867
868 // We store hotseat items in canonical form which is this orientation invariant position
869 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 }
Adam Cohend4844c32011-02-18 19:25:06 -0800876
Adam Cohend4844c32011-02-18 19:25:06 -0800877 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800878 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700879 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
880 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800881 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700882 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
883 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800885
Michael Jurka816474f2012-06-25 14:49:02 -0700886 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700887 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888
889 /**
890 * Update an item to the database in a specified container.
891 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700892 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700893 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100894 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700895 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800896 }
897
Sunny Goyal756a28a2015-04-23 17:07:55 -0700898 private void assertWorkspaceLoaded() {
899 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
900 throw new RuntimeException("Trying to add shortcut while loader is running");
901 }
902 }
903
Adam Cohend4844c32011-02-18 19:25:06 -0800904 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700905 * Returns true if the shortcuts already exists on the workspace. This must be called after
906 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700908 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
909 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 if (intent.getComponent() != null) {
913 // If component is not null, an intent with null package will produce
914 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700916 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700917 intentWithPkg = intent.toUri(0);
918 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700919 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
921 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700922 }
923 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700924 intentWithPkg = intent.toUri(0);
925 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700926 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700927 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700928
929 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700930 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 if (item instanceof ShortcutInfo) {
932 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700933 if (info.getIntent() != null && info.user.equals(user)) {
934 String s = info.getIntent().toUri(0);
935 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
936 return true;
937 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700938 }
939 }
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700942 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700943 }
944
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 /**
946 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
947 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700948 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 final ContentResolver cr = context.getContentResolver();
950 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
951 "_id=? and (itemType=? or itemType=?)",
952 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700953 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700954
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 try {
956 if (c.moveToFirst()) {
957 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
958 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
959 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
960 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
961 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
962 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700963 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 FolderInfo folderInfo = null;
966 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700967 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
968 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700970 }
971
Sunny Goyala508e4f2015-05-21 09:33:57 -0700972 // Do not trim the folder label, as is was set by the user.
973 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 folderInfo.id = id;
975 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700977 folderInfo.cellX = c.getInt(cellXIndex);
978 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700979 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980
981 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 } finally {
984 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
986
987 return null;
988 }
989
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 /**
991 * Add an item to the database in a specified container. Sets the container, screen, cellX and
992 * cellY fields of the item. Also assigns an ID to the item.
993 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700994 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700995 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 item.cellX = cellX;
998 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 // We store hotseat items in canonical form which is this orientation invariant position
1000 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007
1008 final ContentValues values = new ContentValues();
1009 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001010 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011
Michael Jurka414300a2013-08-27 15:42:35 +02001012 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001014
Jason Monk8e19cf22014-03-20 15:06:57 -04001015 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001016 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001017 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001018 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001021 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001022 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 sBgItemsIdMap.put(item.id, item);
1024 switch (item.itemType) {
1025 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1026 sBgFolders.put(item.id, (FolderInfo) item);
1027 // Fall through
1028 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1029 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1030 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1031 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1032 sBgWorkspaceItems.add(item);
1033 } else {
1034 if (!sBgFolders.containsKey(item.container)) {
1035 // Adding an item to a folder that doesn't exist.
1036 String msg = "adding item: " + item + " to a folder that " +
1037 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001038 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001040 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001041 break;
1042 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1043 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1044 break;
1045 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001046 }
1047 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001048 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001050 }
1051
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001053 * Creates a new unique child id, for a given cell span across all layouts.
1054 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001055 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001059 }
1060
Sunny Goyal34942622014-08-29 17:20:55 -07001061 private static ArrayList<ItemInfo> getItemsByPackageName(
1062 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001063 ItemInfoFilter filter = new ItemInfoFilter() {
1064 @Override
1065 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1066 return cn.getPackageName().equals(pn) && info.user.equals(user);
1067 }
1068 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001069 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001070 }
1071
1072 /**
1073 * Removes all the items from the database corresponding to the specified package.
1074 */
1075 static void deletePackageFromDatabase(Context context, final String pn,
1076 final UserHandleCompat user) {
1077 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001078 }
1079
1080 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001081 * Removes the specified item from the database
1082 * @param context
1083 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001085 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001086 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1087 items.add(item);
1088 deleteItemsFromDatabase(context, items);
1089 }
1090
1091 /**
1092 * Removes the specified items from the database
1093 * @param context
1094 * @param item
1095 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001096 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001098 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001099 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001101 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001102 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001103
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001104 // Lock on mBgLock *after* the db operation
1105 synchronized (sBgLock) {
1106 switch (item.itemType) {
1107 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1108 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001109 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001110 if (info.container == item.id) {
1111 // We are deleting a folder which still contains items that
1112 // think they are contained by that folder.
1113 String msg = "deleting a folder (" + item + ") which still " +
1114 "contains items (" + info + ")";
1115 Log.e(TAG, msg);
1116 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001118 sBgWorkspaceItems.remove(item);
1119 break;
1120 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1121 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1122 sBgWorkspaceItems.remove(item);
1123 break;
1124 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1125 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1126 break;
1127 }
1128 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001130 }
1131 }
Michael Jurka83df1882011-08-31 20:59:26 -07001132 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
1135
1136 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001137 * Update the order of the workspace screens in the database. The array list contains
1138 * a list of screen ids in the order that they should appear.
1139 */
Winson Chungc9168342013-06-26 14:54:55 -07001140 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001141 // Log to disk
1142 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1143 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1144
Winson Chung64359a52013-07-08 17:17:08 -07001145 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 final ContentResolver cr = context.getContentResolver();
1147 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1148
1149 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001150 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 while (iter.hasNext()) {
1152 long id = iter.next();
1153 if (id < 0) {
1154 iter.remove();
1155 }
1156 }
1157
1158 Runnable r = new Runnable() {
1159 @Override
1160 public void run() {
Yura085c8532014-02-11 15:15:29 +00001161 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001163 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001164 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001165 for (int i = 0; i < count; i++) {
1166 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001167 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1169 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001170 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 }
Yura085c8532014-02-11 15:15:29 +00001172
1173 try {
1174 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1175 } catch (Exception ex) {
1176 throw new RuntimeException(ex);
1177 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001178
Winson Chungba9c37f2013-08-30 14:11:37 -07001179 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001180 sBgWorkspaceScreens.clear();
1181 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001182 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001183 }
1184 };
1185 runOnWorkerThread(r);
1186 }
1187
1188 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 * Remove the contents of the specified folder from the database
1190 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001191 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 final ContentResolver cr = context.getContentResolver();
1193
Michael Jurkac9d95c52011-08-29 14:03:34 -07001194 Runnable r = new Runnable() {
1195 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001196 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001198 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 sBgItemsIdMap.remove(info.id);
1200 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 sBgWorkspaceItems.remove(info);
1202 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001203
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001204 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001205 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001207 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 for (ItemInfo childInfo : info.contents) {
1209 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001211 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001212 }
1213 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 }
1216
1217 /**
1218 * Set this as the current Launcher activity object for the loader.
1219 */
1220 public void initialize(Callbacks callbacks) {
1221 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001222 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1223 // workspace to prevent leaking Launcher activities on orientation change.
1224 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 mCallbacks = new WeakReference<Callbacks>(callbacks);
1226 }
1227 }
1228
Kenny Guyed131872014-04-30 03:02:21 +01001229 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001230 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001231 int op = PackageUpdatedTask.OP_UPDATE;
1232 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1233 user));
1234 }
1235
1236 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001237 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001238 int op = PackageUpdatedTask.OP_REMOVE;
1239 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1240 user));
1241 }
1242
1243 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001244 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001245 int op = PackageUpdatedTask.OP_ADD;
1246 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1247 user));
1248 }
1249
1250 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001251 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001252 boolean replacing) {
1253 if (!replacing) {
1254 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1255 user));
1256 if (mAppsCanBeOnRemoveableStorage) {
1257 // Only rebind if we support removable storage. It catches the
1258 // case where
1259 // apps on the external sd card need to be reloaded
1260 startLoaderFromBackground();
1261 }
1262 } else {
1263 // If we are replacing then just update the packages in the list
1264 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1265 packageNames, user));
1266 }
1267 }
1268
1269 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001270 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001271 boolean replacing) {
1272 if (!replacing) {
1273 enqueuePackageUpdated(new PackageUpdatedTask(
1274 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1275 user));
1276 }
Kenny Guyed131872014-04-30 03:02:21 +01001277 }
1278
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001279 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1281 * ACTION_PACKAGE_CHANGED.
1282 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001283 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001284 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001285 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001286
Joe Onorato36115782010-06-17 13:28:48 -04001287 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001288 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001289 // If we have changed locale we need to clear out the labels in all apps/workspace.
1290 forceReload();
1291 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1292 // Check if configuration change was an mcc/mnc change which would affect app resources
1293 // and we would need to clear out the labels in all apps/workspace. Same handling as
1294 // above for ACTION_LOCALE_CHANGED
1295 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001296 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001297 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001298 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001299 forceReload();
1300 }
1301 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001302 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001303 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1304 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001305 Callbacks callbacks = getCallback();
1306 if (callbacks != null) {
1307 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001308 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001309 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1310 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1311 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001312 }
1313 }
1314
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001315 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001316 resetLoadedState(true, true);
1317
Reena Lee93f824a2011-09-23 17:20:28 -07001318 // Do this here because if the launcher activity is running it will be restarted.
1319 // If it's not running startLoaderFromBackground will merely tell it that it needs
1320 // to reload.
1321 startLoaderFromBackground();
1322 }
1323
Winson Chungf0c6ae02012-03-21 16:10:31 -07001324 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1325 synchronized (mLock) {
1326 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1327 // mWorkspaceLoaded to true later
1328 stopLoaderLocked();
1329 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1330 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1331 }
1332 }
1333
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001334 /**
1335 * When the launcher is in the background, it's possible for it to miss paired
1336 * configuration changes. So whenever we trigger the loader from the background
1337 * tell the launcher that it needs to re-run the loader when it comes back instead
1338 * of doing it now.
1339 */
1340 public void startLoaderFromBackground() {
1341 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001342 Callbacks callbacks = getCallback();
1343 if (callbacks != null) {
1344 // Only actually run the loader if they're not paused.
1345 if (!callbacks.setLoadOnResume()) {
1346 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001347 }
1348 }
1349 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001350 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001351 }
Joe Onorato36115782010-06-17 13:28:48 -04001352 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001353
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001354 /**
1355 * If there is already a loader task running, tell it to stop.
1356 */
1357 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001358 LoaderTask oldTask = mLoaderTask;
1359 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001360 oldTask.stopLocked();
1361 }
Reena Lee93f824a2011-09-23 17:20:28 -07001362 }
1363
Adam Cohen1a85c582014-09-30 09:48:49 -07001364 public boolean isCurrentCallbacks(Callbacks callbacks) {
1365 return (mCallbacks != null && mCallbacks.get() == callbacks);
1366 }
1367
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001368 public void startLoader(int synchronousBindPage) {
1369 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001370 }
1371
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001372 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001373 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1374 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001375 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 // Clear any deferred bind-runnables from the synchronized load process
1377 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001378 synchronized (mDeferredBindRunnables) {
1379 mDeferredBindRunnables.clear();
1380 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 // Don't bother to start the thread if we know it's not going to do anything
1383 if (mCallbacks != null && mCallbacks.get() != null) {
1384 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001385 stopLoaderLocked();
1386 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001387 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001388 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1390 } else {
1391 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1392 sWorker.post(mLoaderTask);
1393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 }
1396 }
1397
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 void bindRemainingSynchronousPages() {
1399 // Post the remaining side pages to be loaded
1400 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001401 Runnable[] deferredBindRunnables = null;
1402 synchronized (mDeferredBindRunnables) {
1403 deferredBindRunnables = mDeferredBindRunnables.toArray(
1404 new Runnable[mDeferredBindRunnables.size()]);
1405 mDeferredBindRunnables.clear();
1406 }
1407 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001408 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001411
1412 // Run all the bind complete runnables after workspace is bound.
1413 if (!mBindCompleteRunnables.isEmpty()) {
1414 synchronized (mBindCompleteRunnables) {
1415 for (final Runnable r : mBindCompleteRunnables) {
1416 runOnWorkerThread(r);
1417 }
1418 mBindCompleteRunnables.clear();
1419 }
1420 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 }
1422
Joe Onorato36115782010-06-17 13:28:48 -04001423 public void stopLoader() {
1424 synchronized (mLock) {
1425 if (mLoaderTask != null) {
1426 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
1428 }
Joe Onorato36115782010-06-17 13:28:48 -04001429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001431 /**
1432 * Loads the workspace screen ids in an ordered list.
1433 */
Adam Cohen091440a2015-03-18 14:16:05 -07001434 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001435 final ContentResolver contentResolver = context.getContentResolver();
1436 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001437
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001438 // Get screens ordered by rank.
1439 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1440 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1441 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001442 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001443 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001444 while (sc.moveToNext()) {
1445 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001446 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001447 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001448 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1449 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001450 }
1451 }
1452 } finally {
1453 sc.close();
1454 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001455 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001456 }
1457
Michael Jurkac57b7a82011-08-09 22:02:20 -07001458 public boolean isAllAppsLoaded() {
1459 return mAllAppsLoaded;
1460 }
1461
Winson Chung36a62fe2012-05-06 18:04:42 -07001462 boolean isLoadingWorkspace() {
1463 synchronized (mLock) {
1464 if (mLoaderTask != null) {
1465 return mLoaderTask.isLoadingWorkspace();
1466 }
1467 }
1468 return false;
1469 }
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 /**
1472 * Runnable for the thread that loads the contents of the launcher:
1473 * - workspace icons
1474 * - widgets
1475 * - all apps icons
1476 */
1477 private class LoaderTask implements Runnable {
1478 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001479 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001480 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001481 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001482 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001484 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001485 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001486 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 }
1488
Winson Chung36a62fe2012-05-06 18:04:42 -07001489 boolean isLoadingWorkspace() {
1490 return mIsLoadingAndBindingWorkspace;
1491 }
1492
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001493 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001494 mIsLoadingAndBindingWorkspace = true;
1495
Joe Onorato36115782010-06-17 13:28:48 -04001496 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001497 if (DEBUG_LOADERS) {
1498 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001500
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001501 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001502 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001503 synchronized (LoaderTask.this) {
1504 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001505 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001506 }
1507 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 }
1509 }
1510
Joe Onorato36115782010-06-17 13:28:48 -04001511 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001512 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001514
Joe Onorato36115782010-06-17 13:28:48 -04001515 private void waitForIdle() {
1516 // Wait until the either we're stopped or the other threads are done.
1517 // This way we don't start loading all apps until the workspace has settled
1518 // down.
1519 synchronized (LoaderTask.this) {
1520 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001521
Joe Onorato36115782010-06-17 13:28:48 -04001522 mHandler.postIdle(new Runnable() {
1523 public void run() {
1524 synchronized (LoaderTask.this) {
1525 mLoadAndBindStepFinished = true;
1526 if (DEBUG_LOADERS) {
1527 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001528 }
Joe Onorato36115782010-06-17 13:28:48 -04001529 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532 });
1533
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001534 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001535 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001536 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1537 // wait no longer than 1sec at a time
1538 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001539 } catch (InterruptedException ex) {
1540 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001541 }
1542 }
Joe Onorato36115782010-06-17 13:28:48 -04001543 if (DEBUG_LOADERS) {
1544 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001545 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001546 + "ms for previous step to finish binding");
1547 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001548 }
Joe Onorato36115782010-06-17 13:28:48 -04001549 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001550
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001552 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553 // Ensure that we have a valid page index to load synchronously
1554 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1555 "valid page index");
1556 }
1557 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1558 // Ensure that we don't try and bind a specified page when the pages have not been
1559 // loaded already (we should load everything asynchronously in that case)
1560 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1561 }
1562 synchronized (mLock) {
1563 if (mIsLoaderTaskRunning) {
1564 // Ensure that we are never running the background loading at this point since
1565 // we also touch the background collections
1566 throw new RuntimeException("Error! Background loading is already running");
1567 }
1568 }
1569
1570 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1571 // data structures, we can't allow any other thread to touch that data, but because
1572 // this call is synchronous, we can get away with not locking).
1573
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001574 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001575 // operations from the previous activity. We need to ensure that all queued operations
1576 // are executed before any synchronous binding work is done.
1577 mHandler.flush();
1578
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 // Divide the set of loaded items into those that we are binding synchronously, and
1580 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001581 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001582 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1583 // arise from that.
1584 onlyBindAllApps();
1585 }
1586
Joe Onorato36115782010-06-17 13:28:48 -04001587 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001588 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001589 if (mStopped) {
1590 return;
1591 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001592 mIsLoaderTaskRunning = true;
1593 }
Joe Onorato36115782010-06-17 13:28:48 -04001594 // Optimize for end-user experience: if the Launcher is up and // running with the
1595 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1596 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001597 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001598 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001599 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001600
Joe Onorato36115782010-06-17 13:28:48 -04001601 if (mStopped) {
1602 break keep_running;
1603 }
1604
Joe Onorato36115782010-06-17 13:28:48 -04001605 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001606
1607 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001608 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1609 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001610 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611
Joe Onorato36115782010-06-17 13:28:48 -04001612 // Clear out this reference, otherwise we end up holding it until all of the
1613 // callback runnables are done.
1614 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615
Joe Onorato36115782010-06-17 13:28:48 -04001616 synchronized (mLock) {
1617 // If we are still the last one to be scheduled, remove ourselves.
1618 if (mLoaderTask == this) {
1619 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001620 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001621 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001622 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001623 }
Joe Onorato36115782010-06-17 13:28:48 -04001624 }
1625
1626 public void stopLocked() {
1627 synchronized (LoaderTask.this) {
1628 mStopped = true;
1629 this.notify();
1630 }
1631 }
1632
1633 /**
1634 * Gets the callbacks object. If we've been stopped, or if the launcher object
1635 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1636 * object that was around when the deferred message was scheduled, and if there's
1637 * a new Callbacks object around then also return null. This will save us from
1638 * calling onto it with data that will be ignored.
1639 */
1640 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1641 synchronized (mLock) {
1642 if (mStopped) {
1643 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001644 }
Joe Onorato36115782010-06-17 13:28:48 -04001645
1646 if (mCallbacks == null) {
1647 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001648 }
Joe Onorato36115782010-06-17 13:28:48 -04001649
1650 final Callbacks callbacks = mCallbacks.get();
1651 if (callbacks != oldCallbacks) {
1652 return null;
1653 }
1654 if (callbacks == null) {
1655 Log.w(TAG, "no mCallbacks");
1656 return null;
1657 }
1658
1659 return callbacks;
1660 }
1661 }
1662
1663 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001664 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001665 LauncherAppState app = LauncherAppState.getInstance();
1666 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001667 final int countX = (int) grid.numColumns;
1668 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001669
Adam Cohendcd297f2013-06-18 13:13:40 -07001670 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001671 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001672 // Return early if we detect that an item is under the hotseat button
1673 if (mCallbacks == null ||
1674 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001675 Log.e(TAG, "Error loading shortcut into hotseat " + item
1676 + " into position (" + item.screenId + ":" + item.cellX + ","
1677 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001678 return false;
1679 }
1680
Dan Sandler295ae182013-12-10 16:05:47 -05001681 final ItemInfo[][] hotseatItems =
1682 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1683
Adam Cohenae4409d2013-11-26 10:34:59 -08001684 if (item.screenId >= grid.numHotseatIcons) {
1685 Log.e(TAG, "Error loading shortcut " + item
1686 + " into hotseat position " + item.screenId
1687 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1688 + ")");
1689 return false;
1690 }
1691
Dan Sandler295ae182013-12-10 16:05:47 -05001692 if (hotseatItems != null) {
1693 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001694 Log.e(TAG, "Error loading shortcut into hotseat " + item
1695 + " into position (" + item.screenId + ":" + item.cellX + ","
1696 + item.cellY + ") occupied by "
1697 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1698 [(int) item.screenId][0]);
1699 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001700 } else {
1701 hotseatItems[(int) item.screenId][0] = item;
1702 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001704 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001705 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001706 items[(int) item.screenId][0] = item;
1707 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001708 return true;
1709 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001710 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1711 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001712 return true;
1713 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001714
Adam Cohendcd297f2013-06-18 13:13:40 -07001715 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001716 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 occupied.put(item.screenId, items);
1718 }
1719
Dan Sandler295ae182013-12-10 16:05:47 -05001720 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001721 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1722 item.cellX < 0 || item.cellY < 0 ||
1723 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1724 Log.e(TAG, "Error loading shortcut " + item
1725 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1726 + item.cellX + "," + item.cellY
1727 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1728 return false;
1729 }
1730
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001731 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001732 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1733 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001734 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001735 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001737 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001738 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001739 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001740 return false;
1741 }
1742 }
1743 }
1744 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1745 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001746 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001747 }
1748 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001749
Joe Onorato36115782010-06-17 13:28:48 -04001750 return true;
1751 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungba9c37f2013-08-30 14:11:37 -07001753 /** Clears all the sBg data structures */
1754 private void clearSBgDataStructures() {
1755 synchronized (sBgLock) {
1756 sBgWorkspaceItems.clear();
1757 sBgAppWidgets.clear();
1758 sBgFolders.clear();
1759 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001760 sBgWorkspaceScreens.clear();
1761 }
1762 }
1763
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001764 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001765 // Log to disk
1766 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1767
Joe Onorato36115782010-06-17 13:28:48 -04001768 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769
Joe Onorato36115782010-06-17 13:28:48 -04001770 final Context context = mContext;
1771 final ContentResolver contentResolver = context.getContentResolver();
1772 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001773 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001774 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1775 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001776 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001777
Winson Chung892c74d2013-08-22 16:15:50 -07001778 LauncherAppState app = LauncherAppState.getInstance();
1779 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1780 int countX = (int) grid.numColumns;
1781 int countY = (int) grid.numRows;
1782
Dan Sandlerd5024042014-01-09 15:01:33 -05001783 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1784 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1785 LauncherAppState.getLauncherProvider().deleteDatabase();
1786 }
1787
1788 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1789 // append the user's Launcher2 shortcuts
1790 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1791 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1792 } else {
1793 // Make sure the default workspace is loaded
1794 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001795 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001796 }
Adam Cohene25af792013-06-06 23:08:25 -07001797
Winson Chung2abf94d2012-07-18 18:16:38 -07001798 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001799 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001800 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001801 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001802
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001803 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001804 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001805 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001806 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001807 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001808
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 // +1 for the hotseat (it can be larger than the workspace)
1810 // Load workspace in reverse order to ensure that latest items are loaded first (and
1811 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001812 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001814 try {
1815 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1816 final int intentIndex = c.getColumnIndexOrThrow
1817 (LauncherSettings.Favorites.INTENT);
1818 final int titleIndex = c.getColumnIndexOrThrow
1819 (LauncherSettings.Favorites.TITLE);
1820 final int iconTypeIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.ICON_TYPE);
1822 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1823 final int iconPackageIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.ICON_PACKAGE);
1825 final int iconResourceIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.ICON_RESOURCE);
1827 final int containerIndex = c.getColumnIndexOrThrow(
1828 LauncherSettings.Favorites.CONTAINER);
1829 final int itemTypeIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.ITEM_TYPE);
1831 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001833 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1834 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 final int screenIndex = c.getColumnIndexOrThrow(
1836 LauncherSettings.Favorites.SCREEN);
1837 final int cellXIndex = c.getColumnIndexOrThrow
1838 (LauncherSettings.Favorites.CELLX);
1839 final int cellYIndex = c.getColumnIndexOrThrow
1840 (LauncherSettings.Favorites.CELLY);
1841 final int spanXIndex = c.getColumnIndexOrThrow
1842 (LauncherSettings.Favorites.SPANX);
1843 final int spanYIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001845 final int rankIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001847 final int restoredIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001849 final int profileIdIndex = c.getColumnIndexOrThrow(
1850 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001851 final int optionsIndex = c.getColumnIndexOrThrow(
1852 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853
Sunny Goyal7f834d22015-04-21 10:10:23 -07001854 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1855 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1856 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1857 }
1858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 ShortcutInfo info;
1860 String intentDescription;
1861 LauncherAppWidgetInfo appWidgetInfo;
1862 int container;
1863 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001864 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001866 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 while (!mStopped && c.moveToNext()) {
1869 try {
1870 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001871 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001872 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001873
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 switch (itemType) {
1875 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1876 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001877 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001879 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001880 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001881 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001882 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001883 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001884 if (user == null) {
1885 // User has been deleted remove the item.
1886 itemsToRemove.add(id);
1887 continue;
1888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 try {
1890 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001891 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001892 if (cn != null && cn.getPackageName() != null) {
1893 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1894 cn.getPackageName(), user);
1895 boolean validComponent = validPkg &&
1896 launcherApps.isActivityEnabledForProfile(cn, user);
1897
1898 if (validComponent) {
1899 if (restored) {
1900 // no special handling necessary for this item
1901 restoredRows.add(id);
1902 restored = false;
1903 }
1904 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001905 intent = null;
1906 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1907 // We allow auto install apps to have their intent
1908 // updated after an install.
1909 intent = manager.getLaunchIntentForPackage(
1910 cn.getPackageName());
1911 if (intent != null) {
1912 ContentValues values = new ContentValues();
1913 values.put(LauncherSettings.Favorites.INTENT,
1914 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001915 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001916 }
1917 }
1918
1919 if (intent == null) {
1920 // The app is installed but the component is no
1921 // longer available.
1922 Launcher.addDumpLog(TAG,
1923 "Invalid component removed: " + cn, true);
1924 itemsToRemove.add(id);
1925 continue;
1926 } else {
1927 // no special handling necessary for this item
1928 restoredRows.add(id);
1929 restored = false;
1930 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001931 } else if (restored) {
1932 // Package is not yet available but might be
1933 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001934 Launcher.addDumpLog(TAG,
1935 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001936
1937 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1938 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001939 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001940 // App restore has started. Update the flag
1941 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1942 ContentValues values = new ContentValues();
1943 values.put(LauncherSettings.Favorites.RESTORED,
1944 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001945 updateItem(id, values);
1946 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1947 // This is a common app. Try to replace this.
1948 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1949 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1950 if (parser.findDefaultApp()) {
1951 // Default app found. Replace it.
1952 intent = parser.parsedIntent;
1953 cn = intent.getComponent();
1954 ContentValues values = parser.parsedValues;
1955 values.put(LauncherSettings.Favorites.RESTORED, 0);
1956 updateItem(id, values);
1957 restored = false;
1958 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001959
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001960 } else if (REMOVE_UNRESTORED_ICONS) {
1961 Launcher.addDumpLog(TAG,
1962 "Unrestored package removed: " + cn, true);
1963 itemsToRemove.add(id);
1964 continue;
1965 }
Sunny Goyal94485362014-09-18 16:13:58 -07001966 } else if (REMOVE_UNRESTORED_ICONS) {
1967 Launcher.addDumpLog(TAG,
1968 "Unrestored package removed: " + cn, true);
1969 itemsToRemove.add(id);
1970 continue;
1971 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001972 } else if (launcherApps.isAppEnabled(
1973 manager, cn.getPackageName(),
1974 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1975 // Package is present but not available.
1976 allowMissingTarget = true;
1977 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1978 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001979 // SdCard is not ready yet. Package might get available,
1980 // once it is ready.
1981 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1982 + " (check again later)", true);
1983 HashSet<String> pkgs = sPendingPackages.get(user);
1984 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001985 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001986 sPendingPackages.put(user, pkgs);
1987 }
1988 pkgs.add(cn.getPackageName());
1989 allowMissingTarget = true;
1990 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001991
1992 } else {
1993 // Do not wait for external media load anymore.
1994 // Log the invalid package, and remove it
1995 Launcher.addDumpLog(TAG,
1996 "Invalid package removed: " + cn, true);
1997 itemsToRemove.add(id);
1998 continue;
Winson Chungee055712013-07-30 14:46:24 -07001999 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002000 } else if (cn == null) {
2001 // For shortcuts with no component, keep them as they are
2002 restoredRows.add(id);
2003 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002004 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002006 Launcher.addDumpLog(TAG,
2007 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 continue;
2009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002010
Sunny Goyal34b65272015-03-11 16:56:52 -07002011 container = c.getInt(containerIndex);
2012 boolean useLowResIcon = container >= 0 &&
2013 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2014
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002015 if (itemReplaced) {
2016 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002017 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002018 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002019 } else {
2020 // Don't replace items for other profiles.
2021 itemsToRemove.add(id);
2022 continue;
2023 }
2024 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002025 if (user.equals(UserHandleCompat.myUserHandle())) {
2026 Launcher.addDumpLog(TAG,
2027 "constructing info for partially restored package",
2028 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002029 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07002030 promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002031 intent = getRestoredItemIntent(c, context, intent);
2032 } else {
2033 // Don't restore items for other profiles.
2034 itemsToRemove.add(id);
2035 continue;
2036 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002037 } else if (itemType ==
2038 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002039 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002040 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 } else {
2042 info = getShortcutInfo(c, context, iconTypeIndex,
2043 iconPackageIndex, iconResourceIndex, iconIndex,
2044 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002045
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 // App shortcuts that used to be automatically added to Launcher
2047 // didn't always have the correct intent flags set, so do that
2048 // here
2049 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002050 intent.getCategories() != null &&
2051 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002052 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 intent.addFlags(
2054 Intent.FLAG_ACTIVITY_NEW_TASK |
2055 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2056 }
Michael Jurka96879562012-03-22 05:54:33 -07002057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002060 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 info.cellX = c.getInt(cellXIndex);
2065 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002066 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002067 info.spanX = 1;
2068 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002069 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002070 info.isDisabled = disabledState;
2071 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2072 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2073 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002074
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002076 if (!checkItemPlacement(occupied, info)) {
2077 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 break;
2079 }
2080
Sunny Goyal756adbc2015-04-16 15:20:51 -07002081 if (restored) {
2082 ComponentName cn = info.getTargetComponent();
2083 if (cn != null) {
2084 Integer progress = installingPkgs.get(cn.getPackageName());
2085 if (progress != null) {
2086 info.setInstallProgress(progress);
2087 } else {
2088 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2089 }
2090 }
2091 }
2092
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 switch (container) {
2094 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2095 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2096 sBgWorkspaceItems.add(info);
2097 break;
2098 default:
2099 // Item is in a user folder
2100 FolderInfo folderInfo =
2101 findOrMakeFolder(sBgFolders, container);
2102 folderInfo.add(info);
2103 break;
2104 }
2105 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002106 } else {
2107 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 }
2109 break;
2110
2111 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2112 id = c.getLong(idIndex);
2113 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2114
Sunny Goyala508e4f2015-05-21 09:33:57 -07002115 // Do not trim the folder label, as is was set by the user.
2116 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 folderInfo.cellX = c.getInt(cellXIndex);
2122 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002123 folderInfo.spanX = 1;
2124 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002125 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126
Daniel Sandler8802e962010-05-26 16:28:16 -04002127 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002128 if (!checkItemPlacement(occupied, folderInfo)) {
2129 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002130 break;
2131 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002132
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2135 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2136 sBgWorkspaceItems.add(folderInfo);
2137 break;
Joe Onorato36115782010-06-17 13:28:48 -04002138 }
Joe Onorato17a89222011-02-08 17:26:11 -08002139
Chris Wrenf4d08112014-01-16 18:13:56 -05002140 if (restored) {
2141 // no special handling required for restored folders
2142 restoredRows.add(id);
2143 }
2144
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2146 sBgFolders.put(folderInfo.id, folderInfo);
2147 break;
2148
2149 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002150 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002152 boolean customWidget = itemType ==
2153 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2154
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002156 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002157 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002158 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002159 user = allUsers.get(serialNumber);
2160 if (user == null) {
2161 itemsToRemove.add(id);
2162 continue;
2163 }
2164
Sunny Goyalff572272014-07-23 13:58:07 -07002165 final ComponentName component =
2166 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002167
Sunny Goyal651077b2014-06-30 14:15:31 -07002168 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002169 final boolean isIdValid = (restoreStatus &
2170 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002171 final boolean wasProviderReady = (restoreStatus &
2172 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002173
Adam Cohen59400422014-03-05 18:07:04 -08002174 final LauncherAppWidgetProviderInfo provider =
2175 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002176 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002177 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002178
2179 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002180 if (!isSafeMode && !customWidget &&
2181 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002182 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002183 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002184
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002185 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002186 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 itemsToRemove.add(id);
2188 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002189 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002190 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2191 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002192
2193 if (!customWidget) {
2194 int[] minSpan =
2195 Launcher.getMinSpanForWidget(context, provider);
2196 appWidgetInfo.minSpanX = minSpan[0];
2197 appWidgetInfo.minSpanY = minSpan[1];
2198 }
Sunny Goyalff572272014-07-23 13:58:07 -07002199
2200 int status = restoreStatus;
2201 if (!wasProviderReady) {
2202 // If provider was not previously ready, update the
2203 // status and UI flag.
2204
2205 // Id would be valid only if the widget restore broadcast was received.
2206 if (isIdValid) {
2207 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2208 } else {
2209 status &= ~LauncherAppWidgetInfo
2210 .FLAG_PROVIDER_NOT_READY;
2211 }
2212 }
2213 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002214 } else {
2215 Log.v(TAG, "Widget restore pending id=" + id
2216 + " appWidgetId=" + appWidgetId
2217 + " status =" + restoreStatus);
2218 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002219 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002220 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002221 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002222
2223 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2224 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002225 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002226 // App restore has started. Update the flag
2227 appWidgetInfo.restoreStatus |=
2228 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002229 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002230 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002231 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002232 itemsToRemove.add(id);
2233 continue;
2234 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002235
2236 appWidgetInfo.installProgress =
2237 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002238 }
Sunny Goyalff572272014-07-23 13:58:07 -07002239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 appWidgetInfo.cellX = c.getInt(cellXIndex);
2243 appWidgetInfo.cellY = c.getInt(cellYIndex);
2244 appWidgetInfo.spanX = c.getInt(spanXIndex);
2245 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002246
Adam Cohen59400422014-03-05 18:07:04 -08002247 if (!customWidget) {
2248 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2249 appWidgetInfo.minSpanX = minSpan[0];
2250 appWidgetInfo.minSpanY = minSpan[1];
2251 }
2252
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 container = c.getInt(containerIndex);
2254 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2255 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2256 Log.e(TAG, "Widget found where container != " +
2257 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2258 continue;
2259 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260
Adam Cohene25af792013-06-06 23:08:25 -07002261 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002262 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002263 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2264 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 break;
2266 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002267
Adam Cohen59400422014-03-05 18:07:04 -08002268 if (!customWidget) {
2269 String providerName =
2270 appWidgetInfo.providerName.flattenToString();
2271 if (!providerName.equals(savedProvider) ||
2272 (appWidgetInfo.restoreStatus != restoreStatus)) {
2273 ContentValues values = new ContentValues();
2274 values.put(
2275 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2276 providerName);
2277 values.put(LauncherSettings.Favorites.RESTORED,
2278 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002279 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002280 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002281 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2283 sBgAppWidgets.add(appWidgetInfo);
2284 }
Joe Onorato36115782010-06-17 13:28:48 -04002285 break;
2286 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002288 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002289 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 }
2291 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002292 if (c != null) {
2293 c.close();
2294 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 }
2296
Winson Chungba9c37f2013-08-30 14:11:37 -07002297 // Break early if we've stopped loading
2298 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002299 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002300 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002301 }
2302
Sunny Goyal1936ec02015-05-12 11:03:04 -07002303 // Remove any empty folder
2304 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2305 for (ItemInfo item: sBgItemsIdMap) {
2306 long container = item.container;
2307 if (emptyFolders.containsKey(container)) {
2308 emptyFolders.remove(container);
2309 }
2310 }
2311 for (FolderInfo folder : emptyFolders) {
2312 long folderId = folder.id;
2313 sBgFolders.remove(folderId);
2314 sBgItemsIdMap.remove(folderId);
2315 sBgWorkspaceItems.remove(folder);
2316 itemsToRemove.add(folderId);
2317 }
2318
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 if (itemsToRemove.size() > 0) {
2320 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002321 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 // Remove dead items
2323 for (long id : itemsToRemove) {
2324 if (DEBUG_LOADERS) {
2325 Log.d(TAG, "Removed id = " + id);
2326 }
2327 // Don't notify content observers
2328 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002329 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 } catch (RemoteException e) {
2331 Log.w(TAG, "Could not remove id = " + id);
2332 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002333 }
2334 }
2335
Chris Wrenf4d08112014-01-16 18:13:56 -05002336 if (restoredRows.size() > 0) {
2337 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002338 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002339 // Update restored items that no longer require special handling
2340 try {
2341 StringBuilder selectionBuilder = new StringBuilder();
2342 selectionBuilder.append(LauncherSettings.Favorites._ID);
2343 selectionBuilder.append(" IN (");
2344 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2345 selectionBuilder.append(")");
2346 ContentValues values = new ContentValues();
2347 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002348 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002349 values, selectionBuilder.toString(), null);
2350 } catch (RemoteException e) {
2351 Log.w(TAG, "Could not update restored rows");
2352 }
2353 }
2354
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002355 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2356 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002357 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002358 null, sWorker);
2359 }
2360
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002361 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2362 // Log to disk
2363 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2364 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002365
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002366 // Remove any empty screens
2367 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002368 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002369 long screenId = item.screenId;
2370 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2371 unusedScreens.contains(screenId)) {
2372 unusedScreens.remove(screenId);
2373 }
2374 }
2375
2376 // If there are any empty screens remove them, and update.
2377 if (unusedScreens.size() != 0) {
2378 // Log to disk
2379 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2380 TextUtils.join(", ", unusedScreens), true);
2381
2382 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002383 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002384 }
2385
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 if (DEBUG_LOADERS) {
2387 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2388 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002389 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002390 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002392
Sunny Goyale2df0622015-04-24 11:27:00 -07002393 for (int i = 0; i < nScreens; i++) {
2394 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002395 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 line += " | ";
2397 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002398 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002399 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002400 if (x < screen.length && y < screen[x].length) {
2401 line += (screen[x][y] != null) ? "#" : ".";
2402 } else {
2403 line += "!";
2404 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 }
Joe Onorato36115782010-06-17 13:28:48 -04002406 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002408 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002409 }
Joe Onorato36115782010-06-17 13:28:48 -04002410 }
Adam Cohene25af792013-06-06 23:08:25 -07002411 }
2412
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002413 /**
2414 * Partially updates the item without any notification. Must be called on the worker thread.
2415 */
2416 private void updateItem(long itemId, ContentValues update) {
2417 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002418 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002419 update,
2420 BaseColumns._ID + "= ?",
2421 new String[]{Long.toString(itemId)});
2422 }
2423
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 /** Filters the set of items who are directly or indirectly (via another container) on the
2425 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002426 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 ArrayList<ItemInfo> allWorkspaceItems,
2428 ArrayList<ItemInfo> currentScreenItems,
2429 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002430 // Purge any null ItemInfos
2431 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2432 while (iter.hasNext()) {
2433 ItemInfo i = iter.next();
2434 if (i == null) {
2435 iter.remove();
2436 }
2437 }
2438
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439 // Order the set of items by their containers first, this allows use to walk through the
2440 // list sequentially, build up a list of containers that are in the specified screen,
2441 // as well as all items in those containers.
2442 Set<Long> itemsOnScreen = new HashSet<Long>();
2443 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2444 @Override
2445 public int compare(ItemInfo lhs, ItemInfo rhs) {
2446 return (int) (lhs.container - rhs.container);
2447 }
2448 });
2449 for (ItemInfo info : allWorkspaceItems) {
2450 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002451 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 currentScreenItems.add(info);
2453 itemsOnScreen.add(info.id);
2454 } else {
2455 otherScreenItems.add(info);
2456 }
2457 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2458 currentScreenItems.add(info);
2459 itemsOnScreen.add(info.id);
2460 } else {
2461 if (itemsOnScreen.contains(info.container)) {
2462 currentScreenItems.add(info);
2463 itemsOnScreen.add(info.id);
2464 } else {
2465 otherScreenItems.add(info);
2466 }
2467 }
2468 }
2469 }
2470
2471 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002472 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 ArrayList<LauncherAppWidgetInfo> appWidgets,
2474 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2475 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476
2477 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002478 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002480 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 currentScreenWidgets.add(widget);
2482 } else {
2483 otherScreenWidgets.add(widget);
2484 }
2485 }
2486 }
2487
2488 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002489 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002490 LongArrayMap<ItemInfo> itemsIdMap,
2491 LongArrayMap<FolderInfo> folders,
2492 LongArrayMap<FolderInfo> currentScreenFolders,
2493 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494
Sunny Goyale2df0622015-04-24 11:27:00 -07002495 int total = folders.size();
2496 for (int i = 0; i < total; i++) {
2497 long id = folders.keyAt(i);
2498 FolderInfo folder = folders.valueAt(i);
2499
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002501 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002503 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 currentScreenFolders.put(id, folder);
2505 } else {
2506 otherScreenFolders.put(id, folder);
2507 }
2508 }
2509 }
2510
2511 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2512 * right) */
2513 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002514 final LauncherAppState app = LauncherAppState.getInstance();
2515 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 // XXX: review this
2517 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002518 @Override
2519 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002520 int cellCountX = (int) grid.numColumns;
2521 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002522 int screenOffset = cellCountX * cellCountY;
2523 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002524 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002525 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002526 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002527 rhs.cellY * cellCountX + rhs.cellX);
2528 return (int) (lr - rr);
2529 }
2530 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002532
Adam Cohendcd297f2013-06-18 13:13:40 -07002533 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2534 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002535 final Runnable r = new Runnable() {
2536 @Override
2537 public void run() {
2538 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2539 if (callbacks != null) {
2540 callbacks.bindScreens(orderedScreens);
2541 }
2542 }
2543 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002544 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002545 }
2546
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2548 final ArrayList<ItemInfo> workspaceItems,
2549 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002550 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002552
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553 final boolean postOnMainThread = (deferredBindRunnables != null);
2554
2555 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002556 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002558 final int start = i;
2559 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 final Runnable r = new Runnable() {
2561 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002562 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002563 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002564 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002565 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2566 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 }
2568 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569 };
2570 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002571 synchronized (deferredBindRunnables) {
2572 deferredBindRunnables.add(r);
2573 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002575 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 }
Joe Onorato36115782010-06-17 13:28:48 -04002577 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578
2579 // Bind the folders
2580 if (!folders.isEmpty()) {
2581 final Runnable r = new Runnable() {
2582 public void run() {
2583 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2584 if (callbacks != null) {
2585 callbacks.bindFolders(folders);
2586 }
2587 }
2588 };
2589 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002590 synchronized (deferredBindRunnables) {
2591 deferredBindRunnables.add(r);
2592 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002594 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 }
2596 }
2597
2598 // Bind the widgets, one at a time
2599 N = appWidgets.size();
2600 for (int i = 0; i < N; i++) {
2601 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2602 final Runnable r = new Runnable() {
2603 public void run() {
2604 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2605 if (callbacks != null) {
2606 callbacks.bindAppWidget(widget);
2607 }
2608 }
2609 };
2610 if (postOnMainThread) {
2611 deferredBindRunnables.add(r);
2612 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002613 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002614 }
2615 }
2616 }
2617
2618 /**
2619 * Binds all loaded data to actual views on the main thread.
2620 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002621 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622 final long t = SystemClock.uptimeMillis();
2623 Runnable r;
2624
2625 // Don't use these two variables in any of the callback runnables.
2626 // Otherwise we hold a reference to them.
2627 final Callbacks oldCallbacks = mCallbacks.get();
2628 if (oldCallbacks == null) {
2629 // This launcher has exited and nobody bothered to tell us. Just bail.
2630 Log.w(TAG, "LoaderTask running with no launcher");
2631 return;
2632 }
2633
Winson Chung9b9fb962013-11-15 15:39:34 -08002634 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2636 ArrayList<LauncherAppWidgetInfo> appWidgets =
2637 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002638 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002639
2640 final LongArrayMap<FolderInfo> folders;
2641 final LongArrayMap<ItemInfo> itemsIdMap;
2642
Winson Chung2abf94d2012-07-18 18:16:38 -07002643 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002644 workspaceItems.addAll(sBgWorkspaceItems);
2645 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002646 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002647
2648 folders = sBgFolders.clone();
2649 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002650 }
2651
Derek Prothro7aff3992013-12-10 14:00:37 -05002652 final boolean isLoadingSynchronously =
2653 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002654 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002655 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002656 if (currScreen >= orderedScreenIds.size()) {
2657 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002658 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002659 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002660 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002661 final long currentScreenId = currentScreen < 0
2662 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002663
2664 // Load all the items that are on the current page first (and in the process, unbind
2665 // all the existing workspace items before we call startBinding() below.
2666 unbindWorkspaceItemsOnMainThread();
2667
2668 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002669 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2670 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2671 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2672 new ArrayList<LauncherAppWidgetInfo>();
2673 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2674 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002675 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2676 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002677
Winson Chung9b9fb962013-11-15 15:39:34 -08002678 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002679 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002680 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002681 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002682 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002683 otherFolders);
2684 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2685 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2686
2687 // Tell the workspace that we're about to start binding items
2688 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002689 public void run() {
2690 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2691 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002692 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002693 }
2694 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002695 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002696 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002697
Adam Cohendcd297f2013-06-18 13:13:40 -07002698 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2699
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002700 // Load items on the current page
2701 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2702 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002703 if (isLoadingSynchronously) {
2704 r = new Runnable() {
2705 public void run() {
2706 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002707 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002708 callbacks.onPageBoundSynchronously(currentScreen);
2709 }
2710 }
2711 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002712 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002713 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002714
Winson Chung4a2afa32012-07-19 14:53:05 -07002715 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2716 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002717 synchronized (mDeferredBindRunnables) {
2718 mDeferredBindRunnables.clear();
2719 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002720 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002721 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002722
2723 // Tell the workspace that we're done binding items
2724 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002725 public void run() {
2726 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2727 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002728 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002729 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002730
Winson Chung98e030b2012-05-07 16:01:11 -07002731 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002732 if (DEBUG_LOADERS) {
2733 Log.d(TAG, "bound workspace in "
2734 + (SystemClock.uptimeMillis()-t) + "ms");
2735 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002736
2737 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002738 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002739 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002740 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002741 synchronized (mDeferredBindRunnables) {
2742 mDeferredBindRunnables.add(r);
2743 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002744 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002745 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002746 }
Joe Onorato36115782010-06-17 13:28:48 -04002747 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002748
Joe Onorato36115782010-06-17 13:28:48 -04002749 private void loadAndBindAllApps() {
2750 if (DEBUG_LOADERS) {
2751 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2752 }
2753 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002754 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002755 synchronized (LoaderTask.this) {
2756 if (mStopped) {
2757 return;
2758 }
2759 }
Sunny Goyal75b0f552015-05-20 21:57:06 -07002760 mIconCache.updateDbIcons();
Reena Lee93f824a2011-09-23 17:20:28 -07002761 synchronized (LoaderTask.this) {
2762 if (mStopped) {
2763 return;
2764 }
2765 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002766 }
Joe Onorato36115782010-06-17 13:28:48 -04002767 } else {
2768 onlyBindAllApps();
2769 }
2770 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002771
Joe Onorato36115782010-06-17 13:28:48 -04002772 private void onlyBindAllApps() {
2773 final Callbacks oldCallbacks = mCallbacks.get();
2774 if (oldCallbacks == null) {
2775 // This launcher has exited and nobody bothered to tell us. Just bail.
2776 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2777 return;
2778 }
2779
2780 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002781 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002782 final ArrayList<AppInfo> list
2783 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002784 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002785 public void run() {
2786 final long t = SystemClock.uptimeMillis();
2787 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2788 if (callbacks != null) {
2789 callbacks.bindAllApplications(list);
2790 }
2791 if (DEBUG_LOADERS) {
2792 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2793 + (SystemClock.uptimeMillis()-t) + "ms");
2794 }
2795 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002796 };
2797 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002798 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002799 r.run();
2800 } else {
2801 mHandler.post(r);
2802 }
Hyunyoung Song219d0482015-05-07 12:51:42 -07002803 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2804 false /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002805 }
2806
Winson Chung64359a52013-07-08 17:17:08 -07002807 private void loadAllApps() {
2808 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002809
Joe Onorato36115782010-06-17 13:28:48 -04002810 final Callbacks oldCallbacks = mCallbacks.get();
2811 if (oldCallbacks == null) {
2812 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002813 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002814 return;
2815 }
2816
Kenny Guyed131872014-04-30 03:02:21 +01002817 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2818
Winson Chung64359a52013-07-08 17:17:08 -07002819 // Clear the list of apps
2820 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002821 for (UserHandleCompat user : profiles) {
2822 // Query for the set of apps
2823 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002824 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002825 if (DEBUG_LOADERS) {
2826 Log.d(TAG, "getActivityList took "
2827 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2828 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2829 }
2830 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002831 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002832 if (apps == null || apps.isEmpty()) {
2833 return;
2834 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002835
Kenny Guyed131872014-04-30 03:02:21 +01002836 // Create the ApplicationInfos
2837 for (int i = 0; i < apps.size(); i++) {
2838 LauncherActivityInfoCompat app = apps.get(i);
2839 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002840 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002841 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002842
Sunny Goyal756a28a2015-04-23 17:07:55 -07002843 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2844 if (heuristic != null) {
2845 runAfterBindCompletes(new Runnable() {
2846
2847 @Override
2848 public void run() {
2849 heuristic.processUserApps(apps);
2850 }
2851 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002852 }
Winson Chung64359a52013-07-08 17:17:08 -07002853 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002854 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002855 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2856 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002857
2858 // Post callback on main thread
2859 mHandler.post(new Runnable() {
2860 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002861
Winson Chung64359a52013-07-08 17:17:08 -07002862 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002863 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002864 if (callbacks != null) {
2865 callbacks.bindAllApplications(added);
2866 if (DEBUG_LOADERS) {
2867 Log.d(TAG, "bound " + added.size() + " apps in "
2868 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2869 }
2870 } else {
2871 Log.i(TAG, "not binding apps: no Launcher activity");
2872 }
2873 }
2874 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002875 // Cleanup any data stored for a deleted user.
2876 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002877
Hyunyoung Song219d0482015-05-07 12:51:42 -07002878 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2879 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002880 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002881 Log.d(TAG, "Icons processed in "
2882 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002883 }
2884 }
2885
2886 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002887 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002888 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002889 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2890 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2891 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2892 }
Joe Onorato36115782010-06-17 13:28:48 -04002893 }
2894 }
2895
Sunny Goyal75b0f552015-05-20 21:57:06 -07002896 /**
2897 * Called when the icons for packages have been updated in the icon cache.
2898 */
2899 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2900 final Callbacks callbacks = getCallback();
2901 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2902 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2903
2904 // If any package icon has changed (app was updated while launcher was dead),
2905 // update the corresponding shortcuts.
2906 synchronized (sBgLock) {
2907 for (ItemInfo info : sBgItemsIdMap) {
2908 if (info instanceof ShortcutInfo && user.equals(info.user)
2909 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2910 ShortcutInfo si = (ShortcutInfo) info;
2911 ComponentName cn = si.getTargetComponent();
2912 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2913 si.updateIcon(mIconCache);
2914 updatedShortcuts.add(si);
2915 }
2916 }
2917 }
2918 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2919 }
2920
2921 if (!updatedShortcuts.isEmpty()) {
2922 final UserHandleCompat userFinal = user;
2923 mHandler.post(new Runnable() {
2924
2925 public void run() {
2926 Callbacks cb = getCallback();
2927 if (cb != null && callbacks == cb) {
2928 cb.bindShortcutsChanged(updatedShortcuts,
2929 new ArrayList<ShortcutInfo>(), userFinal);
2930 }
2931 }
2932 });
2933 }
2934
2935 if (!updatedApps.isEmpty()) {
2936 mHandler.post(new Runnable() {
2937
2938 public void run() {
2939 Callbacks cb = getCallback();
2940 if (cb != null && callbacks == cb) {
2941 cb.bindAppsUpdated(updatedApps);
2942 }
2943 }
2944 });
2945 }
2946 }
2947
Joe Onorato36115782010-06-17 13:28:48 -04002948 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002949 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002950 }
2951
Adam Cohen091440a2015-03-18 14:16:05 -07002952 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953
2954 @Override
2955 public void onReceive(Context context, Intent intent) {
2956 synchronized (sBgLock) {
2957 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2958 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002959 final PackageManager manager = context.getPackageManager();
2960 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2961 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002962 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2963 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002964 packagesRemoved.clear();
2965 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002966 for (String pkg : entry.getValue()) {
2967 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002968 boolean packageOnSdcard = launcherApps.isAppEnabled(
2969 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2970 if (packageOnSdcard) {
2971 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2972 packagesUnavailable.add(pkg);
2973 } else {
2974 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2975 packagesRemoved.add(pkg);
2976 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002977 }
2978 }
2979 if (!packagesRemoved.isEmpty()) {
2980 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2981 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2982 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002983 if (!packagesUnavailable.isEmpty()) {
2984 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2985 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2986 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002987 }
Sunny Goyal34942622014-08-29 17:20:55 -07002988 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002989 }
2990 }
2991 }
2992
Joe Onorato36115782010-06-17 13:28:48 -04002993 private class PackageUpdatedTask implements Runnable {
2994 int mOp;
2995 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002996 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002997
2998 public static final int OP_NONE = 0;
2999 public static final int OP_ADD = 1;
3000 public static final int OP_UPDATE = 2;
3001 public static final int OP_REMOVE = 3; // uninstlled
3002 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3003
3004
Kenny Guyed131872014-04-30 03:02:21 +01003005 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003006 mOp = op;
3007 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003008 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003009 }
3010
3011 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003012 if (!mHasLoaderCompletedOnce) {
3013 // Loader has not yet run.
3014 return;
3015 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003016 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003017
3018 final String[] packages = mPackages;
3019 final int N = packages.length;
3020 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003021 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003022 for (int i=0; i<N; i++) {
3023 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003024 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003025 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003027
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003028 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3029 if (heuristic != null) {
3030 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003031 }
Joe Onorato36115782010-06-17 13:28:48 -04003032 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003033 }
Joe Onorato36115782010-06-17 13:28:48 -04003034 case OP_UPDATE:
3035 for (int i=0; i<N; i++) {
3036 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003037 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003038 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003039 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003040 }
3041 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003042 case OP_REMOVE: {
3043 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3044 if (heuristic != null) {
3045 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003046 }
Joe Onorato36115782010-06-17 13:28:48 -04003047 for (int i=0; i<N; i++) {
3048 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003049 mIconCache.removeIconsForPkg(packages[i], mUser);
3050 }
3051 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003052 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003053 case OP_UNAVAILABLE:
3054 for (int i=0; i<N; i++) {
3055 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3056 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003057 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003058 }
3059 break;
3060 }
3061
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003062 ArrayList<AppInfo> added = null;
3063 ArrayList<AppInfo> modified = null;
3064 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003065
Adam Cohen487f7dd2012-06-28 18:12:10 -07003066 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003067 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003068 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003069 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003070 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003071 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003072 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003073 }
Winson Chung5d55f332012-07-16 20:45:03 -07003074 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003075 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003076 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003077 }
3078
Sunny Goyale0f58d72014-11-10 18:05:31 -08003079 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003080 if (callbacks == null) {
3081 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3082 return;
3083 }
3084
Sunny Goyal4390ace2014-10-13 11:33:11 -07003085 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3086 new HashMap<ComponentName, AppInfo>();
3087
Joe Onorato36115782010-06-17 13:28:48 -04003088 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003089 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 for (AppInfo ai : added) {
3091 addedOrUpdatedApps.put(ai.componentName, ai);
3092 }
Joe Onorato36115782010-06-17 13:28:48 -04003093 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003094
Joe Onorato36115782010-06-17 13:28:48 -04003095 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003096 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003097 for (AppInfo ai : modified) {
3098 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003099 }
3100
Joe Onorato36115782010-06-17 13:28:48 -04003101 mHandler.post(new Runnable() {
3102 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003103 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003104 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003105 callbacks.bindAppsUpdated(modifiedFinal);
3106 }
3107 }
3108 });
3109 }
Winson Chung83892cc2013-05-01 16:53:33 -07003110
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3114 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3115 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3116
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003118 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003119 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003120 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3121 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 boolean infoUpdated = false;
3123 boolean shortcutUpdated = false;
3124
3125 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003126 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003127 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3129 si.iconResource.resourceName, mIconCache, context);
3130 if (icon != null) {
3131 si.setIcon(icon);
3132 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003134 }
3135 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136
3137 ComponentName cn = si.getTargetComponent();
3138 if (cn != null && packageSet.contains(cn.getPackageName())) {
3139 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3140
3141 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3143 // Auto install icon
3144 PackageManager pm = context.getPackageManager();
3145 ResolveInfo matched = pm.resolveActivity(
3146 new Intent(Intent.ACTION_MAIN)
3147 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3148 PackageManager.MATCH_DEFAULT_ONLY);
3149 if (matched == null) {
3150 // Try to find the best match activity.
3151 Intent intent = pm.getLaunchIntentForPackage(
3152 cn.getPackageName());
3153 if (intent != null) {
3154 cn = intent.getComponent();
3155 appInfo = addedOrUpdatedApps.get(cn);
3156 }
3157
3158 if ((intent == null) || (appInfo == null)) {
3159 removedShortcuts.add(si);
3160 continue;
3161 }
3162 si.promisedIntent = intent;
3163 }
3164 }
3165
3166 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003167 if (appInfo != null) {
3168 si.flags = appInfo.flags;
3169 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003170
Sunny Goyal756adbc2015-04-16 15:20:51 -07003171 si.intent = si.promisedIntent;
3172 si.promisedIntent = null;
3173 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174 infoUpdated = true;
3175 si.updateIcon(mIconCache);
3176 }
3177
3178 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3179 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3180 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003181 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 si.contentDescription = appInfo.contentDescription;
3183 infoUpdated = true;
3184 }
3185
3186 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3187 // Since package was just updated, the target must be available now.
3188 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3189 shortcutUpdated = true;
3190 }
3191 }
3192
3193 if (infoUpdated || shortcutUpdated) {
3194 updatedShortcuts.add(si);
3195 }
3196 if (infoUpdated) {
3197 updateItemInDatabase(context, si);
3198 }
3199 } else if (info instanceof LauncherAppWidgetInfo) {
3200 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3201 if (mUser.equals(widgetInfo.user)
3202 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3203 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3204 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3205 widgets.add(widgetInfo);
3206 updateItemInDatabase(context, widgetInfo);
3207 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003208 }
3209 }
3210 }
3211
Sunny Goyal4390ace2014-10-13 11:33:11 -07003212 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3213 mHandler.post(new Runnable() {
3214
3215 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003216 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003217 if (callbacks == cb && cb != null) {
3218 callbacks.bindShortcutsChanged(
3219 updatedShortcuts, removedShortcuts, mUser);
3220 }
3221 }
3222 });
3223 if (!removedShortcuts.isEmpty()) {
3224 deleteItemsFromDatabase(context, removedShortcuts);
3225 }
3226 }
3227 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003228 mHandler.post(new Runnable() {
3229 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003230 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003231 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003232 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003233 }
3234 }
3235 });
3236 }
3237 }
3238
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 final ArrayList<String> removedPackageNames =
3240 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003241 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003242 // Mark all packages in the broadcast to be removed
3243 removedPackageNames.addAll(Arrays.asList(packages));
3244 } else if (mOp == OP_UPDATE) {
3245 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003246 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003247 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003249 }
3250 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003252
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003254 final int removeReason;
3255 if (mOp == OP_UNAVAILABLE) {
3256 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3257 } else {
3258 // Remove all the components associated with this package
3259 for (String pn : removedPackageNames) {
3260 deletePackageFromDatabase(context, pn, mUser);
3261 }
3262 // Remove all the specific components
3263 for (AppInfo a : removedApps) {
3264 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3265 deleteItemsFromDatabase(context, infos);
3266 }
3267 removeReason = 0;
3268 }
3269
Winson Chungdf95eb12013-10-16 14:57:07 -07003270 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003271 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003272 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003273 mHandler.post(new Runnable() {
3274 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003275 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003276 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003277 callbacks.bindComponentsRemoved(
3278 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003279 }
3280 }
3281 });
Joe Onoratobe386092009-11-17 17:32:16 -08003282 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003283
3284 // onProvidersChanged method (API >= 17) already refreshed the widget list
3285 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3286
Adam Cohen4caf2982013-08-20 18:54:31 -07003287 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003288 mHandler.post(new Runnable() {
3289 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003290 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003291 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003292 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003293 }
3294 }
3295 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 }
3297 }
3298
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003299 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3300 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003301 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003302 if (sBgWidgetProviders == null || refresh) {
3303 sBgWidgetProviders = new HashMap<>();
3304 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3305 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003306
Robin Lee26ace122015-03-16 19:41:43 +00003307 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3308 for (AppWidgetProviderInfo pInfo : widgets) {
3309 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3310 UserHandleCompat user = wm.getUser(info);
3311 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3312 }
3313
3314 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3315 for (CustomAppWidget widget : customWidgets) {
3316 info = new LauncherAppWidgetProviderInfo(context, widget);
3317 UserHandleCompat user = wm.getUser(info);
3318 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3319 }
Adam Cohen59400422014-03-05 18:07:04 -08003320 }
Adam Cohen59400422014-03-05 18:07:04 -08003321 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3322 }
3323 }
3324
Robin Lee26ace122015-03-16 19:41:43 +00003325 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3326 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003327 synchronized (sBgLock) {
3328 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003329 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003330 }
Robin Lee26ace122015-03-16 19:41:43 +00003331 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003332 }
3333 }
3334
Hyunyoung Song227239e2015-05-04 18:17:35 -07003335 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3336 final boolean refresh) {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003337 runOnWorkerThread(new Runnable(){
3338 @Override
3339 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003340 final WidgetsModel model = createWidgetsModel(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 mHandler.post(new Runnable() {
3342 @Override
3343 public void run() {
3344 Callbacks cb = getCallback();
3345 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003346 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003347 }
3348 }
3349 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003350 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003351 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3352 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003353 }
3354 });
3355 }
3356
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003357 /**
3358 * Returns a list of ResolveInfos/AppWidgetInfos.
3359 *
3360 * @see #loadAndBindWidgetsAndShortcuts
3361 */
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003362 private WidgetsModel createWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003363 PackageManager packageManager = context.getPackageManager();
3364 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003365 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003366 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3367 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003368 WidgetsModel model = new WidgetsModel(context);
3369 model.addWidgetsAndShortcuts(widgetsAndShortcuts);
3370 return model;
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 }
3372
Adam Cohen091440a2015-03-18 14:16:05 -07003373 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003374 UserHandleCompat user) {
3375 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3376 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003377 }
Adam Cohen556f6132014-01-15 15:18:08 -08003378
Kenny Guyed131872014-04-30 03:02:21 +01003379 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3380 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003381 if (cn == null) {
3382 return false;
3383 }
Kenny Guyed131872014-04-30 03:02:21 +01003384 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3385 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003386 return false;
3387 }
Kenny Guyed131872014-04-30 03:02:21 +01003388 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003389 }
3390
Adam Cohena28b78e2014-05-20 17:03:04 -07003391 public static boolean isValidPackage(Context context, String packageName,
3392 UserHandleCompat user) {
3393 if (packageName == null) {
3394 return false;
3395 }
3396 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3397 return launcherApps.isPackageEnabledForProfile(packageName, user);
3398 }
3399
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003401 * Make an ShortcutInfo object for a restored application or shortcut item that points
3402 * to a package that is not yet installed on the system.
3403 */
Sunny Goyal34942622014-08-29 17:20:55 -07003404 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07003405 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003406 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003407 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal5c97f512015-05-19 16:03:28 -07003408 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
Sunny Goyal34942622014-08-29 17:20:55 -07003409
3410 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3411 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3412 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003413 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003414 }
Sunny Goyal34942622014-08-29 17:20:55 -07003415 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3416 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003417 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003418 }
Sunny Goyal34942622014-08-29 17:20:55 -07003419 } else {
3420 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3421 }
3422
Winson Chung82b016c2015-05-08 17:00:10 -07003423 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003425 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003426 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003427 return info;
3428 }
3429
3430 /**
3431 * Make an Intent object for a restored application or shortcut item that points
3432 * to the market page for the item.
3433 */
Adam Cohen091440a2015-03-18 14:16:05 -07003434 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003435 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003436 return getMarketIntent(componentName.getPackageName());
3437 }
3438
3439 static Intent getMarketIntent(String packageName) {
3440 return new Intent(Intent.ACTION_VIEW)
3441 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003442 .scheme("market")
3443 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003444 .appendQueryParameter("id", packageName)
3445 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003446 }
3447
3448 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003449 * Make an ShortcutInfo object for a shortcut that is an application.
3450 *
3451 * If c is not null, then it will be used to fill in missing data like the title and icon.
3452 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003453 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003454 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003455 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003456 if (user == null) {
3457 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003458 return null;
3459 }
3460
Kenny Guyed131872014-04-30 03:02:21 +01003461 ComponentName componentName = intent.getComponent();
3462 if (componentName == null) {
3463 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3464 return null;
3465 }
3466
3467 Intent newIntent = new Intent(intent.getAction(), null);
3468 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3469 newIntent.setComponent(componentName);
3470 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003471 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003472 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3473 return null;
3474 }
3475
3476 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003477 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003478 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3479 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3480 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003481 }
3482
Joe Onorato56d82912010-03-07 14:32:10 -05003483 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003484 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003485 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003486 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003487
Joe Onorato56d82912010-03-07 14:32:10 -05003488 // fall back to the class name of the activity
3489 if (info.title == null) {
3490 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003491 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003492
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003494 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003495 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003496 if (lai != null) {
3497 info.flags = AppInfo.initFlags(lai);
3498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003500 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003501
Sunny Goyale2df0622015-04-24 11:27:00 -07003502 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003503 ItemInfoFilter f) {
3504 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3505 for (ItemInfo i : infos) {
3506 if (i instanceof ShortcutInfo) {
3507 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003508 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003509 if (cn != null && f.filterItem(null, info, cn)) {
3510 filtered.add(info);
3511 }
3512 } else if (i instanceof FolderInfo) {
3513 FolderInfo info = (FolderInfo) i;
3514 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003515 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003516 if (cn != null && f.filterItem(info, s, cn)) {
3517 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003518 }
3519 }
Winson Chung64359a52013-07-08 17:17:08 -07003520 } else if (i instanceof LauncherAppWidgetInfo) {
3521 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3522 ComponentName cn = info.providerName;
3523 if (cn != null && f.filterItem(null, info, cn)) {
3524 filtered.add(info);
3525 }
Winson Chung8a435102012-08-30 17:16:53 -07003526 }
3527 }
Winson Chung64359a52013-07-08 17:17:08 -07003528 return new ArrayList<ItemInfo>(filtered);
3529 }
3530
Adam Cohen091440a2015-03-18 14:16:05 -07003531 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003532 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003533 ItemInfoFilter filter = new ItemInfoFilter() {
3534 @Override
3535 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003536 if (info.user == null) {
3537 return cn.equals(cname);
3538 } else {
3539 return cn.equals(cname) && info.user.equals(user);
3540 }
Winson Chung64359a52013-07-08 17:17:08 -07003541 }
3542 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003543 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003544 }
3545
Sunny Goyal1a745e82014-10-02 15:58:31 -07003546 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003547 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003548 */
Adam Cohen091440a2015-03-18 14:16:05 -07003549 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003550 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3551 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552
Joe Onorato56d82912010-03-07 14:32:10 -05003553 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003554 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003555 // Non-app shortcuts are only supported for current user.
3556 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003559 // TODO: If there's an explicit component and we can't install that, delete it.
3560
Winson Chung82b016c2015-05-08 17:00:10 -07003561 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003562
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 int iconType = c.getInt(iconTypeIndex);
3564 switch (iconType) {
3565 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3566 String packageName = c.getString(iconPackageIndex);
3567 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003568 info.customIcon = false;
3569 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003570 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003571 // the db
3572 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003573 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003574 }
3575 // the fallback icon
3576 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003577 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003578 info.usingFallbackIcon = true;
3579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 break;
3581 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003582 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003583 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003584 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003585 info.customIcon = false;
3586 info.usingFallbackIcon = true;
3587 } else {
3588 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 break;
3591 default:
Kenny Guyed131872014-04-30 03:02:21 +01003592 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003593 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 info.customIcon = false;
3595 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003597 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 return info;
3599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003600
Sunny Goyal2350bc92014-10-14 16:42:54 -07003601 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003602 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3603 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3604 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3605
Adam Cohend9198822011-11-22 16:42:47 -08003606 if (intent == null) {
3607 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3608 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3609 return null;
3610 }
3611
Joe Onorato0589f0f2010-02-08 13:44:00 -08003612 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003613 boolean customIcon = false;
3614 ShortcutIconResource iconResource = null;
3615
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003616 if (bitmap instanceof Bitmap) {
3617 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003618 customIcon = true;
3619 } else {
3620 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003621 if (extra instanceof ShortcutIconResource) {
3622 iconResource = (ShortcutIconResource) extra;
3623 icon = Utilities.createIconBitmap(iconResource.packageName,
3624 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003625 }
3626 }
3627
Michael Jurkac9d95c52011-08-29 14:03:34 -07003628 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003629
Kenny Guyed131872014-04-30 03:02:21 +01003630 // Only support intents for current user for now. Intents sent from other
3631 // users wouldn't get here without intent forwarding anyway.
3632 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003633 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003634 icon = mIconCache.getDefaultIcon(info.user);
3635 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003636 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003637 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003638
Winson Chung82b016c2015-05-08 17:00:10 -07003639 info.title = Utilities.trim(name);
3640 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003641 info.intent = intent;
3642 info.customIcon = customIcon;
3643 info.iconResource = iconResource;
3644
3645 return info;
3646 }
3647
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003649 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 * or make a new one.
3651 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003652 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 // See if a placeholder was created for us already
3654 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003655 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003657 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 folders.put(id, folderInfo);
3659 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003660 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003661 }
3662
Joe Onoratobe386092009-11-17 17:32:16 -08003663
Sunny Goyal651077b2014-06-30 14:15:31 -07003664 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3665 return (provider != null) && (provider.provider != null)
3666 && (provider.provider.getPackageName() != null);
3667 }
3668
Joe Onoratobe386092009-11-17 17:32:16 -08003669 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003670 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003671 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3672 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3673 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3674 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003675 if (mLoaderTask != null) {
3676 mLoaderTask.dumpState();
3677 } else {
3678 Log.d(TAG, "mLoaderTask=null");
3679 }
Joe Onoratobe386092009-11-17 17:32:16 -08003680 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003681
3682 public Callbacks getCallback() {
3683 return mCallbacks != null ? mCallbacks.get() : null;
3684 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003685
3686 /**
3687 * @return {@link FolderInfo} if its already loaded.
3688 */
3689 public FolderInfo findFolderById(Long folderId) {
3690 synchronized (sBgLock) {
3691 return sBgFolders.get(folderId);
3692 }
3693 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003694
3695 /**
3696 * @return the looper for the worker thread which can be used to start background tasks.
3697 */
3698 public static Looper getWorkerLooper() {
3699 return sWorkerThread.getLooper();
3700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003701}