blob: 41f36eb04f656153384b4235a2772d67ce5e75ce [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070036import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale5bb7052015-07-27 14:36:07 -070058import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070059import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000060import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070061import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070062import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080065
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.lang.ref.WeakReference;
67import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070068import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070070import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070071import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.Collections;
73import java.util.Comparator;
74import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070075import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070076import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070078import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070079import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081/**
82 * Maintains in-memory state of the Launcher. It is expected that there should be only one
83 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070084 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 */
Kenny Guyed131872014-04-30 03:02:21 +010086public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010087 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080088 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040089 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070090 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040091
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070093
Dan Sandlerd5024042014-01-09 15:01:33 -050094 public static final int LOADER_FLAG_NONE = 0;
95 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
96 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
97
Joe Onorato36115782010-06-17 13:28:48 -040098 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050099 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800100
Adam Cohen091440a2015-03-18 14:16:05 -0700101 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800102 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final LauncherAppState mApp;
105 @Thunk final Object mLock = new Object();
106 @Thunk DeferredHandler mHandler = new DeferredHandler();
107 @Thunk LoaderTask mLoaderTask;
108 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700109 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
142 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700152 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700164 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 // sBgWorkspaceScreens is the ordered set of workspace screens.
167 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
168
Adam Cohen59400422014-03-05 18:07:04 -0800169 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000170 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800171
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700172 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700173 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
174 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700175
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176 // </ only access in worker thread >
177
Adam Cohen091440a2015-03-18 14:16:05 -0700178 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk final LauncherAppsCompat mLauncherApps;
181 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700184 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public int getCurrentWorkspaceScreen();
186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700191 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800192 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700195 public void bindAppsAdded(ArrayList<Long> newScreens,
196 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700197 ArrayList<ItemInfo> addAnimated,
198 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
201 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700204 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700205 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700206 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100207 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700208 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700210 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chung64359a52013-07-08 17:17:08 -0700213 public interface ItemInfoFilter {
214 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
215 }
216
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800218 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219
Winson Chungee055712013-07-30 14:46:24 -0700220 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700221 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400222 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
223 // resource string.
224 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
225 ProviderInfo providerInfo =
226 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
227 ProviderInfo redirectProvider =
228 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400231 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 if (mOldContentProviderExists) {
234 Log.d(TAG, "Old launcher provider exists.");
235 } else {
236 Log.d(TAG, "Old launcher provider does not exist.");
237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700241 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Kenny Guyed131872014-04-30 03:02:21 +0100244 mLauncherApps = LauncherAppsCompat.getInstance(context);
245 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 }
247
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700248 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
249 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 if (sWorkerThread.getThreadId() == Process.myTid()) {
252 // If we are on the worker thread, post onto the main handler
253 mHandler.post(r);
254 } else {
255 r.run();
256 }
257 }
258
259 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
260 * posted on the worker thread handler. */
261 private static void runOnWorkerThread(Runnable r) {
262 if (sWorkerThread.getThreadId() == Process.myTid()) {
263 r.run();
264 } else {
265 // If we are not on the worker thread, then post to the worker handler
266 sWorker.post(r);
267 }
268 }
269
Sunny Goyal756a28a2015-04-23 17:07:55 -0700270 /**
271 * Runs the specified runnable after the loader is complete
272 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700273 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700274 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
275 synchronized (mBindCompleteRunnables) {
276 mBindCompleteRunnables.add(r);
277 }
278 } else {
279 runOnWorkerThread(r);
280 }
281 }
282
Winson Chunge43a1e72014-01-15 10:33:02 -0800283 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
284 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800285 }
286
Sunny Goyal756adbc2015-04-16 15:20:51 -0700287 public void setPackageState(final PackageInstallInfo installInfo) {
288 Runnable updateRunnable = new Runnable() {
289
290 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500291 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700292 synchronized (sBgLock) {
293 final HashSet<ItemInfo> updates = new HashSet<>();
294
295 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
296 // Ignore install success events as they are handled by Package add events.
297 return;
298 }
299
Sunny Goyale2df0622015-04-24 11:27:00 -0700300 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700301 if (info instanceof ShortcutInfo) {
302 ShortcutInfo si = (ShortcutInfo) info;
303 ComponentName cn = si.getTargetComponent();
304 if (si.isPromise() && (cn != null)
305 && installInfo.packageName.equals(cn.getPackageName())) {
306 si.setInstallProgress(installInfo.progress);
307
308 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
309 // Mark this info as broken.
310 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
311 }
312 updates.add(si);
313 }
314 }
315 }
316
317 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
318 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
319 widget.installProgress = installInfo.progress;
320 updates.add(widget);
321 }
322 }
323
324 if (!updates.isEmpty()) {
325 // Push changes to the callback.
326 Runnable r = new Runnable() {
327 public void run() {
328 Callbacks callbacks = getCallback();
329 if (callbacks != null) {
330 callbacks.bindRestoreItemsChange(updates);
331 }
332 }
333 };
334 mHandler.post(r);
335 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500336 }
337 }
338 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500340 }
341
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 /**
343 * Updates the icons and label of all pending icons for the provided package name.
344 */
345 public void updateSessionDisplayInfo(final String packageName) {
346 Runnable updateRunnable = new Runnable() {
347
348 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700349 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700350 synchronized (sBgLock) {
351 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
352 final UserHandleCompat user = UserHandleCompat.myUserHandle();
353
Sunny Goyale2df0622015-04-24 11:27:00 -0700354 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700355 if (info instanceof ShortcutInfo) {
356 ShortcutInfo si = (ShortcutInfo) info;
357 ComponentName cn = si.getTargetComponent();
358 if (si.isPromise() && (cn != null)
359 && packageName.equals(cn.getPackageName())) {
360 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
361 // For auto install apps update the icon as well as label.
362 mIconCache.getTitleAndIcon(si,
363 si.promisedIntent, user,
364 si.shouldUseLowResIcon());
365 } else {
366 // Only update the icon for restored apps.
367 si.updateIcon(mIconCache);
368 }
369 updates.add(si);
370 }
371 }
372 }
373
374 if (!updates.isEmpty()) {
375 // Push changes to the callback.
376 Runnable r = new Runnable() {
377 public void run() {
378 Callbacks callbacks = getCallback();
379 if (callbacks != null) {
380 callbacks.bindShortcutsChanged(updates,
381 new ArrayList<ShortcutInfo>(), user);
382 }
383 }
384 };
385 mHandler.post(r);
386 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700387 }
388 }
389 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700390 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700391 }
392
Adam Cohen76a47a12014-02-05 11:47:43 -0800393 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800394 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800395
396 if (allAppsApps == null) {
397 throw new RuntimeException("allAppsApps must not be null");
398 }
399 if (allAppsApps.isEmpty()) {
400 return;
401 }
402
403 // Process the newly added applications and add them to the database first
404 Runnable r = new Runnable() {
405 public void run() {
406 runOnMainThread(new Runnable() {
407 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800408 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800409 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500410 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800411 }
412 }
413 });
414 }
415 };
416 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700417 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800418
Sunny Goyala9116722015-04-29 13:55:58 -0700419 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 int[] xy, int spanX, int spanY) {
421 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700422 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
423 final int xCount = (int) profile.numColumns;
424 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800425 boolean[][] occupied = new boolean[xCount][yCount];
426 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700427 for (ItemInfo r : occupiedPos) {
428 int right = r.cellX + r.spanX;
429 int bottom = r.cellY + r.spanY;
430 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
431 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 occupied[x][y] = true;
433 }
434 }
435 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800436 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700437 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 }
439
440 /**
441 * Find a position on the screen for the given size or adds a new screen.
442 * @return screenId and the coordinates for the item.
443 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700444 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 ArrayList<Long> workspaceScreens,
447 ArrayList<Long> addedWorkspaceScreensFinal,
448 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700449 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450
Sunny Goyala9116722015-04-29 13:55:58 -0700451 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700452 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700453 synchronized (sBgLock) {
454 for (ItemInfo info : sBgItemsIdMap) {
455 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
456 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
457 if (items == null) {
458 items = new ArrayList<>();
459 screenItems.put(info.screenId, items);
460 }
461 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
465
466 // Find appropriate space for the item.
467 long screenId = 0;
468 int[] cordinates = new int[2];
469 boolean found = false;
470
471 int screenCount = workspaceScreens.size();
472 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700473 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800474 if (preferredScreenIndex < screenCount) {
475 screenId = workspaceScreens.get(preferredScreenIndex);
476 found = findNextAvailableIconSpaceInScreen(
477 screenItems.get(screenId), cordinates, spanX, spanY);
478 }
479
480 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700481 // Search on any of the screens starting from the first screen.
482 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 screenId = workspaceScreens.get(screen);
484 if (findNextAvailableIconSpaceInScreen(
485 screenItems.get(screenId), cordinates, spanX, spanY)) {
486 // We found a space for it
487 found = true;
488 break;
489 }
490 }
491 }
492
493 if (!found) {
494 // Still no position found. Add a new screen to the end.
495 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
496
497 // Save the screen id for binding in the workspace
498 workspaceScreens.add(screenId);
499 addedWorkspaceScreensFinal.add(screenId);
500
501 // If we still can't find an empty space, then God help us all!!!
502 if (!findNextAvailableIconSpaceInScreen(
503 screenItems.get(screenId), cordinates, spanX, spanY)) {
504 throw new RuntimeException("Can't find space to add the item");
505 }
506 }
507 return Pair.create(screenId, cordinates);
508 }
509
510 /**
511 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800512 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700513 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700514 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800516 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700517 return;
Winson Chung997a9232013-07-24 15:33:46 -0700518 }
Winson Chung64359a52013-07-08 17:17:08 -0700519 // Process the newly added applications and add them to the database first
520 Runnable r = new Runnable() {
521 public void run() {
522 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
523 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
524
Winson Chung76828c82013-08-19 15:43:29 -0700525 // Get the list of workspace screens. We need to append to this list and
526 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
527 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800528 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700529 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700531 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700533 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700535 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 }
Winson Chung76828c82013-08-19 15:43:29 -0700537
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700539 Pair<Long, int[]> coords = findSpaceForItem(context,
540 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 1, 1);
542 long screenId = coords.first;
543 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700544
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 ItemInfo itemInfo;
546 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
547 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800548 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700549 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700550 } else {
551 throw new RuntimeException("Unexpected info type");
552 }
Winson Chung94d67682013-09-25 16:29:40 -0700553
Winson Chung64359a52013-07-08 17:17:08 -0700554 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700555 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700556 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700557 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700558 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700559 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700560 }
561 }
562
Winson Chung76828c82013-08-19 15:43:29 -0700563 // Update the workspace screens
564 updateWorkspaceScreenOrder(context, workspaceScreens);
565
Adam Cohen76a47a12014-02-05 11:47:43 -0800566 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700567 runOnMainThread(new Runnable() {
568 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800569 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700570 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700571 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
572 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700573 if (!addedShortcutsFinal.isEmpty()) {
574 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
575 long lastScreenId = info.screenId;
576 for (ItemInfo i : addedShortcutsFinal) {
577 if (i.screenId == lastScreenId) {
578 addAnimated.add(i);
579 } else {
580 addNotAnimated.add(i);
581 }
Winson Chung997a9232013-07-24 15:33:46 -0700582 }
583 }
Winson Chungd64d1762013-08-20 14:37:16 -0700584 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800585 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700586 }
Winson Chung64359a52013-07-08 17:17:08 -0700587 }
Winson Chung997a9232013-07-24 15:33:46 -0700588 });
589 }
Winson Chung64359a52013-07-08 17:17:08 -0700590 }
591 };
592 runOnWorkerThread(r);
593 }
594
Sunny Goyald33860f2015-04-23 16:02:20 -0700595 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700596 if (sWorkerThread.getThreadId() == Process.myTid()) {
597 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
598 "main thread");
599 }
600
601 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400602 synchronized (mDeferredBindRunnables) {
603 mDeferredBindRunnables.clear();
604 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700605
606 // Remove any queued UI runnables
607 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700608 // Unbind all the workspace items
609 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700610 }
611
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700613 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700614 // Ensure that we don't use the same workspace items data structure on the main thread
615 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700616 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700617 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700618 tmpItems.addAll(sBgWorkspaceItems);
619 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700620 }
621 Runnable r = new Runnable() {
622 @Override
623 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700624 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 item.unbind();
626 }
627 }
628 };
629 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700630 }
631
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 /**
633 * Adds an item to the DB if it was not created previously, or move it to a new
634 * <container, screen, cellX, cellY>
635 */
636 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700637 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 if (item.container == ItemInfo.NO_ID) {
639 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700640 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 } else {
642 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
645 }
646
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700647 static void checkItemInfoLocked(
648 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
649 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
650 if (modelItem != null && item != modelItem) {
651 // check all the data is consistent
652 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
653 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
654 ShortcutInfo shortcut = (ShortcutInfo) item;
655 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
656 modelShortcut.intent.filterEquals(shortcut.intent) &&
657 modelShortcut.id == shortcut.id &&
658 modelShortcut.itemType == shortcut.itemType &&
659 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700661 modelShortcut.cellX == shortcut.cellX &&
662 modelShortcut.cellY == shortcut.cellY &&
663 modelShortcut.spanX == shortcut.spanX &&
664 modelShortcut.spanY == shortcut.spanY &&
665 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
666 (modelShortcut.dropPos != null &&
667 shortcut.dropPos != null &&
668 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
669 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
670 // For all intents and purposes, this is the same object
671 return;
672 }
673 }
674
675 // the modelItem needs to match up perfectly with item if our model is
676 // to be consistent with the database-- for now, just require
677 // modelItem == item or the equality check above
678 String msg = "item: " + ((item != null) ? item.toString() : "null") +
679 "modelItem: " +
680 ((modelItem != null) ? modelItem.toString() : "null") +
681 "Error: ItemInfo passed to checkItemInfo doesn't match original";
682 RuntimeException e = new RuntimeException(msg);
683 if (stackTrace != null) {
684 e.setStackTrace(stackTrace);
685 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800686 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700687 }
688 }
689
Michael Jurka816474f2012-06-25 14:49:02 -0700690 static void checkItemInfo(final ItemInfo item) {
691 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
692 final long itemId = item.id;
693 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700694 public void run() {
695 synchronized (sBgLock) {
696 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700697 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700698 }
699 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700700 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700701 }
702
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
704 final ItemInfo item, final String callingFunction) {
705 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700706 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700707 final ContentResolver cr = context.getContentResolver();
708
Adam Cohen487f7dd2012-06-28 18:12:10 -0700709 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700710 Runnable r = new Runnable() {
711 public void run() {
712 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 updateItemArrays(item, itemId, stackTrace);
714 }
715 };
716 runOnWorkerThread(r);
717 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700718
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
720 final ArrayList<ItemInfo> items, final String callingFunction) {
721 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700722
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
724 Runnable r = new Runnable() {
725 public void run() {
726 ArrayList<ContentProviderOperation> ops =
727 new ArrayList<ContentProviderOperation>();
728 int count = items.size();
729 for (int i = 0; i < count; i++) {
730 ItemInfo item = items.get(i);
731 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700732 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700733 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700734
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700735 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
736 updateItemArrays(item, itemId, stackTrace);
737
738 }
739 try {
740 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
741 } catch (Exception e) {
742 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 }
744 }
745 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700746 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700747 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700748
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
750 // Lock on mBgLock *after* the db operation
751 synchronized (sBgLock) {
752 checkItemInfoLocked(itemId, item, stackTrace);
753
754 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
755 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
756 // Item is in a folder, make sure this folder exists
757 if (!sBgFolders.containsKey(item.container)) {
758 // An items container is being set to a that of an item which is not in
759 // the list of Folders.
760 String msg = "item: " + item + " container being set to: " +
761 item.container + ", not in the list of folders";
762 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763 }
764 }
765
766 // Items are added/removed from the corresponding FolderInfo elsewhere, such
767 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
768 // that are on the desktop, as appropriate
769 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800770 if (modelItem != null &&
771 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
772 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700773 switch (modelItem.itemType) {
774 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
775 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
776 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
777 if (!sBgWorkspaceItems.contains(modelItem)) {
778 sBgWorkspaceItems.add(modelItem);
779 }
780 break;
781 default:
782 break;
783 }
784 } else {
785 sBgWorkspaceItems.remove(modelItem);
786 }
787 }
788 }
789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700792 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700793 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 item.cellX = cellX;
797 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798
Winson Chung3d503fb2011-07-13 17:25:49 -0700799 // We store hotseat items in canonical form which is this orientation invariant position
800 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700802 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700803 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700805 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807
808 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
811 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800812 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700813 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700816 }
817
818 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700819 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
820 * cellX, cellY have already been updated on the ItemInfos.
821 */
822 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
823 final long container, final int screen) {
824
825 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
826 int count = items.size();
827
828 for (int i = 0; i < count; i++) {
829 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700830 item.container = container;
831
832 // We store hotseat items in canonical form which is this orientation invariant position
833 // in the hotseat
834 if (context instanceof Launcher && screen < 0 &&
835 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700836 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700837 item.cellY);
838 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700840 }
841
842 final ContentValues values = new ContentValues();
843 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
844 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
845 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800846 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700848
849 contentValues.add(values);
850 }
851 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
852 }
853
854 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800856 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700859 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800860 item.cellX = cellX;
861 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 item.spanX = spanX;
863 item.spanY = spanY;
864
865 // We store hotseat items in canonical form which is this orientation invariant position
866 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700867 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700868 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700871 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 }
Adam Cohend4844c32011-02-18 19:25:06 -0800873
Adam Cohend4844c32011-02-18 19:25:06 -0800874 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800875 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700876 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
877 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800878 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700879 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
880 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700881 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800882
Michael Jurka816474f2012-06-25 14:49:02 -0700883 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700884 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700885
886 /**
887 * Update an item to the database in a specified container.
888 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700889 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700890 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100891 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700892 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800893 }
894
Sunny Goyal756a28a2015-04-23 17:07:55 -0700895 private void assertWorkspaceLoaded() {
896 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
897 throw new RuntimeException("Trying to add shortcut while loader is running");
898 }
899 }
900
Adam Cohend4844c32011-02-18 19:25:06 -0800901 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700902 * Returns true if the shortcuts already exists on the workspace. This must be called after
903 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700905 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
906 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700907 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 if (intent.getComponent() != null) {
909 // If component is not null, an intent with null package will produce
910 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700911 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700913 intentWithPkg = intent.toUri(0);
914 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700915 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
917 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 }
919 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 intentWithPkg = intent.toUri(0);
921 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700922 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700923
924 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700925 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700926 if (item instanceof ShortcutInfo) {
927 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700928 Intent targetIntent = info.promisedIntent == null
929 ? info.intent : info.promisedIntent;
930 if (targetIntent != null && info.user.equals(user)) {
931 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700932 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
933 return true;
934 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700935 }
936 }
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700939 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700940 }
941
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 /**
943 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
944 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700945 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 final ContentResolver cr = context.getContentResolver();
947 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
948 "_id=? and (itemType=? or itemType=?)",
949 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700950 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700951
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 try {
953 if (c.moveToFirst()) {
954 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
955 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
956 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
957 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
958 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
959 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700960 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 FolderInfo folderInfo = null;
963 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700964 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
965 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700967 }
968
Sunny Goyala508e4f2015-05-21 09:33:57 -0700969 // Do not trim the folder label, as is was set by the user.
970 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 folderInfo.id = id;
972 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700974 folderInfo.cellX = c.getInt(cellXIndex);
975 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700976 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977
978 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 } finally {
981 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700982 }
983
984 return null;
985 }
986
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 /**
988 * Add an item to the database in a specified container. Sets the container, screen, cellX and
989 * cellY fields of the item. Also assigns an ID to the item.
990 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700991 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700992 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 item.cellX = cellX;
995 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 // We store hotseat items in canonical form which is this orientation invariant position
997 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004
1005 final ContentValues values = new ContentValues();
1006 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001007 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Michael Jurka414300a2013-08-27 15:42:35 +02001009 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001011
Jason Monk8e19cf22014-03-20 15:06:57 -04001012 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001015 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001018 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001019 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 sBgItemsIdMap.put(item.id, item);
1021 switch (item.itemType) {
1022 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1023 sBgFolders.put(item.id, (FolderInfo) item);
1024 // Fall through
1025 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1026 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1027 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1028 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1029 sBgWorkspaceItems.add(item);
1030 } else {
1031 if (!sBgFolders.containsKey(item.container)) {
1032 // Adding an item to a folder that doesn't exist.
1033 String msg = "adding item: " + item + " to a folder that " +
1034 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001035 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001036 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001037 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 break;
1039 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1040 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1041 break;
1042 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 }
1044 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001045 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001047 }
1048
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001050 * Creates a new unique child id, for a given cell span across all layouts.
1051 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001052 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001053 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001054 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001056 }
1057
Sunny Goyal34942622014-08-29 17:20:55 -07001058 private static ArrayList<ItemInfo> getItemsByPackageName(
1059 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001060 ItemInfoFilter filter = new ItemInfoFilter() {
1061 @Override
1062 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1063 return cn.getPackageName().equals(pn) && info.user.equals(user);
1064 }
1065 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001066 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001067 }
1068
1069 /**
1070 * Removes all the items from the database corresponding to the specified package.
1071 */
1072 static void deletePackageFromDatabase(Context context, final String pn,
1073 final UserHandleCompat user) {
1074 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001075 }
1076
1077 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001078 * Removes the specified item from the database
1079 * @param context
1080 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001082 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001083 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1084 items.add(item);
1085 deleteItemsFromDatabase(context, items);
1086 }
1087
1088 /**
1089 * Removes the specified items from the database
1090 * @param context
1091 * @param item
1092 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001093 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001095 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001096 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001097 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001098 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001099 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001100
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 // Lock on mBgLock *after* the db operation
1102 synchronized (sBgLock) {
1103 switch (item.itemType) {
1104 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1105 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001106 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001107 if (info.container == item.id) {
1108 // We are deleting a folder which still contains items that
1109 // think they are contained by that folder.
1110 String msg = "deleting a folder (" + item + ") which still " +
1111 "contains items (" + info + ")";
1112 Log.e(TAG, msg);
1113 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001115 sBgWorkspaceItems.remove(item);
1116 break;
1117 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1118 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1119 sBgWorkspaceItems.remove(item);
1120 break;
1121 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1122 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1123 break;
1124 }
1125 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001127 }
1128 }
Michael Jurka83df1882011-08-31 20:59:26 -07001129 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 }
1132
1133 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001134 * Update the order of the workspace screens in the database. The array list contains
1135 * a list of screen ids in the order that they should appear.
1136 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001137 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001138 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 final ContentResolver cr = context.getContentResolver();
1140 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1141
1142 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001143 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001144 while (iter.hasNext()) {
1145 long id = iter.next();
1146 if (id < 0) {
1147 iter.remove();
1148 }
1149 }
1150
1151 Runnable r = new Runnable() {
1152 @Override
1153 public void run() {
Yura085c8532014-02-11 15:15:29 +00001154 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001155 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001156 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001157 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001158 for (int i = 0; i < count; i++) {
1159 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001160 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1162 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001163 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 }
Yura085c8532014-02-11 15:15:29 +00001165
1166 try {
1167 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1168 } catch (Exception ex) {
1169 throw new RuntimeException(ex);
1170 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001171
Winson Chungba9c37f2013-08-30 14:11:37 -07001172 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001173 sBgWorkspaceScreens.clear();
1174 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001175 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001176 }
1177 };
1178 runOnWorkerThread(r);
1179 }
1180
1181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 * Remove the contents of the specified folder from the database
1183 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001184 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 final ContentResolver cr = context.getContentResolver();
1186
Michael Jurkac9d95c52011-08-29 14:03:34 -07001187 Runnable r = new Runnable() {
1188 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001189 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001191 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 sBgItemsIdMap.remove(info.id);
1193 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 sBgWorkspaceItems.remove(info);
1195 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001196
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001197 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001198 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001200 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 for (ItemInfo childInfo : info.contents) {
1202 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001204 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001205 }
1206 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 }
1209
1210 /**
1211 * Set this as the current Launcher activity object for the loader.
1212 */
1213 public void initialize(Callbacks callbacks) {
1214 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001215 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1216 // workspace to prevent leaking Launcher activities on orientation change.
1217 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 mCallbacks = new WeakReference<Callbacks>(callbacks);
1219 }
1220 }
1221
Kenny Guyed131872014-04-30 03:02:21 +01001222 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001223 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001224 int op = PackageUpdatedTask.OP_UPDATE;
1225 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1226 user));
1227 }
1228
1229 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001230 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001231 int op = PackageUpdatedTask.OP_REMOVE;
1232 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1233 user));
1234 }
1235
1236 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001237 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001238 int op = PackageUpdatedTask.OP_ADD;
1239 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1240 user));
1241 }
1242
1243 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001244 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001245 boolean replacing) {
1246 if (!replacing) {
1247 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1248 user));
1249 if (mAppsCanBeOnRemoveableStorage) {
1250 // Only rebind if we support removable storage. It catches the
1251 // case where
1252 // apps on the external sd card need to be reloaded
1253 startLoaderFromBackground();
1254 }
1255 } else {
1256 // If we are replacing then just update the packages in the list
1257 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1258 packageNames, user));
1259 }
1260 }
1261
1262 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001263 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001264 boolean replacing) {
1265 if (!replacing) {
1266 enqueuePackageUpdated(new PackageUpdatedTask(
1267 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1268 user));
1269 }
Kenny Guyed131872014-04-30 03:02:21 +01001270 }
1271
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001272 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1274 * ACTION_PACKAGE_CHANGED.
1275 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001276 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001277 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001278 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001279
Joe Onorato36115782010-06-17 13:28:48 -04001280 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001281 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001282 // If we have changed locale we need to clear out the labels in all apps/workspace.
1283 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001284 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1285 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001286 Callbacks callbacks = getCallback();
1287 if (callbacks != null) {
1288 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001289 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001290 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1291 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1292 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001293 }
1294 }
1295
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001296 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001297 resetLoadedState(true, true);
1298
Reena Lee93f824a2011-09-23 17:20:28 -07001299 // Do this here because if the launcher activity is running it will be restarted.
1300 // If it's not running startLoaderFromBackground will merely tell it that it needs
1301 // to reload.
1302 startLoaderFromBackground();
1303 }
1304
Winson Chungf0c6ae02012-03-21 16:10:31 -07001305 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1306 synchronized (mLock) {
1307 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1308 // mWorkspaceLoaded to true later
1309 stopLoaderLocked();
1310 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1311 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1312 }
1313 }
1314
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001315 /**
1316 * When the launcher is in the background, it's possible for it to miss paired
1317 * configuration changes. So whenever we trigger the loader from the background
1318 * tell the launcher that it needs to re-run the loader when it comes back instead
1319 * of doing it now.
1320 */
1321 public void startLoaderFromBackground() {
1322 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001323 Callbacks callbacks = getCallback();
1324 if (callbacks != null) {
1325 // Only actually run the loader if they're not paused.
1326 if (!callbacks.setLoadOnResume()) {
1327 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001328 }
1329 }
1330 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001331 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001332 }
Joe Onorato36115782010-06-17 13:28:48 -04001333 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001334
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001335 /**
1336 * If there is already a loader task running, tell it to stop.
1337 */
1338 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001339 LoaderTask oldTask = mLoaderTask;
1340 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001341 oldTask.stopLocked();
1342 }
Reena Lee93f824a2011-09-23 17:20:28 -07001343 }
1344
Adam Cohen1a85c582014-09-30 09:48:49 -07001345 public boolean isCurrentCallbacks(Callbacks callbacks) {
1346 return (mCallbacks != null && mCallbacks.get() == callbacks);
1347 }
1348
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 public void startLoader(int synchronousBindPage) {
1350 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001351 }
1352
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001353 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001354 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1355 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001356 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001357 // Clear any deferred bind-runnables from the synchronized load process
1358 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001359 synchronized (mDeferredBindRunnables) {
1360 mDeferredBindRunnables.clear();
1361 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362
Joe Onorato36115782010-06-17 13:28:48 -04001363 // Don't bother to start the thread if we know it's not going to do anything
1364 if (mCallbacks != null && mCallbacks.get() != null) {
1365 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001366 stopLoaderLocked();
1367 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001368 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001369 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1371 } else {
1372 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1373 sWorker.post(mLoaderTask);
1374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
1377 }
1378
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 void bindRemainingSynchronousPages() {
1380 // Post the remaining side pages to be loaded
1381 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001382 Runnable[] deferredBindRunnables = null;
1383 synchronized (mDeferredBindRunnables) {
1384 deferredBindRunnables = mDeferredBindRunnables.toArray(
1385 new Runnable[mDeferredBindRunnables.size()]);
1386 mDeferredBindRunnables.clear();
1387 }
1388 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001389 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001390 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001392
1393 // Run all the bind complete runnables after workspace is bound.
1394 if (!mBindCompleteRunnables.isEmpty()) {
1395 synchronized (mBindCompleteRunnables) {
1396 for (final Runnable r : mBindCompleteRunnables) {
1397 runOnWorkerThread(r);
1398 }
1399 mBindCompleteRunnables.clear();
1400 }
1401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001402 }
1403
Joe Onorato36115782010-06-17 13:28:48 -04001404 public void stopLoader() {
1405 synchronized (mLock) {
1406 if (mLoaderTask != null) {
1407 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001408 }
1409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001412 /**
1413 * Loads the workspace screen ids in an ordered list.
1414 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001415 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001416 final ContentResolver contentResolver = context.getContentResolver();
1417 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001418
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001419 // Get screens ordered by rank.
1420 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1421 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1422 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001423 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001424 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001425 while (sc.moveToNext()) {
1426 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001427 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001428 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001429 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1430 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001431 }
1432 }
1433 } finally {
1434 sc.close();
1435 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001436 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001437 }
1438
Michael Jurkac57b7a82011-08-09 22:02:20 -07001439 public boolean isAllAppsLoaded() {
1440 return mAllAppsLoaded;
1441 }
1442
Winson Chung36a62fe2012-05-06 18:04:42 -07001443 boolean isLoadingWorkspace() {
1444 synchronized (mLock) {
1445 if (mLoaderTask != null) {
1446 return mLoaderTask.isLoadingWorkspace();
1447 }
1448 }
1449 return false;
1450 }
1451
Joe Onorato36115782010-06-17 13:28:48 -04001452 /**
1453 * Runnable for the thread that loads the contents of the launcher:
1454 * - workspace icons
1455 * - widgets
1456 * - all apps icons
1457 */
1458 private class LoaderTask implements Runnable {
1459 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001460 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001461 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001462 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001463 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001465 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001466 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001467 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 }
1469
Winson Chung36a62fe2012-05-06 18:04:42 -07001470 boolean isLoadingWorkspace() {
1471 return mIsLoadingAndBindingWorkspace;
1472 }
1473
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001474 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001475 mIsLoadingAndBindingWorkspace = true;
1476
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001478 if (DEBUG_LOADERS) {
1479 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001481
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001482 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001483 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001484 synchronized (LoaderTask.this) {
1485 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001486 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001487 }
1488 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
1490 }
1491
Joe Onorato36115782010-06-17 13:28:48 -04001492 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001493 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001494 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001495
Joe Onorato36115782010-06-17 13:28:48 -04001496 private void waitForIdle() {
1497 // Wait until the either we're stopped or the other threads are done.
1498 // This way we don't start loading all apps until the workspace has settled
1499 // down.
1500 synchronized (LoaderTask.this) {
1501 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001502
Joe Onorato36115782010-06-17 13:28:48 -04001503 mHandler.postIdle(new Runnable() {
1504 public void run() {
1505 synchronized (LoaderTask.this) {
1506 mLoadAndBindStepFinished = true;
1507 if (DEBUG_LOADERS) {
1508 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001511 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513 });
1514
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001515 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001516 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001517 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1518 // wait no longer than 1sec at a time
1519 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001520 } catch (InterruptedException ex) {
1521 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001522 }
1523 }
Joe Onorato36115782010-06-17 13:28:48 -04001524 if (DEBUG_LOADERS) {
1525 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001526 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001527 + "ms for previous step to finish binding");
1528 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001529 }
Joe Onorato36115782010-06-17 13:28:48 -04001530 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001531
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001533 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 // Ensure that we have a valid page index to load synchronously
1535 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1536 "valid page index");
1537 }
1538 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1539 // Ensure that we don't try and bind a specified page when the pages have not been
1540 // loaded already (we should load everything asynchronously in that case)
1541 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1542 }
1543 synchronized (mLock) {
1544 if (mIsLoaderTaskRunning) {
1545 // Ensure that we are never running the background loading at this point since
1546 // we also touch the background collections
1547 throw new RuntimeException("Error! Background loading is already running");
1548 }
1549 }
1550
1551 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1552 // data structures, we can't allow any other thread to touch that data, but because
1553 // this call is synchronous, we can get away with not locking).
1554
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001555 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001556 // operations from the previous activity. We need to ensure that all queued operations
1557 // are executed before any synchronous binding work is done.
1558 mHandler.flush();
1559
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001560 // Divide the set of loaded items into those that we are binding synchronously, and
1561 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001562 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001563 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1564 // arise from that.
1565 onlyBindAllApps();
1566 }
1567
Joe Onorato36115782010-06-17 13:28:48 -04001568 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001569 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001570 if (mStopped) {
1571 return;
1572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001573 mIsLoaderTaskRunning = true;
1574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575 // Optimize for end-user experience: if the Launcher is up and // running with the
1576 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1577 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001578 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001579 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001580 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001581
Joe Onorato36115782010-06-17 13:28:48 -04001582 if (mStopped) {
1583 break keep_running;
1584 }
1585
Joe Onorato36115782010-06-17 13:28:48 -04001586 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001587
1588 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001589 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1590 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001591 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592
Joe Onorato36115782010-06-17 13:28:48 -04001593 // Clear out this reference, otherwise we end up holding it until all of the
1594 // callback runnables are done.
1595 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596
Joe Onorato36115782010-06-17 13:28:48 -04001597 synchronized (mLock) {
1598 // If we are still the last one to be scheduled, remove ourselves.
1599 if (mLoaderTask == this) {
1600 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001603 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
1606
1607 public void stopLocked() {
1608 synchronized (LoaderTask.this) {
1609 mStopped = true;
1610 this.notify();
1611 }
1612 }
1613
1614 /**
1615 * Gets the callbacks object. If we've been stopped, or if the launcher object
1616 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1617 * object that was around when the deferred message was scheduled, and if there's
1618 * a new Callbacks object around then also return null. This will save us from
1619 * calling onto it with data that will be ignored.
1620 */
1621 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1622 synchronized (mLock) {
1623 if (mStopped) {
1624 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626
1627 if (mCallbacks == null) {
1628 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001629 }
Joe Onorato36115782010-06-17 13:28:48 -04001630
1631 final Callbacks callbacks = mCallbacks.get();
1632 if (callbacks != oldCallbacks) {
1633 return null;
1634 }
1635 if (callbacks == null) {
1636 Log.w(TAG, "no mCallbacks");
1637 return null;
1638 }
1639
1640 return callbacks;
1641 }
1642 }
1643
1644 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001645 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001646 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001647 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1648 final int countX = (int) profile.numColumns;
1649 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001650
Adam Cohendcd297f2013-06-18 13:13:40 -07001651 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001652 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001653 // Return early if we detect that an item is under the hotseat button
1654 if (mCallbacks == null ||
1655 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001656 Log.e(TAG, "Error loading shortcut into hotseat " + item
1657 + " into position (" + item.screenId + ":" + item.cellX + ","
1658 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001659 return false;
1660 }
1661
Dan Sandler295ae182013-12-10 16:05:47 -05001662 final ItemInfo[][] hotseatItems =
1663 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1664
Adam Cohen2e6da152015-05-06 11:42:25 -07001665 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001666 Log.e(TAG, "Error loading shortcut " + item
1667 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001668 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001669 + ")");
1670 return false;
1671 }
1672
Dan Sandler295ae182013-12-10 16:05:47 -05001673 if (hotseatItems != null) {
1674 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001675 Log.e(TAG, "Error loading shortcut into hotseat " + item
1676 + " into position (" + item.screenId + ":" + item.cellX + ","
1677 + item.cellY + ") occupied by "
1678 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1679 [(int) item.screenId][0]);
1680 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001681 } else {
1682 hotseatItems[(int) item.screenId][0] = item;
1683 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001684 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001685 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001686 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001687 items[(int) item.screenId][0] = item;
1688 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001689 return true;
1690 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001691 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1692 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001693 return true;
1694 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001695
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001697 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001698 occupied.put(item.screenId, items);
1699 }
1700
Dan Sandler295ae182013-12-10 16:05:47 -05001701 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001702 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1703 item.cellX < 0 || item.cellY < 0 ||
1704 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1705 Log.e(TAG, "Error loading shortcut " + item
1706 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1707 + item.cellX + "," + item.cellY
1708 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1709 return false;
1710 }
1711
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001712 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001713 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1714 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001715 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001716 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001718 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001719 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001721 return false;
1722 }
1723 }
1724 }
1725 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1726 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001727 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001728 }
1729 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001730
Joe Onorato36115782010-06-17 13:28:48 -04001731 return true;
1732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733
Winson Chungba9c37f2013-08-30 14:11:37 -07001734 /** Clears all the sBg data structures */
1735 private void clearSBgDataStructures() {
1736 synchronized (sBgLock) {
1737 sBgWorkspaceItems.clear();
1738 sBgAppWidgets.clear();
1739 sBgFolders.clear();
1740 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001741 sBgWorkspaceScreens.clear();
1742 }
1743 }
1744
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001745 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001746 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747
Joe Onorato36115782010-06-17 13:28:48 -04001748 final Context context = mContext;
1749 final ContentResolver contentResolver = context.getContentResolver();
1750 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001751 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001752 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1753 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001754 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001755
Winson Chung892c74d2013-08-22 16:15:50 -07001756 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001757 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1758 int countX = (int) profile.numColumns;
1759 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001760
Sunny Goyale5bb7052015-07-27 14:36:07 -07001761
1762 if (MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001763 long migrationStartTime = System.currentTimeMillis();
1764 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001765 try {
1766 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1767 // Clear the flags before starting the task, so that we do not run the task
1768 // again, in case there was an uncaught error.
1769 MigrateFromRestoreTask.clearFlags(mContext);
1770 task.execute();
1771 } catch (Exception e) {
1772 Log.e(TAG, "Error during grid migration", e);
1773
1774 // Clear workspace.
1775 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1776 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001777 Log.v(TAG, "Workspace migration completed in "
1778 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001779 }
1780
Dan Sandlerd5024042014-01-09 15:01:33 -05001781 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1782 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1783 LauncherAppState.getLauncherProvider().deleteDatabase();
1784 }
1785
1786 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1787 // append the user's Launcher2 shortcuts
1788 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1789 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1790 } else {
1791 // Make sure the default workspace is loaded
1792 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001793 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001794 }
Adam Cohene25af792013-06-06 23:08:25 -07001795
Winson Chung2abf94d2012-07-18 18:16:38 -07001796 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001797 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001798 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001799 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001800
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001802 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001803 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001804 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001805 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001806
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 // +1 for the hotseat (it can be larger than the workspace)
1808 // Load workspace in reverse order to ensure that latest items are loaded first (and
1809 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001810 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 try {
1813 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1814 final int intentIndex = c.getColumnIndexOrThrow
1815 (LauncherSettings.Favorites.INTENT);
1816 final int titleIndex = c.getColumnIndexOrThrow
1817 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 final int containerIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.CONTAINER);
1820 final int itemTypeIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.ITEM_TYPE);
1822 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001824 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1825 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 final int screenIndex = c.getColumnIndexOrThrow(
1827 LauncherSettings.Favorites.SCREEN);
1828 final int cellXIndex = c.getColumnIndexOrThrow
1829 (LauncherSettings.Favorites.CELLX);
1830 final int cellYIndex = c.getColumnIndexOrThrow
1831 (LauncherSettings.Favorites.CELLY);
1832 final int spanXIndex = c.getColumnIndexOrThrow
1833 (LauncherSettings.Favorites.SPANX);
1834 final int spanYIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001836 final int rankIndex = c.getColumnIndexOrThrow(
1837 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001838 final int restoredIndex = c.getColumnIndexOrThrow(
1839 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001840 final int profileIdIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001842 final int optionsIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001844 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845
Sunny Goyal7f834d22015-04-21 10:10:23 -07001846 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1847 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1848 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1849 }
1850
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 ShortcutInfo info;
1852 String intentDescription;
1853 LauncherAppWidgetInfo appWidgetInfo;
1854 int container;
1855 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001856 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001858 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 while (!mStopped && c.moveToNext()) {
1861 try {
1862 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001863 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001864 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001865 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 switch (itemType) {
1868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1869 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001870 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001871 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001872 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001873 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001874 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001875 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001876 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001877 if (user == null) {
1878 // User has been deleted remove the item.
1879 itemsToRemove.add(id);
1880 continue;
1881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 try {
1883 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001884 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001885 if (cn != null && cn.getPackageName() != null) {
1886 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1887 cn.getPackageName(), user);
1888 boolean validComponent = validPkg &&
1889 launcherApps.isActivityEnabledForProfile(cn, user);
1890
1891 if (validComponent) {
1892 if (restored) {
1893 // no special handling necessary for this item
1894 restoredRows.add(id);
1895 restored = false;
1896 }
1897 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001898 intent = null;
1899 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1900 // We allow auto install apps to have their intent
1901 // updated after an install.
1902 intent = manager.getLaunchIntentForPackage(
1903 cn.getPackageName());
1904 if (intent != null) {
1905 ContentValues values = new ContentValues();
1906 values.put(LauncherSettings.Favorites.INTENT,
1907 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001908 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001909 }
1910 }
1911
1912 if (intent == null) {
1913 // The app is installed but the component is no
1914 // longer available.
1915 Launcher.addDumpLog(TAG,
1916 "Invalid component removed: " + cn, true);
1917 itemsToRemove.add(id);
1918 continue;
1919 } else {
1920 // no special handling necessary for this item
1921 restoredRows.add(id);
1922 restored = false;
1923 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001924 } else if (restored) {
1925 // Package is not yet available but might be
1926 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001927 Launcher.addDumpLog(TAG,
1928 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001929
1930 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1931 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001932 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001933 // App restore has started. Update the flag
1934 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1935 ContentValues values = new ContentValues();
1936 values.put(LauncherSettings.Favorites.RESTORED,
1937 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001938 updateItem(id, values);
1939 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1940 // This is a common app. Try to replace this.
1941 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1942 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1943 if (parser.findDefaultApp()) {
1944 // Default app found. Replace it.
1945 intent = parser.parsedIntent;
1946 cn = intent.getComponent();
1947 ContentValues values = parser.parsedValues;
1948 values.put(LauncherSettings.Favorites.RESTORED, 0);
1949 updateItem(id, values);
1950 restored = false;
1951 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001952
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001953 } else if (REMOVE_UNRESTORED_ICONS) {
1954 Launcher.addDumpLog(TAG,
1955 "Unrestored package removed: " + cn, true);
1956 itemsToRemove.add(id);
1957 continue;
1958 }
Sunny Goyal94485362014-09-18 16:13:58 -07001959 } else if (REMOVE_UNRESTORED_ICONS) {
1960 Launcher.addDumpLog(TAG,
1961 "Unrestored package removed: " + cn, true);
1962 itemsToRemove.add(id);
1963 continue;
1964 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001965 } else if (launcherApps.isAppEnabled(
1966 manager, cn.getPackageName(),
1967 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1968 // Package is present but not available.
1969 allowMissingTarget = true;
1970 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1971 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001972 // SdCard is not ready yet. Package might get available,
1973 // once it is ready.
1974 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1975 + " (check again later)", true);
1976 HashSet<String> pkgs = sPendingPackages.get(user);
1977 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001978 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001979 sPendingPackages.put(user, pkgs);
1980 }
1981 pkgs.add(cn.getPackageName());
1982 allowMissingTarget = true;
1983 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001984
1985 } else {
1986 // Do not wait for external media load anymore.
1987 // Log the invalid package, and remove it
1988 Launcher.addDumpLog(TAG,
1989 "Invalid package removed: " + cn, true);
1990 itemsToRemove.add(id);
1991 continue;
Winson Chungee055712013-07-30 14:46:24 -07001992 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001993 } else if (cn == null) {
1994 // For shortcuts with no component, keep them as they are
1995 restoredRows.add(id);
1996 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001997 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001999 Launcher.addDumpLog(TAG,
2000 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 continue;
2002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003
Sunny Goyal34b65272015-03-11 16:56:52 -07002004 boolean useLowResIcon = container >= 0 &&
2005 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2006
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002007 if (itemReplaced) {
2008 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002009 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 cursorIconInfo.iconIndex, titleIndex,
2011 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002012 } else {
2013 // Don't replace items for other profiles.
2014 itemsToRemove.add(id);
2015 continue;
2016 }
2017 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002018 if (user.equals(UserHandleCompat.myUserHandle())) {
2019 Launcher.addDumpLog(TAG,
2020 "constructing info for partially restored package",
2021 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002022 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002023 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002024 intent = getRestoredItemIntent(c, context, intent);
2025 } else {
2026 // Don't restore items for other profiles.
2027 itemsToRemove.add(id);
2028 continue;
2029 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002030 } else if (itemType ==
2031 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002032 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002033 cursorIconInfo.iconIndex, titleIndex,
2034 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002036 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 // App shortcuts that used to be automatically added to Launcher
2039 // didn't always have the correct intent flags set, so do that
2040 // here
2041 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002042 intent.getCategories() != null &&
2043 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002044 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 intent.addFlags(
2046 Intent.FLAG_ACTIVITY_NEW_TASK |
2047 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2048 }
Michael Jurka96879562012-03-22 05:54:33 -07002049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002052 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002055 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 info.cellX = c.getInt(cellXIndex);
2057 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002058 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002059 info.spanX = 1;
2060 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002061 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002062 if (info.promisedIntent != null) {
2063 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2064 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002065 info.isDisabled = disabledState;
2066 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2067 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2068 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002069
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002071 if (!checkItemPlacement(occupied, info)) {
2072 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 break;
2074 }
2075
Sunny Goyal756adbc2015-04-16 15:20:51 -07002076 if (restored) {
2077 ComponentName cn = info.getTargetComponent();
2078 if (cn != null) {
2079 Integer progress = installingPkgs.get(cn.getPackageName());
2080 if (progress != null) {
2081 info.setInstallProgress(progress);
2082 } else {
2083 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2084 }
2085 }
2086 }
2087
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 switch (container) {
2089 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2090 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2091 sBgWorkspaceItems.add(info);
2092 break;
2093 default:
2094 // Item is in a user folder
2095 FolderInfo folderInfo =
2096 findOrMakeFolder(sBgFolders, container);
2097 folderInfo.add(info);
2098 break;
2099 }
2100 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002101 } else {
2102 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 }
2104 break;
2105
2106 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2107 id = c.getLong(idIndex);
2108 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2109
Sunny Goyala508e4f2015-05-21 09:33:57 -07002110 // Do not trim the folder label, as is was set by the user.
2111 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 folderInfo.cellX = c.getInt(cellXIndex);
2116 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002117 folderInfo.spanX = 1;
2118 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002119 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120
Daniel Sandler8802e962010-05-26 16:28:16 -04002121 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002122 if (!checkItemPlacement(occupied, folderInfo)) {
2123 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002124 break;
2125 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002126
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2129 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2130 sBgWorkspaceItems.add(folderInfo);
2131 break;
Joe Onorato36115782010-06-17 13:28:48 -04002132 }
Joe Onorato17a89222011-02-08 17:26:11 -08002133
Chris Wrenf4d08112014-01-16 18:13:56 -05002134 if (restored) {
2135 // no special handling required for restored folders
2136 restoredRows.add(id);
2137 }
2138
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2140 sBgFolders.put(folderInfo.id, folderInfo);
2141 break;
2142
2143 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002144 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002146 boolean customWidget = itemType ==
2147 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2148
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002150 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002151 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002152 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002153 user = allUsers.get(serialNumber);
2154 if (user == null) {
2155 itemsToRemove.add(id);
2156 continue;
2157 }
2158
Sunny Goyalff572272014-07-23 13:58:07 -07002159 final ComponentName component =
2160 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002161
Sunny Goyal651077b2014-06-30 14:15:31 -07002162 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002163 final boolean isIdValid = (restoreStatus &
2164 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002165 final boolean wasProviderReady = (restoreStatus &
2166 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002167
Adam Cohen59400422014-03-05 18:07:04 -08002168 final LauncherAppWidgetProviderInfo provider =
2169 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002170 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002171 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002172
2173 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002174 if (!isSafeMode && !customWidget &&
2175 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002177 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002180 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 itemsToRemove.add(id);
2182 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002183 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002184 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2185 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002186
Sunny Goyal53f96722015-07-13 19:54:53 -07002187 // The provider is available. So the widget is either
2188 // available or not available. We do not need to track
2189 // any future restore updates.
2190 int status = restoreStatus &
2191 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002192 if (!wasProviderReady) {
2193 // If provider was not previously ready, update the
2194 // status and UI flag.
2195
2196 // Id would be valid only if the widget restore broadcast was received.
2197 if (isIdValid) {
2198 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2199 } else {
2200 status &= ~LauncherAppWidgetInfo
2201 .FLAG_PROVIDER_NOT_READY;
2202 }
2203 }
2204 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002205 } else {
2206 Log.v(TAG, "Widget restore pending id=" + id
2207 + " appWidgetId=" + appWidgetId
2208 + " status =" + restoreStatus);
2209 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002210 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002211 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002212 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002213
2214 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2215 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002216 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002217 // App restore has started. Update the flag
2218 appWidgetInfo.restoreStatus |=
2219 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002220 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002221 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002222 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002223 itemsToRemove.add(id);
2224 continue;
2225 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002226
2227 appWidgetInfo.installProgress =
2228 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002229 }
Sunny Goyalff572272014-07-23 13:58:07 -07002230
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 appWidgetInfo.cellX = c.getInt(cellXIndex);
2234 appWidgetInfo.cellY = c.getInt(cellYIndex);
2235 appWidgetInfo.spanX = c.getInt(spanXIndex);
2236 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002237 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2240 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2241 Log.e(TAG, "Widget found where container != " +
2242 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2243 continue;
2244 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002246 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002248 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2249 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 break;
2251 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002252
Adam Cohen59400422014-03-05 18:07:04 -08002253 if (!customWidget) {
2254 String providerName =
2255 appWidgetInfo.providerName.flattenToString();
2256 if (!providerName.equals(savedProvider) ||
2257 (appWidgetInfo.restoreStatus != restoreStatus)) {
2258 ContentValues values = new ContentValues();
2259 values.put(
2260 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2261 providerName);
2262 values.put(LauncherSettings.Favorites.RESTORED,
2263 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002264 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002265 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002266 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2268 sBgAppWidgets.add(appWidgetInfo);
2269 }
Joe Onorato36115782010-06-17 13:28:48 -04002270 break;
2271 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002273 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002274 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 }
2276 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002277 if (c != null) {
2278 c.close();
2279 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 }
2281
Winson Chungba9c37f2013-08-30 14:11:37 -07002282 // Break early if we've stopped loading
2283 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002284 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002285 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002286 }
2287
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002290 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2291 Utilities.createDbSelectionQuery(
2292 LauncherSettings.Favorites._ID, itemsToRemove), null);
2293 if (DEBUG_LOADERS) {
2294 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2295 LauncherSettings.Favorites._ID, itemsToRemove));
2296 }
2297
2298 // Remove any empty folder
2299 for (long folderId : LauncherAppState.getLauncherProvider()
2300 .deleteEmptyFolders()) {
2301 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2302 sBgFolders.remove(folderId);
2303 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002304 }
2305 }
2306
Chris Wrenf4d08112014-01-16 18:13:56 -05002307 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002308 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002309 ContentValues values = new ContentValues();
2310 values.put(LauncherSettings.Favorites.RESTORED, 0);
2311 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2312 Utilities.createDbSelectionQuery(
2313 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002314 }
2315
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002316 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2317 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002318 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002319 null, sWorker);
2320 }
2321
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002322 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002323
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002324 // Remove any empty screens
2325 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002326 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002327 long screenId = item.screenId;
2328 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2329 unusedScreens.contains(screenId)) {
2330 unusedScreens.remove(screenId);
2331 }
2332 }
2333
2334 // If there are any empty screens remove them, and update.
2335 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002336 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 }
2339
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 if (DEBUG_LOADERS) {
2341 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2342 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002344 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002346
Sunny Goyale2df0622015-04-24 11:27:00 -07002347 for (int i = 0; i < nScreens; i++) {
2348 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002349 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 line += " | ";
2351 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002352 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002353 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002354 if (x < screen.length && y < screen[x].length) {
2355 line += (screen[x][y] != null) ? "#" : ".";
2356 } else {
2357 line += "!";
2358 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 }
Joe Onorato36115782010-06-17 13:28:48 -04002360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002363 }
Joe Onorato36115782010-06-17 13:28:48 -04002364 }
Adam Cohene25af792013-06-06 23:08:25 -07002365 }
2366
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002367 /**
2368 * Partially updates the item without any notification. Must be called on the worker thread.
2369 */
2370 private void updateItem(long itemId, ContentValues update) {
2371 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002372 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002373 update,
2374 BaseColumns._ID + "= ?",
2375 new String[]{Long.toString(itemId)});
2376 }
2377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 /** Filters the set of items who are directly or indirectly (via another container) on the
2379 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002380 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 ArrayList<ItemInfo> allWorkspaceItems,
2382 ArrayList<ItemInfo> currentScreenItems,
2383 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002384 // Purge any null ItemInfos
2385 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2386 while (iter.hasNext()) {
2387 ItemInfo i = iter.next();
2388 if (i == null) {
2389 iter.remove();
2390 }
2391 }
2392
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 // Order the set of items by their containers first, this allows use to walk through the
2394 // list sequentially, build up a list of containers that are in the specified screen,
2395 // as well as all items in those containers.
2396 Set<Long> itemsOnScreen = new HashSet<Long>();
2397 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2398 @Override
2399 public int compare(ItemInfo lhs, ItemInfo rhs) {
2400 return (int) (lhs.container - rhs.container);
2401 }
2402 });
2403 for (ItemInfo info : allWorkspaceItems) {
2404 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002405 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 currentScreenItems.add(info);
2407 itemsOnScreen.add(info.id);
2408 } else {
2409 otherScreenItems.add(info);
2410 }
2411 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2412 currentScreenItems.add(info);
2413 itemsOnScreen.add(info.id);
2414 } else {
2415 if (itemsOnScreen.contains(info.container)) {
2416 currentScreenItems.add(info);
2417 itemsOnScreen.add(info.id);
2418 } else {
2419 otherScreenItems.add(info);
2420 }
2421 }
2422 }
2423 }
2424
2425 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002426 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 ArrayList<LauncherAppWidgetInfo> appWidgets,
2428 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2429 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430
2431 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002432 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002434 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 currentScreenWidgets.add(widget);
2436 } else {
2437 otherScreenWidgets.add(widget);
2438 }
2439 }
2440 }
2441
2442 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002443 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002444 LongArrayMap<ItemInfo> itemsIdMap,
2445 LongArrayMap<FolderInfo> folders,
2446 LongArrayMap<FolderInfo> currentScreenFolders,
2447 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448
Sunny Goyale2df0622015-04-24 11:27:00 -07002449 int total = folders.size();
2450 for (int i = 0; i < total; i++) {
2451 long id = folders.keyAt(i);
2452 FolderInfo folder = folders.valueAt(i);
2453
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002455 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002457 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 currentScreenFolders.put(id, folder);
2459 } else {
2460 otherScreenFolders.put(id, folder);
2461 }
2462 }
2463 }
2464
2465 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2466 * right) */
2467 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002468 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002469 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 // XXX: review this
2471 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002472 @Override
2473 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002474 int cellCountX = (int) profile.numColumns;
2475 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002476 int screenOffset = cellCountX * cellCountY;
2477 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002479 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002480 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002481 rhs.cellY * cellCountX + rhs.cellX);
2482 return (int) (lr - rr);
2483 }
2484 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002486
Adam Cohendcd297f2013-06-18 13:13:40 -07002487 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2488 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002489 final Runnable r = new Runnable() {
2490 @Override
2491 public void run() {
2492 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2493 if (callbacks != null) {
2494 callbacks.bindScreens(orderedScreens);
2495 }
2496 }
2497 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002498 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002499 }
2500
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2502 final ArrayList<ItemInfo> workspaceItems,
2503 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002504 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002506
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 final boolean postOnMainThread = (deferredBindRunnables != null);
2508
2509 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002510 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002512 final int start = i;
2513 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 final Runnable r = new Runnable() {
2515 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002517 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002519 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2520 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 }
2522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 };
2524 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002525 synchronized (deferredBindRunnables) {
2526 deferredBindRunnables.add(r);
2527 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002529 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 }
Joe Onorato36115782010-06-17 13:28:48 -04002531 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532
2533 // Bind the folders
2534 if (!folders.isEmpty()) {
2535 final Runnable r = new Runnable() {
2536 public void run() {
2537 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2538 if (callbacks != null) {
2539 callbacks.bindFolders(folders);
2540 }
2541 }
2542 };
2543 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002544 synchronized (deferredBindRunnables) {
2545 deferredBindRunnables.add(r);
2546 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002548 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 }
2550 }
2551
2552 // Bind the widgets, one at a time
2553 N = appWidgets.size();
2554 for (int i = 0; i < N; i++) {
2555 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2556 final Runnable r = new Runnable() {
2557 public void run() {
2558 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2559 if (callbacks != null) {
2560 callbacks.bindAppWidget(widget);
2561 }
2562 }
2563 };
2564 if (postOnMainThread) {
2565 deferredBindRunnables.add(r);
2566 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002567 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568 }
2569 }
2570 }
2571
2572 /**
2573 * Binds all loaded data to actual views on the main thread.
2574 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002575 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 final long t = SystemClock.uptimeMillis();
2577 Runnable r;
2578
2579 // Don't use these two variables in any of the callback runnables.
2580 // Otherwise we hold a reference to them.
2581 final Callbacks oldCallbacks = mCallbacks.get();
2582 if (oldCallbacks == null) {
2583 // This launcher has exited and nobody bothered to tell us. Just bail.
2584 Log.w(TAG, "LoaderTask running with no launcher");
2585 return;
2586 }
2587
Winson Chung9b9fb962013-11-15 15:39:34 -08002588 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002589 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2590 ArrayList<LauncherAppWidgetInfo> appWidgets =
2591 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002592 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002593
2594 final LongArrayMap<FolderInfo> folders;
2595 final LongArrayMap<ItemInfo> itemsIdMap;
2596
Winson Chung2abf94d2012-07-18 18:16:38 -07002597 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002598 workspaceItems.addAll(sBgWorkspaceItems);
2599 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002600 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002601
2602 folders = sBgFolders.clone();
2603 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002604 }
2605
Derek Prothro7aff3992013-12-10 14:00:37 -05002606 final boolean isLoadingSynchronously =
2607 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002608 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002609 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002610 if (currScreen >= orderedScreenIds.size()) {
2611 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002612 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002613 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002614 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002615 final long currentScreenId = currentScreen < 0
2616 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002617
2618 // Load all the items that are on the current page first (and in the process, unbind
2619 // all the existing workspace items before we call startBinding() below.
2620 unbindWorkspaceItemsOnMainThread();
2621
2622 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2624 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2625 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2626 new ArrayList<LauncherAppWidgetInfo>();
2627 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2628 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002629 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2630 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002631
Winson Chung9b9fb962013-11-15 15:39:34 -08002632 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002634 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002636 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637 otherFolders);
2638 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2639 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2640
2641 // Tell the workspace that we're about to start binding items
2642 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002643 public void run() {
2644 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2645 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002646 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002647 }
2648 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002649 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002650 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002651
Adam Cohendcd297f2013-06-18 13:13:40 -07002652 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2653
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002654 // Load items on the current page
2655 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2656 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002657 if (isLoadingSynchronously) {
2658 r = new Runnable() {
2659 public void run() {
2660 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002661 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002662 callbacks.onPageBoundSynchronously(currentScreen);
2663 }
2664 }
2665 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002666 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002667 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668
Winson Chung4a2afa32012-07-19 14:53:05 -07002669 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2670 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002671 synchronized (mDeferredBindRunnables) {
2672 mDeferredBindRunnables.clear();
2673 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002674 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002675 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002676
2677 // Tell the workspace that we're done binding items
2678 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002679 public void run() {
2680 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2681 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002682 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002683 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002684
Winson Chung98e030b2012-05-07 16:01:11 -07002685 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002686 if (DEBUG_LOADERS) {
2687 Log.d(TAG, "bound workspace in "
2688 + (SystemClock.uptimeMillis()-t) + "ms");
2689 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002690
2691 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002692 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002693 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002694 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002695 synchronized (mDeferredBindRunnables) {
2696 mDeferredBindRunnables.add(r);
2697 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002698 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002699 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002700 }
Joe Onorato36115782010-06-17 13:28:48 -04002701 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002702
Joe Onorato36115782010-06-17 13:28:48 -04002703 private void loadAndBindAllApps() {
2704 if (DEBUG_LOADERS) {
2705 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2706 }
2707 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002708 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002709 synchronized (LoaderTask.this) {
2710 if (mStopped) {
2711 return;
2712 }
2713 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002714 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002715 synchronized (LoaderTask.this) {
2716 if (mStopped) {
2717 return;
2718 }
2719 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002720 }
Joe Onorato36115782010-06-17 13:28:48 -04002721 } else {
2722 onlyBindAllApps();
2723 }
2724 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002725
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002726 private void updateIconCache() {
2727 // Ignore packages which have a promise icon.
2728 HashSet<String> packagesToIgnore = new HashSet<>();
2729 synchronized (sBgLock) {
2730 for (ItemInfo info : sBgItemsIdMap) {
2731 if (info instanceof ShortcutInfo) {
2732 ShortcutInfo si = (ShortcutInfo) info;
2733 if (si.isPromise() && si.getTargetComponent() != null) {
2734 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2735 }
2736 } else if (info instanceof LauncherAppWidgetInfo) {
2737 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2738 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2739 packagesToIgnore.add(lawi.providerName.getPackageName());
2740 }
2741 }
2742 }
2743 }
2744 mIconCache.updateDbIcons(packagesToIgnore);
2745 }
2746
Joe Onorato36115782010-06-17 13:28:48 -04002747 private void onlyBindAllApps() {
2748 final Callbacks oldCallbacks = mCallbacks.get();
2749 if (oldCallbacks == null) {
2750 // This launcher has exited and nobody bothered to tell us. Just bail.
2751 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2752 return;
2753 }
2754
2755 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002756 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002757 final ArrayList<AppInfo> list
2758 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002759 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002760 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002761 public void run() {
2762 final long t = SystemClock.uptimeMillis();
2763 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2764 if (callbacks != null) {
2765 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002766 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002767 }
2768 if (DEBUG_LOADERS) {
2769 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2770 + (SystemClock.uptimeMillis()-t) + "ms");
2771 }
2772 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002773 };
2774 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002775 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002776 r.run();
2777 } else {
2778 mHandler.post(r);
2779 }
Joe Onorato36115782010-06-17 13:28:48 -04002780 }
2781
Winson Chung64359a52013-07-08 17:17:08 -07002782 private void loadAllApps() {
2783 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002784
Joe Onorato36115782010-06-17 13:28:48 -04002785 final Callbacks oldCallbacks = mCallbacks.get();
2786 if (oldCallbacks == null) {
2787 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002788 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002789 return;
2790 }
2791
Kenny Guyed131872014-04-30 03:02:21 +01002792 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2793
Winson Chung64359a52013-07-08 17:17:08 -07002794 // Clear the list of apps
2795 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002796 for (UserHandleCompat user : profiles) {
2797 // Query for the set of apps
2798 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002799 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002800 if (DEBUG_LOADERS) {
2801 Log.d(TAG, "getActivityList took "
2802 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2803 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2804 }
2805 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002806 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002807 if (apps == null || apps.isEmpty()) {
2808 return;
2809 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002810
Kenny Guyed131872014-04-30 03:02:21 +01002811 // Create the ApplicationInfos
2812 for (int i = 0; i < apps.size(); i++) {
2813 LauncherActivityInfoCompat app = apps.get(i);
2814 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002815 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002816 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002817
Sunny Goyal756a28a2015-04-23 17:07:55 -07002818 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2819 if (heuristic != null) {
2820 runAfterBindCompletes(new Runnable() {
2821
2822 @Override
2823 public void run() {
2824 heuristic.processUserApps(apps);
2825 }
2826 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002827 }
Winson Chung64359a52013-07-08 17:17:08 -07002828 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002829 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002830 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2831 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002832
2833 // Post callback on main thread
2834 mHandler.post(new Runnable() {
2835 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002836
Winson Chung64359a52013-07-08 17:17:08 -07002837 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002838 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002839 if (callbacks != null) {
2840 callbacks.bindAllApplications(added);
2841 if (DEBUG_LOADERS) {
2842 Log.d(TAG, "bound " + added.size() + " apps in "
2843 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2844 }
2845 } else {
2846 Log.i(TAG, "not binding apps: no Launcher activity");
2847 }
2848 }
2849 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002850 // Cleanup any data stored for a deleted user.
2851 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002852
Hyunyoung Song219d0482015-05-07 12:51:42 -07002853 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2854 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002855 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002856 Log.d(TAG, "Icons processed in "
2857 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002858 }
2859 }
2860
2861 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002862 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002863 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002864 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2865 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2866 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2867 }
Joe Onorato36115782010-06-17 13:28:48 -04002868 }
2869 }
2870
Sunny Goyal75b0f552015-05-20 21:57:06 -07002871 /**
2872 * Called when the icons for packages have been updated in the icon cache.
2873 */
2874 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2875 final Callbacks callbacks = getCallback();
2876 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2877 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2878
2879 // If any package icon has changed (app was updated while launcher was dead),
2880 // update the corresponding shortcuts.
2881 synchronized (sBgLock) {
2882 for (ItemInfo info : sBgItemsIdMap) {
2883 if (info instanceof ShortcutInfo && user.equals(info.user)
2884 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2885 ShortcutInfo si = (ShortcutInfo) info;
2886 ComponentName cn = si.getTargetComponent();
2887 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2888 si.updateIcon(mIconCache);
2889 updatedShortcuts.add(si);
2890 }
2891 }
2892 }
2893 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2894 }
2895
2896 if (!updatedShortcuts.isEmpty()) {
2897 final UserHandleCompat userFinal = user;
2898 mHandler.post(new Runnable() {
2899
2900 public void run() {
2901 Callbacks cb = getCallback();
2902 if (cb != null && callbacks == cb) {
2903 cb.bindShortcutsChanged(updatedShortcuts,
2904 new ArrayList<ShortcutInfo>(), userFinal);
2905 }
2906 }
2907 });
2908 }
2909
2910 if (!updatedApps.isEmpty()) {
2911 mHandler.post(new Runnable() {
2912
2913 public void run() {
2914 Callbacks cb = getCallback();
2915 if (cb != null && callbacks == cb) {
2916 cb.bindAppsUpdated(updatedApps);
2917 }
2918 }
2919 });
2920 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002921
2922 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2923 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002924 }
2925
Joe Onorato36115782010-06-17 13:28:48 -04002926 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002927 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002928 }
2929
Adam Cohen091440a2015-03-18 14:16:05 -07002930 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002931
2932 @Override
2933 public void onReceive(Context context, Intent intent) {
2934 synchronized (sBgLock) {
2935 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2936 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002937 final PackageManager manager = context.getPackageManager();
2938 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2939 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002940 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2941 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002942 packagesRemoved.clear();
2943 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002944 for (String pkg : entry.getValue()) {
2945 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002946 boolean packageOnSdcard = launcherApps.isAppEnabled(
2947 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2948 if (packageOnSdcard) {
2949 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2950 packagesUnavailable.add(pkg);
2951 } else {
2952 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2953 packagesRemoved.add(pkg);
2954 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002955 }
2956 }
2957 if (!packagesRemoved.isEmpty()) {
2958 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2959 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2960 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002961 if (!packagesUnavailable.isEmpty()) {
2962 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2963 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2964 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002965 }
Sunny Goyal34942622014-08-29 17:20:55 -07002966 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002967 }
2968 }
2969 }
2970
Joe Onorato36115782010-06-17 13:28:48 -04002971 private class PackageUpdatedTask implements Runnable {
2972 int mOp;
2973 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002974 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002975
2976 public static final int OP_NONE = 0;
2977 public static final int OP_ADD = 1;
2978 public static final int OP_UPDATE = 2;
2979 public static final int OP_REMOVE = 3; // uninstlled
2980 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2981
2982
Kenny Guyed131872014-04-30 03:02:21 +01002983 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002984 mOp = op;
2985 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002986 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002987 }
2988
2989 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002990 if (!mHasLoaderCompletedOnce) {
2991 // Loader has not yet run.
2992 return;
2993 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002994 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002995
2996 final String[] packages = mPackages;
2997 final int N = packages.length;
2998 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002999 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003000 for (int i=0; i<N; i++) {
3001 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003002 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003003 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003004 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003005
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003006 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3007 if (heuristic != null) {
3008 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003009 }
Joe Onorato36115782010-06-17 13:28:48 -04003010 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003011 }
Joe Onorato36115782010-06-17 13:28:48 -04003012 case OP_UPDATE:
3013 for (int i=0; i<N; i++) {
3014 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003015 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003016 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003017 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003018 }
3019 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003020 case OP_REMOVE: {
3021 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3022 if (heuristic != null) {
3023 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024 }
Joe Onorato36115782010-06-17 13:28:48 -04003025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003027 mIconCache.removeIconsForPkg(packages[i], mUser);
3028 }
3029 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003031 case OP_UNAVAILABLE:
3032 for (int i=0; i<N; i++) {
3033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3034 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003035 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003036 }
3037 break;
3038 }
3039
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003040 ArrayList<AppInfo> added = null;
3041 ArrayList<AppInfo> modified = null;
3042 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003043
Adam Cohen487f7dd2012-06-28 18:12:10 -07003044 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003045 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003046 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003047 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003048 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003049 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003050 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003051 }
Winson Chung5d55f332012-07-16 20:45:03 -07003052 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003053 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003054 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003055 }
3056
Sunny Goyale0f58d72014-11-10 18:05:31 -08003057 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003058 if (callbacks == null) {
3059 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3060 return;
3061 }
3062
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3064 new HashMap<ComponentName, AppInfo>();
3065
Joe Onorato36115782010-06-17 13:28:48 -04003066 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003067 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068 for (AppInfo ai : added) {
3069 addedOrUpdatedApps.put(ai.componentName, ai);
3070 }
Joe Onorato36115782010-06-17 13:28:48 -04003071 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003072
Joe Onorato36115782010-06-17 13:28:48 -04003073 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003074 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075 for (AppInfo ai : modified) {
3076 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003077 }
3078
Joe Onorato36115782010-06-17 13:28:48 -04003079 mHandler.post(new Runnable() {
3080 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003081 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003082 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003083 callbacks.bindAppsUpdated(modifiedFinal);
3084 }
3085 }
3086 });
3087 }
Winson Chung83892cc2013-05-01 16:53:33 -07003088
Sunny Goyal4390ace2014-10-13 11:33:11 -07003089 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003090 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3092 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3093 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3094
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003095 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003096 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003097 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003098 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3099 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 boolean infoUpdated = false;
3101 boolean shortcutUpdated = false;
3102
3103 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003104 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003106 Bitmap icon = Utilities.createIconBitmap(
3107 si.iconResource.packageName,
3108 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 if (icon != null) {
3110 si.setIcon(icon);
3111 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003112 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003113 }
3114 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115
3116 ComponentName cn = si.getTargetComponent();
3117 if (cn != null && packageSet.contains(cn.getPackageName())) {
3118 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3119
3120 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3122 // Auto install icon
3123 PackageManager pm = context.getPackageManager();
3124 ResolveInfo matched = pm.resolveActivity(
3125 new Intent(Intent.ACTION_MAIN)
3126 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3127 PackageManager.MATCH_DEFAULT_ONLY);
3128 if (matched == null) {
3129 // Try to find the best match activity.
3130 Intent intent = pm.getLaunchIntentForPackage(
3131 cn.getPackageName());
3132 if (intent != null) {
3133 cn = intent.getComponent();
3134 appInfo = addedOrUpdatedApps.get(cn);
3135 }
3136
3137 if ((intent == null) || (appInfo == null)) {
3138 removedShortcuts.add(si);
3139 continue;
3140 }
3141 si.promisedIntent = intent;
3142 }
3143 }
3144
3145 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003146 if (appInfo != null) {
3147 si.flags = appInfo.flags;
3148 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003149
Sunny Goyal756adbc2015-04-16 15:20:51 -07003150 si.intent = si.promisedIntent;
3151 si.promisedIntent = null;
3152 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003153 infoUpdated = true;
3154 si.updateIcon(mIconCache);
3155 }
3156
3157 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3158 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3159 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003160 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003161 si.contentDescription = appInfo.contentDescription;
3162 infoUpdated = true;
3163 }
3164
3165 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3166 // Since package was just updated, the target must be available now.
3167 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3168 shortcutUpdated = true;
3169 }
3170 }
3171
3172 if (infoUpdated || shortcutUpdated) {
3173 updatedShortcuts.add(si);
3174 }
3175 if (infoUpdated) {
3176 updateItemInDatabase(context, si);
3177 }
3178 } else if (info instanceof LauncherAppWidgetInfo) {
3179 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3180 if (mUser.equals(widgetInfo.user)
3181 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3182 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003183 widgetInfo.restoreStatus &=
3184 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3185 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003186 widgets.add(widgetInfo);
3187 updateItemInDatabase(context, widgetInfo);
3188 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003189 }
3190 }
3191 }
3192
Sunny Goyal4390ace2014-10-13 11:33:11 -07003193 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3194 mHandler.post(new Runnable() {
3195
3196 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003197 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 if (callbacks == cb && cb != null) {
3199 callbacks.bindShortcutsChanged(
3200 updatedShortcuts, removedShortcuts, mUser);
3201 }
3202 }
3203 });
3204 if (!removedShortcuts.isEmpty()) {
3205 deleteItemsFromDatabase(context, removedShortcuts);
3206 }
3207 }
3208 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 });
3217 }
3218 }
3219
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 final ArrayList<String> removedPackageNames =
3221 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003222 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003223 // Mark all packages in the broadcast to be removed
3224 removedPackageNames.addAll(Arrays.asList(packages));
3225 } else if (mOp == OP_UPDATE) {
3226 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003227 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003228 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003229 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003230 }
3231 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003232 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003233
Winson Chungdf95eb12013-10-16 14:57:07 -07003234 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003235 final int removeReason;
3236 if (mOp == OP_UNAVAILABLE) {
3237 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3238 } else {
3239 // Remove all the components associated with this package
3240 for (String pn : removedPackageNames) {
3241 deletePackageFromDatabase(context, pn, mUser);
3242 }
3243 // Remove all the specific components
3244 for (AppInfo a : removedApps) {
3245 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3246 deleteItemsFromDatabase(context, infos);
3247 }
3248 removeReason = 0;
3249 }
3250
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003252 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003254 mHandler.post(new Runnable() {
3255 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003256 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003257 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003258 callbacks.bindComponentsRemoved(
3259 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003260 }
3261 }
3262 });
Joe Onoratobe386092009-11-17 17:32:16 -08003263 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003264
3265 // onProvidersChanged method (API >= 17) already refreshed the widget list
3266 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3267
Adam Cohen4caf2982013-08-20 18:54:31 -07003268 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003269 mHandler.post(new Runnable() {
3270 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003271 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003272 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003273 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003274 }
3275 }
3276 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 }
3278 }
3279
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003280 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3281 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003282 ArrayList<LauncherAppWidgetProviderInfo> results =
3283 new ArrayList<LauncherAppWidgetProviderInfo>();
3284 try {
3285 synchronized (sBgLock) {
3286 if (sBgWidgetProviders == null || refresh) {
3287 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3288 = new HashMap<>();
3289 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3290 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003291
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003292 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3293 for (AppWidgetProviderInfo pInfo : widgets) {
3294 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3295 UserHandleCompat user = wm.getUser(info);
3296 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3297 }
Robin Lee26ace122015-03-16 19:41:43 +00003298
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003299 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3300 for (CustomAppWidget widget : customWidgets) {
3301 info = new LauncherAppWidgetProviderInfo(context, widget);
3302 UserHandleCompat user = wm.getUser(info);
3303 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3304 }
3305 // Replace the global list at the very end, so that if there is an exception,
3306 // previously loaded provider list is used.
3307 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003308 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003309 results.addAll(sBgWidgetProviders.values());
3310 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003311 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003312 } catch (Exception e) {
3313 if (e.getCause() instanceof TransactionTooLargeException) {
3314 // the returned value may be incomplete and will not be refreshed until the next
3315 // time Launcher starts.
3316 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3317 // onResume is called to refresh the widget provider list.
3318 synchronized (sBgLock) {
3319 if (sBgWidgetProviders != null) {
3320 results.addAll(sBgWidgetProviders.values());
3321 }
3322 return results;
3323 }
3324 } else {
3325 throw e;
3326 }
Adam Cohen59400422014-03-05 18:07:04 -08003327 }
3328 }
3329
Robin Lee26ace122015-03-16 19:41:43 +00003330 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3331 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003332 synchronized (sBgLock) {
3333 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003334 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003335 }
Robin Lee26ace122015-03-16 19:41:43 +00003336 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003337 }
3338 }
3339
Hyunyoung Song227239e2015-05-04 18:17:35 -07003340 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3341 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003342
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003343 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003344 @Override
3345 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003346 updateWidgetsModel(context, refresh);
3347 final WidgetsModel model = mBgWidgetsModel.clone();
3348
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003349 mHandler.post(new Runnable() {
3350 @Override
3351 public void run() {
3352 Callbacks cb = getCallback();
3353 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003354 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003355 }
3356 }
3357 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003358 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003359 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3360 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003361 }
3362 });
3363 }
3364
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003365 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003366 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003367 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003368 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003369 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003370 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 PackageManager packageManager = context.getPackageManager();
3372 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003373 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3375 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003376 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003377 }
3378
Adam Cohen091440a2015-03-18 14:16:05 -07003379 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003380 UserHandleCompat user) {
3381 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3382 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003383 }
Adam Cohen556f6132014-01-15 15:18:08 -08003384
Kenny Guyed131872014-04-30 03:02:21 +01003385 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3386 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003387 if (cn == null) {
3388 return false;
3389 }
Kenny Guyed131872014-04-30 03:02:21 +01003390 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3391 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003392 return false;
3393 }
Kenny Guyed131872014-04-30 03:02:21 +01003394 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003395 }
3396
Adam Cohena28b78e2014-05-20 17:03:04 -07003397 public static boolean isValidPackage(Context context, String packageName,
3398 UserHandleCompat user) {
3399 if (packageName == null) {
3400 return false;
3401 }
3402 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3403 return launcherApps.isPackageEnabledForProfile(packageName, user);
3404 }
3405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003407 * Make an ShortcutInfo object for a restored application or shortcut item that points
3408 * to a package that is not yet installed on the system.
3409 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003410 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003411 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003412 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003413 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003414
3415 Bitmap icon = iconInfo.loadIcon(c, info, context);
3416 // the fallback icon
3417 if (icon == null) {
3418 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3419 } else {
3420 info.setIcon(icon);
3421 }
Sunny Goyal34942622014-08-29 17:20:55 -07003422
3423 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003424 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003425 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003426 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003427 }
Sunny Goyal34942622014-08-29 17:20:55 -07003428 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3429 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003430 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003431 }
Sunny Goyal34942622014-08-29 17:20:55 -07003432 } else {
3433 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3434 }
3435
Winson Chung82b016c2015-05-08 17:00:10 -07003436 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003437 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003438 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003439 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 return info;
3441 }
3442
3443 /**
3444 * Make an Intent object for a restored application or shortcut item that points
3445 * to the market page for the item.
3446 */
Adam Cohen091440a2015-03-18 14:16:05 -07003447 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003448 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003449 return getMarketIntent(componentName.getPackageName());
3450 }
3451
3452 static Intent getMarketIntent(String packageName) {
3453 return new Intent(Intent.ACTION_VIEW)
3454 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003455 .scheme("market")
3456 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003457 .appendQueryParameter("id", packageName)
3458 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003459 }
3460
3461 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003462 * Make an ShortcutInfo object for a shortcut that is an application.
3463 *
3464 * If c is not null, then it will be used to fill in missing data like the title and icon.
3465 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003466 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003467 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003468 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003469 if (user == null) {
3470 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003471 return null;
3472 }
3473
Kenny Guyed131872014-04-30 03:02:21 +01003474 ComponentName componentName = intent.getComponent();
3475 if (componentName == null) {
3476 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3477 return null;
3478 }
3479
3480 Intent newIntent = new Intent(intent.getAction(), null);
3481 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3482 newIntent.setComponent(componentName);
3483 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003484 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003485 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3486 return null;
3487 }
3488
3489 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003490 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003491 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3492 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3493 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003494 }
3495
Joe Onorato56d82912010-03-07 14:32:10 -05003496 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003497 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003498 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003499 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003500
Joe Onorato56d82912010-03-07 14:32:10 -05003501 // fall back to the class name of the activity
3502 if (info.title == null) {
3503 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003504 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003505
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003507 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003508 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003509 if (lai != null) {
3510 info.flags = AppInfo.initFlags(lai);
3511 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003513 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003514
Sunny Goyale2df0622015-04-24 11:27:00 -07003515 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003516 ItemInfoFilter f) {
3517 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3518 for (ItemInfo i : infos) {
3519 if (i instanceof ShortcutInfo) {
3520 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003521 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003522 if (cn != null && f.filterItem(null, info, cn)) {
3523 filtered.add(info);
3524 }
3525 } else if (i instanceof FolderInfo) {
3526 FolderInfo info = (FolderInfo) i;
3527 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003528 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003529 if (cn != null && f.filterItem(info, s, cn)) {
3530 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003531 }
3532 }
Winson Chung64359a52013-07-08 17:17:08 -07003533 } else if (i instanceof LauncherAppWidgetInfo) {
3534 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3535 ComponentName cn = info.providerName;
3536 if (cn != null && f.filterItem(null, info, cn)) {
3537 filtered.add(info);
3538 }
Winson Chung8a435102012-08-30 17:16:53 -07003539 }
3540 }
Winson Chung64359a52013-07-08 17:17:08 -07003541 return new ArrayList<ItemInfo>(filtered);
3542 }
3543
Adam Cohen091440a2015-03-18 14:16:05 -07003544 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003545 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003546 ItemInfoFilter filter = new ItemInfoFilter() {
3547 @Override
3548 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003549 if (info.user == null) {
3550 return cn.equals(cname);
3551 } else {
3552 return cn.equals(cname) && info.user.equals(user);
3553 }
Winson Chung64359a52013-07-08 17:17:08 -07003554 }
3555 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003556 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003557 }
3558
Sunny Goyal1a745e82014-10-02 15:58:31 -07003559 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003560 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561 */
Adam Cohen091440a2015-03-18 14:16:05 -07003562 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003563 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003564 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003565 // Non-app shortcuts are only supported for current user.
3566 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003568
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003569 // TODO: If there's an explicit component and we can't install that, delete it.
3570
Winson Chung82b016c2015-05-08 17:00:10 -07003571 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003572
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003573 Bitmap icon = iconInfo.loadIcon(c, info, context);
3574 // the fallback icon
3575 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003576 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003577 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003579 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 return info;
3581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003582
Sunny Goyal2350bc92014-10-14 16:42:54 -07003583 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003584 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3585 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3586 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3587
Adam Cohend9198822011-11-22 16:42:47 -08003588 if (intent == null) {
3589 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3590 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3591 return null;
3592 }
3593
Joe Onorato0589f0f2010-02-08 13:44:00 -08003594 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003595 boolean customIcon = false;
3596 ShortcutIconResource iconResource = null;
3597
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003598 if (bitmap instanceof Bitmap) {
3599 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003600 customIcon = true;
3601 } else {
3602 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003603 if (extra instanceof ShortcutIconResource) {
3604 iconResource = (ShortcutIconResource) extra;
3605 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003606 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 }
3608 }
3609
Michael Jurkac9d95c52011-08-29 14:03:34 -07003610 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003611
Kenny Guyed131872014-04-30 03:02:21 +01003612 // Only support intents for current user for now. Intents sent from other
3613 // users wouldn't get here without intent forwarding anyway.
3614 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003615 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003616 icon = mIconCache.getDefaultIcon(info.user);
3617 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003618 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003619 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003620
Winson Chung82b016c2015-05-08 17:00:10 -07003621 info.title = Utilities.trim(name);
3622 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003623 info.intent = intent;
3624 info.customIcon = customIcon;
3625 info.iconResource = iconResource;
3626
3627 return info;
3628 }
3629
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003631 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 * or make a new one.
3633 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003634 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003635 // See if a placeholder was created for us already
3636 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003637 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003639 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 folders.put(id, folderInfo);
3641 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003642 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003643 }
3644
Joe Onoratobe386092009-11-17 17:32:16 -08003645
Sunny Goyal651077b2014-06-30 14:15:31 -07003646 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3647 return (provider != null) && (provider.provider != null)
3648 && (provider.provider.getPackageName() != null);
3649 }
3650
Joe Onoratobe386092009-11-17 17:32:16 -08003651 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003652 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003653 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3654 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3655 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3656 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003657 if (mLoaderTask != null) {
3658 mLoaderTask.dumpState();
3659 } else {
3660 Log.d(TAG, "mLoaderTask=null");
3661 }
Joe Onoratobe386092009-11-17 17:32:16 -08003662 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003663
3664 public Callbacks getCallback() {
3665 return mCallbacks != null ? mCallbacks.get() : null;
3666 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003667
3668 /**
3669 * @return {@link FolderInfo} if its already loaded.
3670 */
3671 public FolderInfo findFolderById(Long folderId) {
3672 synchronized (sBgLock) {
3673 return sBgFolders.get(folderId);
3674 }
3675 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003676
3677 /**
3678 * @return the looper for the worker thread which can be used to start background tasks.
3679 */
3680 public static Looper getWorkerLooper() {
3681 return sWorkerThread.getLooper();
3682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003683}