blob: ad176aeca3d6c38d27939d2211fc6a83b6b12006 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070039import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080040import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040041import android.os.Handler;
42import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070043import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040048import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080049import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080051import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070052import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010053
Sunny Goyalffe83f12014-08-14 17:39:34 -070054import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010055import com.android.launcher3.compat.LauncherActivityInfoCompat;
56import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070057import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070058import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010059import com.android.launcher3.compat.UserHandleCompat;
60import com.android.launcher3.compat.UserManagerCompat;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
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;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700170 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700171 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
172 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700174 // </ only access in worker thread >
175
Adam Cohen091440a2015-03-18 14:16:05 -0700176 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Reena Lee99a73f32011-10-24 17:27:37 -0700178 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700179
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 Song8821ca92015-05-04 16:28:20 -0700206 public void bindAllPackages(ArrayList<Object> widgetsAndShortcuts);
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);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700244 Configuration config = res.getConfiguration();
245 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
263 private static void runOnWorkerThread(Runnable r) {
264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Sunny Goyal756a28a2015-04-23 17:07:55 -0700272 /**
273 * Runs the specified runnable after the loader is complete
274 */
275 private void runAfterBindCompletes(Runnable r) {
276 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
277 synchronized (mBindCompleteRunnables) {
278 mBindCompleteRunnables.add(r);
279 }
280 } else {
281 runOnWorkerThread(r);
282 }
283 }
284
Winson Chunge43a1e72014-01-15 10:33:02 -0800285 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
286 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800287 }
288
Sunny Goyal756adbc2015-04-16 15:20:51 -0700289 public void setPackageState(final PackageInstallInfo installInfo) {
290 Runnable updateRunnable = new Runnable() {
291
292 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500293 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700294 synchronized (sBgLock) {
295 final HashSet<ItemInfo> updates = new HashSet<>();
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
298 // Ignore install success events as they are handled by Package add events.
299 return;
300 }
301
Sunny Goyale2df0622015-04-24 11:27:00 -0700302 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700303 if (info instanceof ShortcutInfo) {
304 ShortcutInfo si = (ShortcutInfo) info;
305 ComponentName cn = si.getTargetComponent();
306 if (si.isPromise() && (cn != null)
307 && installInfo.packageName.equals(cn.getPackageName())) {
308 si.setInstallProgress(installInfo.progress);
309
310 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
311 // Mark this info as broken.
312 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
313 }
314 updates.add(si);
315 }
316 }
317 }
318
319 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
320 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
321 widget.installProgress = installInfo.progress;
322 updates.add(widget);
323 }
324 }
325
326 if (!updates.isEmpty()) {
327 // Push changes to the callback.
328 Runnable r = new Runnable() {
329 public void run() {
330 Callbacks callbacks = getCallback();
331 if (callbacks != null) {
332 callbacks.bindRestoreItemsChange(updates);
333 }
334 }
335 };
336 mHandler.post(r);
337 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500338 }
339 }
340 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500342 }
343
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 /**
345 * Updates the icons and label of all pending icons for the provided package name.
346 */
347 public void updateSessionDisplayInfo(final String packageName) {
348 Runnable updateRunnable = new Runnable() {
349
350 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700351 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700352 synchronized (sBgLock) {
353 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
354 final UserHandleCompat user = UserHandleCompat.myUserHandle();
355
Sunny Goyale2df0622015-04-24 11:27:00 -0700356 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700357 if (info instanceof ShortcutInfo) {
358 ShortcutInfo si = (ShortcutInfo) info;
359 ComponentName cn = si.getTargetComponent();
360 if (si.isPromise() && (cn != null)
361 && packageName.equals(cn.getPackageName())) {
362 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
363 // For auto install apps update the icon as well as label.
364 mIconCache.getTitleAndIcon(si,
365 si.promisedIntent, user,
366 si.shouldUseLowResIcon());
367 } else {
368 // Only update the icon for restored apps.
369 si.updateIcon(mIconCache);
370 }
371 updates.add(si);
372 }
373 }
374 }
375
376 if (!updates.isEmpty()) {
377 // Push changes to the callback.
378 Runnable r = new Runnable() {
379 public void run() {
380 Callbacks callbacks = getCallback();
381 if (callbacks != null) {
382 callbacks.bindShortcutsChanged(updates,
383 new ArrayList<ShortcutInfo>(), user);
384 }
385 }
386 };
387 mHandler.post(r);
388 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700389 }
390 }
391 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700392 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700393 }
394
Adam Cohen76a47a12014-02-05 11:47:43 -0800395 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800396 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800397
398 if (allAppsApps == null) {
399 throw new RuntimeException("allAppsApps must not be null");
400 }
401 if (allAppsApps.isEmpty()) {
402 return;
403 }
404
405 // Process the newly added applications and add them to the database first
406 Runnable r = new Runnable() {
407 public void run() {
408 runOnMainThread(new Runnable() {
409 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800410 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800411 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500412 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800413 }
414 }
415 });
416 }
417 };
418 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700419 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800420
Sunny Goyala9116722015-04-29 13:55:58 -0700421 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 int[] xy, int spanX, int spanY) {
423 LauncherAppState app = LauncherAppState.getInstance();
424 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
425 final int xCount = (int) grid.numColumns;
426 final int yCount = (int) grid.numRows;
427 boolean[][] occupied = new boolean[xCount][yCount];
428 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700429 for (ItemInfo r : occupiedPos) {
430 int right = r.cellX + r.spanX;
431 int bottom = r.cellY + r.spanY;
432 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
433 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 occupied[x][y] = true;
435 }
436 }
437 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800438 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700439 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 }
441
442 /**
443 * Find a position on the screen for the given size or adds a new screen.
444 * @return screenId and the coordinates for the item.
445 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700446 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 ArrayList<Long> workspaceScreens,
449 ArrayList<Long> addedWorkspaceScreensFinal,
450 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700451 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452
Sunny Goyala9116722015-04-29 13:55:58 -0700453 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700454 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700455 synchronized (sBgLock) {
456 for (ItemInfo info : sBgItemsIdMap) {
457 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
458 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
459 if (items == null) {
460 items = new ArrayList<>();
461 screenItems.put(info.screenId, items);
462 }
463 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
467
468 // Find appropriate space for the item.
469 long screenId = 0;
470 int[] cordinates = new int[2];
471 boolean found = false;
472
473 int screenCount = workspaceScreens.size();
474 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700475 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800476 if (preferredScreenIndex < screenCount) {
477 screenId = workspaceScreens.get(preferredScreenIndex);
478 found = findNextAvailableIconSpaceInScreen(
479 screenItems.get(screenId), cordinates, spanX, spanY);
480 }
481
482 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700483 // Search on any of the screens starting from the first screen.
484 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 screenId = workspaceScreens.get(screen);
486 if (findNextAvailableIconSpaceInScreen(
487 screenItems.get(screenId), cordinates, spanX, spanY)) {
488 // We found a space for it
489 found = true;
490 break;
491 }
492 }
493 }
494
495 if (!found) {
496 // Still no position found. Add a new screen to the end.
497 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
498
499 // Save the screen id for binding in the workspace
500 workspaceScreens.add(screenId);
501 addedWorkspaceScreensFinal.add(screenId);
502
503 // If we still can't find an empty space, then God help us all!!!
504 if (!findNextAvailableIconSpaceInScreen(
505 screenItems.get(screenId), cordinates, spanX, spanY)) {
506 throw new RuntimeException("Can't find space to add the item");
507 }
508 }
509 return Pair.create(screenId, cordinates);
510 }
511
512 /**
513 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700516 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800518 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700519 return;
Winson Chung997a9232013-07-24 15:33:46 -0700520 }
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Process the newly added applications and add them to the database first
522 Runnable r = new Runnable() {
523 public void run() {
524 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
525 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
526
Winson Chung76828c82013-08-19 15:43:29 -0700527 // Get the list of workspace screens. We need to append to this list and
528 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
529 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800530 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700531 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700533 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700535 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700537 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 }
Winson Chung76828c82013-08-19 15:43:29 -0700539
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700541 Pair<Long, int[]> coords = findSpaceForItem(context,
542 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800543 1, 1);
544 long screenId = coords.first;
545 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700546
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700547 ItemInfo itemInfo;
548 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
549 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800550 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700551 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700552 } else {
553 throw new RuntimeException("Unexpected info type");
554 }
Winson Chung94d67682013-09-25 16:29:40 -0700555
Winson Chung64359a52013-07-08 17:17:08 -0700556 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700557 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700558 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700559 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700560 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700561 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700562 }
563 }
564
Winson Chung76828c82013-08-19 15:43:29 -0700565 // Update the workspace screens
566 updateWorkspaceScreenOrder(context, workspaceScreens);
567
Adam Cohen76a47a12014-02-05 11:47:43 -0800568 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700569 runOnMainThread(new Runnable() {
570 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800571 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700572 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700573 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
574 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700575 if (!addedShortcutsFinal.isEmpty()) {
576 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
577 long lastScreenId = info.screenId;
578 for (ItemInfo i : addedShortcutsFinal) {
579 if (i.screenId == lastScreenId) {
580 addAnimated.add(i);
581 } else {
582 addNotAnimated.add(i);
583 }
Winson Chung997a9232013-07-24 15:33:46 -0700584 }
585 }
Winson Chungd64d1762013-08-20 14:37:16 -0700586 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800587 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700588 }
Winson Chung64359a52013-07-08 17:17:08 -0700589 }
Winson Chung997a9232013-07-24 15:33:46 -0700590 });
591 }
Winson Chung64359a52013-07-08 17:17:08 -0700592 }
593 };
594 runOnWorkerThread(r);
595 }
596
Sunny Goyald33860f2015-04-23 16:02:20 -0700597 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700598 if (sWorkerThread.getThreadId() == Process.myTid()) {
599 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
600 "main thread");
601 }
602
603 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400604 synchronized (mDeferredBindRunnables) {
605 mDeferredBindRunnables.clear();
606 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700607
608 // Remove any queued UI runnables
609 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700610 // Unbind all the workspace items
611 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700612 }
613
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700615 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700616 // Ensure that we don't use the same workspace items data structure on the main thread
617 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700618 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700619 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700620 tmpItems.addAll(sBgWorkspaceItems);
621 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700622 }
623 Runnable r = new Runnable() {
624 @Override
625 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700626 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700627 item.unbind();
628 }
629 }
630 };
631 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700632 }
633
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634 /**
635 * Adds an item to the DB if it was not created previously, or move it to a new
636 * <container, screen, cellX, cellY>
637 */
638 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 if (item.container == ItemInfo.NO_ID) {
641 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700642 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 } else {
644 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
647 }
648
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700649 static void checkItemInfoLocked(
650 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
651 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
652 if (modelItem != null && item != modelItem) {
653 // check all the data is consistent
654 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
655 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
656 ShortcutInfo shortcut = (ShortcutInfo) item;
657 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
658 modelShortcut.intent.filterEquals(shortcut.intent) &&
659 modelShortcut.id == shortcut.id &&
660 modelShortcut.itemType == shortcut.itemType &&
661 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700662 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700663 modelShortcut.cellX == shortcut.cellX &&
664 modelShortcut.cellY == shortcut.cellY &&
665 modelShortcut.spanX == shortcut.spanX &&
666 modelShortcut.spanY == shortcut.spanY &&
667 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
668 (modelShortcut.dropPos != null &&
669 shortcut.dropPos != null &&
670 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
671 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
672 // For all intents and purposes, this is the same object
673 return;
674 }
675 }
676
677 // the modelItem needs to match up perfectly with item if our model is
678 // to be consistent with the database-- for now, just require
679 // modelItem == item or the equality check above
680 String msg = "item: " + ((item != null) ? item.toString() : "null") +
681 "modelItem: " +
682 ((modelItem != null) ? modelItem.toString() : "null") +
683 "Error: ItemInfo passed to checkItemInfo doesn't match original";
684 RuntimeException e = new RuntimeException(msg);
685 if (stackTrace != null) {
686 e.setStackTrace(stackTrace);
687 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800688 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700689 }
690 }
691
Michael Jurka816474f2012-06-25 14:49:02 -0700692 static void checkItemInfo(final ItemInfo item) {
693 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
694 final long itemId = item.id;
695 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700696 public void run() {
697 synchronized (sBgLock) {
698 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700699 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700700 }
701 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700702 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700703 }
704
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
706 final ItemInfo item, final String callingFunction) {
707 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700708 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709 final ContentResolver cr = context.getContentResolver();
710
Adam Cohen487f7dd2012-06-28 18:12:10 -0700711 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712 Runnable r = new Runnable() {
713 public void run() {
714 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700715 updateItemArrays(item, itemId, stackTrace);
716 }
717 };
718 runOnWorkerThread(r);
719 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700721 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
722 final ArrayList<ItemInfo> items, final String callingFunction) {
723 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700724
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
726 Runnable r = new Runnable() {
727 public void run() {
728 ArrayList<ContentProviderOperation> ops =
729 new ArrayList<ContentProviderOperation>();
730 int count = items.size();
731 for (int i = 0; i < count; i++) {
732 ItemInfo item = items.get(i);
733 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700734 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700735 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700736
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
738 updateItemArrays(item, itemId, stackTrace);
739
740 }
741 try {
742 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
743 } catch (Exception e) {
744 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700745 }
746 }
747 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700748 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700749 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700750
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
752 // Lock on mBgLock *after* the db operation
753 synchronized (sBgLock) {
754 checkItemInfoLocked(itemId, item, stackTrace);
755
756 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
757 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
758 // Item is in a folder, make sure this folder exists
759 if (!sBgFolders.containsKey(item.container)) {
760 // An items container is being set to a that of an item which is not in
761 // the list of Folders.
762 String msg = "item: " + item + " container being set to: " +
763 item.container + ", not in the list of folders";
764 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 }
766 }
767
768 // Items are added/removed from the corresponding FolderInfo elsewhere, such
769 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
770 // that are on the desktop, as appropriate
771 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800772 if (modelItem != null &&
773 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
774 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775 switch (modelItem.itemType) {
776 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
777 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
778 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
779 if (!sBgWorkspaceItems.contains(modelItem)) {
780 sBgWorkspaceItems.add(modelItem);
781 }
782 break;
783 default:
784 break;
785 }
786 } else {
787 sBgWorkspaceItems.remove(modelItem);
788 }
789 }
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700794 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700795 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 item.cellX = cellX;
799 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 // We store hotseat items in canonical form which is this orientation invariant position
802 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700803 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700805 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809
810 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
813 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800814 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816
Michael Jurkac9d95c52011-08-29 14:03:34 -0700817 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700818 }
819
820 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
822 * cellX, cellY have already been updated on the ItemInfos.
823 */
824 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
825 final long container, final int screen) {
826
827 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
828 int count = items.size();
829
830 for (int i = 0; i < count; i++) {
831 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832 item.container = container;
833
834 // We store hotseat items in canonical form which is this orientation invariant position
835 // in the hotseat
836 if (context instanceof Launcher && screen < 0 &&
837 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 item.cellY);
840 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 }
843
844 final ContentValues values = new ContentValues();
845 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
846 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
847 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800848 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700850
851 contentValues.add(values);
852 }
853 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
854 }
855
856 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800858 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700861 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800862 item.cellX = cellX;
863 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 item.spanX = spanX;
865 item.spanY = spanY;
866
867 // We store hotseat items in canonical form which is this orientation invariant position
868 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700871 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700874 }
Adam Cohend4844c32011-02-18 19:25:06 -0800875
Adam Cohend4844c32011-02-18 19:25:06 -0800876 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800877 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
879 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800880 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700881 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
882 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700883 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800884
Michael Jurka816474f2012-06-25 14:49:02 -0700885 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700886 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700887
888 /**
889 * Update an item to the database in a specified container.
890 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700891 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700892 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100893 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700894 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800895 }
896
Sunny Goyal756a28a2015-04-23 17:07:55 -0700897 private void assertWorkspaceLoaded() {
898 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
899 throw new RuntimeException("Trying to add shortcut while loader is running");
900 }
901 }
902
Adam Cohend4844c32011-02-18 19:25:06 -0800903 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700904 * Returns true if the shortcuts already exists on the workspace. This must be called after
905 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700907 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
908 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700910 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 if (intent.getComponent() != null) {
912 // If component is not null, an intent with null package will produce
913 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700914 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700915 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 intentWithPkg = intent.toUri(0);
917 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700919 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
920 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700921 }
922 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700923 intentWithPkg = intent.toUri(0);
924 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700925 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927
928 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700929 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700930 if (item instanceof ShortcutInfo) {
931 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700932 if (info.getIntent() != null && info.user.equals(user)) {
933 String s = info.getIntent().toUri(0);
934 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
935 return true;
936 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700937 }
938 }
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700941 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700942 }
943
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 /**
945 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
946 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700947 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 final ContentResolver cr = context.getContentResolver();
949 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
950 "_id=? and (itemType=? or itemType=?)",
951 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700952 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700953
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 try {
955 if (c.moveToFirst()) {
956 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
957 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
958 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
959 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
960 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
961 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700962 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 FolderInfo folderInfo = null;
965 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700966 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
967 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700969 }
970
Sunny Goyala508e4f2015-05-21 09:33:57 -0700971 // Do not trim the folder label, as is was set by the user.
972 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 folderInfo.id = id;
974 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700976 folderInfo.cellX = c.getInt(cellXIndex);
977 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700978 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
980 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 } finally {
983 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700984 }
985
986 return null;
987 }
988
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 /**
990 * Add an item to the database in a specified container. Sets the container, screen, cellX and
991 * cellY fields of the item. Also assigns an ID to the item.
992 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700993 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700994 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 item.cellX = cellX;
997 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 // We store hotseat items in canonical form which is this orientation invariant position
999 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006
1007 final ContentValues values = new ContentValues();
1008 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001009 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
Michael Jurka414300a2013-08-27 15:42:35 +02001011 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001013
Jason Monk8e19cf22014-03-20 15:06:57 -04001014 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001015 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001017 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001020 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001021 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 sBgItemsIdMap.put(item.id, item);
1023 switch (item.itemType) {
1024 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1025 sBgFolders.put(item.id, (FolderInfo) item);
1026 // Fall through
1027 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1028 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1029 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1030 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1031 sBgWorkspaceItems.add(item);
1032 } else {
1033 if (!sBgFolders.containsKey(item.container)) {
1034 // Adding an item to a folder that doesn't exist.
1035 String msg = "adding item: " + item + " to a folder that " +
1036 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001037 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001039 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 break;
1041 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1042 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1043 break;
1044 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001045 }
1046 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001049 }
1050
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001052 * Creates a new unique child id, for a given cell span across all layouts.
1053 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001054 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001056 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001058 }
1059
Sunny Goyal34942622014-08-29 17:20:55 -07001060 private static ArrayList<ItemInfo> getItemsByPackageName(
1061 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001062 ItemInfoFilter filter = new ItemInfoFilter() {
1063 @Override
1064 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1065 return cn.getPackageName().equals(pn) && info.user.equals(user);
1066 }
1067 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001068 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001069 }
1070
1071 /**
1072 * Removes all the items from the database corresponding to the specified package.
1073 */
1074 static void deletePackageFromDatabase(Context context, final String pn,
1075 final UserHandleCompat user) {
1076 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 }
1078
1079 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001080 * Removes the specified item from the database
1081 * @param context
1082 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001084 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001085 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1086 items.add(item);
1087 deleteItemsFromDatabase(context, items);
1088 }
1089
1090 /**
1091 * Removes the specified items from the database
1092 * @param context
1093 * @param item
1094 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001095 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001097 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001098 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001099 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001100 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 // Lock on mBgLock *after* the db operation
1104 synchronized (sBgLock) {
1105 switch (item.itemType) {
1106 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1107 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001108 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001109 if (info.container == item.id) {
1110 // We are deleting a folder which still contains items that
1111 // think they are contained by that folder.
1112 String msg = "deleting a folder (" + item + ") which still " +
1113 "contains items (" + info + ")";
1114 Log.e(TAG, msg);
1115 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001117 sBgWorkspaceItems.remove(item);
1118 break;
1119 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1120 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1121 sBgWorkspaceItems.remove(item);
1122 break;
1123 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1124 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1125 break;
1126 }
1127 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001129 }
1130 }
Michael Jurka83df1882011-08-31 20:59:26 -07001131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 * Update the order of the workspace screens in the database. The array list contains
1137 * a list of screen ids in the order that they should appear.
1138 */
Winson Chungc9168342013-06-26 14:54:55 -07001139 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001140 // Log to disk
1141 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1142 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1143
Winson Chung64359a52013-07-08 17:17:08 -07001144 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 final ContentResolver cr = context.getContentResolver();
1146 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1147
1148 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001149 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 while (iter.hasNext()) {
1151 long id = iter.next();
1152 if (id < 0) {
1153 iter.remove();
1154 }
1155 }
1156
1157 Runnable r = new Runnable() {
1158 @Override
1159 public void run() {
Yura085c8532014-02-11 15:15:29 +00001160 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001162 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001163 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 for (int i = 0; i < count; i++) {
1165 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001166 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1168 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001169 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 }
Yura085c8532014-02-11 15:15:29 +00001171
1172 try {
1173 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1174 } catch (Exception ex) {
1175 throw new RuntimeException(ex);
1176 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001177
Winson Chungba9c37f2013-08-30 14:11:37 -07001178 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001179 sBgWorkspaceScreens.clear();
1180 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001181 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 }
1183 };
1184 runOnWorkerThread(r);
1185 }
1186
1187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 * Remove the contents of the specified folder from the database
1189 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001190 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 final ContentResolver cr = context.getContentResolver();
1192
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 Runnable r = new Runnable() {
1194 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001195 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001197 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 sBgItemsIdMap.remove(info.id);
1199 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 sBgWorkspaceItems.remove(info);
1201 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001202
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001203 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001204 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001206 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 for (ItemInfo childInfo : info.contents) {
1208 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001210 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001211 }
1212 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
1215
1216 /**
1217 * Set this as the current Launcher activity object for the loader.
1218 */
1219 public void initialize(Callbacks callbacks) {
1220 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001221 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1222 // workspace to prevent leaking Launcher activities on orientation change.
1223 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 mCallbacks = new WeakReference<Callbacks>(callbacks);
1225 }
1226 }
1227
Kenny Guyed131872014-04-30 03:02:21 +01001228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001230 int op = PackageUpdatedTask.OP_UPDATE;
1231 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1232 user));
1233 }
1234
1235 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001236 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001237 int op = PackageUpdatedTask.OP_REMOVE;
1238 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1239 user));
1240 }
1241
1242 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001243 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001244 int op = PackageUpdatedTask.OP_ADD;
1245 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1246 user));
1247 }
1248
1249 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001250 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001251 boolean replacing) {
1252 if (!replacing) {
1253 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1254 user));
1255 if (mAppsCanBeOnRemoveableStorage) {
1256 // Only rebind if we support removable storage. It catches the
1257 // case where
1258 // apps on the external sd card need to be reloaded
1259 startLoaderFromBackground();
1260 }
1261 } else {
1262 // If we are replacing then just update the packages in the list
1263 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1264 packageNames, user));
1265 }
1266 }
1267
1268 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001269 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001270 boolean replacing) {
1271 if (!replacing) {
1272 enqueuePackageUpdated(new PackageUpdatedTask(
1273 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1274 user));
1275 }
Kenny Guyed131872014-04-30 03:02:21 +01001276 }
1277
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001278 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1280 * ACTION_PACKAGE_CHANGED.
1281 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001282 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001283 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001284 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Joe Onorato36115782010-06-17 13:28:48 -04001286 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001287 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 // If we have changed locale we need to clear out the labels in all apps/workspace.
1289 forceReload();
1290 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1291 // Check if configuration change was an mcc/mnc change which would affect app resources
1292 // and we would need to clear out the labels in all apps/workspace. Same handling as
1293 // above for ACTION_LOCALE_CHANGED
1294 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001295 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001296 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001297 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001298 forceReload();
1299 }
1300 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001301 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001302 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1303 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001304 Callbacks callbacks = getCallback();
1305 if (callbacks != null) {
1306 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001307 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001308 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1309 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1310 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001311 }
1312 }
1313
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001314 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001315 resetLoadedState(true, true);
1316
Reena Lee93f824a2011-09-23 17:20:28 -07001317 // Do this here because if the launcher activity is running it will be restarted.
1318 // If it's not running startLoaderFromBackground will merely tell it that it needs
1319 // to reload.
1320 startLoaderFromBackground();
1321 }
1322
Winson Chungf0c6ae02012-03-21 16:10:31 -07001323 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1324 synchronized (mLock) {
1325 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1326 // mWorkspaceLoaded to true later
1327 stopLoaderLocked();
1328 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1329 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1330 }
1331 }
1332
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001333 /**
1334 * When the launcher is in the background, it's possible for it to miss paired
1335 * configuration changes. So whenever we trigger the loader from the background
1336 * tell the launcher that it needs to re-run the loader when it comes back instead
1337 * of doing it now.
1338 */
1339 public void startLoaderFromBackground() {
1340 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001341 Callbacks callbacks = getCallback();
1342 if (callbacks != null) {
1343 // Only actually run the loader if they're not paused.
1344 if (!callbacks.setLoadOnResume()) {
1345 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001346 }
1347 }
1348 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001350 }
Joe Onorato36115782010-06-17 13:28:48 -04001351 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001352
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001353 /**
1354 * If there is already a loader task running, tell it to stop.
1355 */
1356 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001357 LoaderTask oldTask = mLoaderTask;
1358 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001359 oldTask.stopLocked();
1360 }
Reena Lee93f824a2011-09-23 17:20:28 -07001361 }
1362
Adam Cohen1a85c582014-09-30 09:48:49 -07001363 public boolean isCurrentCallbacks(Callbacks callbacks) {
1364 return (mCallbacks != null && mCallbacks.get() == callbacks);
1365 }
1366
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001367 public void startLoader(int synchronousBindPage) {
1368 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001369 }
1370
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001371 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001372 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1373 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001374 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 // Clear any deferred bind-runnables from the synchronized load process
1376 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001377 synchronized (mDeferredBindRunnables) {
1378 mDeferredBindRunnables.clear();
1379 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 // Don't bother to start the thread if we know it's not going to do anything
1382 if (mCallbacks != null && mCallbacks.get() != null) {
1383 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001384 stopLoaderLocked();
1385 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001386 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001387 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1389 } else {
1390 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1391 sWorker.post(mLoaderTask);
1392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
1396
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 void bindRemainingSynchronousPages() {
1398 // Post the remaining side pages to be loaded
1399 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001400 Runnable[] deferredBindRunnables = null;
1401 synchronized (mDeferredBindRunnables) {
1402 deferredBindRunnables = mDeferredBindRunnables.toArray(
1403 new Runnable[mDeferredBindRunnables.size()]);
1404 mDeferredBindRunnables.clear();
1405 }
1406 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001407 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001410
1411 // Run all the bind complete runnables after workspace is bound.
1412 if (!mBindCompleteRunnables.isEmpty()) {
1413 synchronized (mBindCompleteRunnables) {
1414 for (final Runnable r : mBindCompleteRunnables) {
1415 runOnWorkerThread(r);
1416 }
1417 mBindCompleteRunnables.clear();
1418 }
1419 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 }
1421
Joe Onorato36115782010-06-17 13:28:48 -04001422 public void stopLoader() {
1423 synchronized (mLock) {
1424 if (mLoaderTask != null) {
1425 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 }
1427 }
Joe Onorato36115782010-06-17 13:28:48 -04001428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 /**
1431 * Loads the workspace screen ids in an ordered list.
1432 */
Adam Cohen091440a2015-03-18 14:16:05 -07001433 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001434 final ContentResolver contentResolver = context.getContentResolver();
1435 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001436
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001437 // Get screens ordered by rank.
1438 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1439 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1440 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001441 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001442 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001443 while (sc.moveToNext()) {
1444 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001445 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001446 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001447 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1448 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001449 }
1450 }
1451 } finally {
1452 sc.close();
1453 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001454 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001455 }
1456
Michael Jurkac57b7a82011-08-09 22:02:20 -07001457 public boolean isAllAppsLoaded() {
1458 return mAllAppsLoaded;
1459 }
1460
Winson Chung36a62fe2012-05-06 18:04:42 -07001461 boolean isLoadingWorkspace() {
1462 synchronized (mLock) {
1463 if (mLoaderTask != null) {
1464 return mLoaderTask.isLoadingWorkspace();
1465 }
1466 }
1467 return false;
1468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 /**
1471 * Runnable for the thread that loads the contents of the launcher:
1472 * - workspace icons
1473 * - widgets
1474 * - all apps icons
1475 */
1476 private class LoaderTask implements Runnable {
1477 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001478 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001479 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001480 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001481 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001483 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001484 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001485 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 }
1487
Winson Chung36a62fe2012-05-06 18:04:42 -07001488 boolean isLoadingWorkspace() {
1489 return mIsLoadingAndBindingWorkspace;
1490 }
1491
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001492 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001493 mIsLoadingAndBindingWorkspace = true;
1494
Joe Onorato36115782010-06-17 13:28:48 -04001495 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001496 if (DEBUG_LOADERS) {
1497 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001499
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001500 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001501 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001502 synchronized (LoaderTask.this) {
1503 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001504 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001505 }
1506 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 }
1508 }
1509
Joe Onorato36115782010-06-17 13:28:48 -04001510 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001511 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001513
Joe Onorato36115782010-06-17 13:28:48 -04001514 private void waitForIdle() {
1515 // Wait until the either we're stopped or the other threads are done.
1516 // This way we don't start loading all apps until the workspace has settled
1517 // down.
1518 synchronized (LoaderTask.this) {
1519 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001520
Joe Onorato36115782010-06-17 13:28:48 -04001521 mHandler.postIdle(new Runnable() {
1522 public void run() {
1523 synchronized (LoaderTask.this) {
1524 mLoadAndBindStepFinished = true;
1525 if (DEBUG_LOADERS) {
1526 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001527 }
Joe Onorato36115782010-06-17 13:28:48 -04001528 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001529 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 });
1532
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001533 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001534 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001535 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1536 // wait no longer than 1sec at a time
1537 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001538 } catch (InterruptedException ex) {
1539 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001540 }
1541 }
Joe Onorato36115782010-06-17 13:28:48 -04001542 if (DEBUG_LOADERS) {
1543 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001544 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001545 + "ms for previous step to finish binding");
1546 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001547 }
Joe Onorato36115782010-06-17 13:28:48 -04001548 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001551 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001552 // Ensure that we have a valid page index to load synchronously
1553 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1554 "valid page index");
1555 }
1556 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1557 // Ensure that we don't try and bind a specified page when the pages have not been
1558 // loaded already (we should load everything asynchronously in that case)
1559 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1560 }
1561 synchronized (mLock) {
1562 if (mIsLoaderTaskRunning) {
1563 // Ensure that we are never running the background loading at this point since
1564 // we also touch the background collections
1565 throw new RuntimeException("Error! Background loading is already running");
1566 }
1567 }
1568
1569 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1570 // data structures, we can't allow any other thread to touch that data, but because
1571 // this call is synchronous, we can get away with not locking).
1572
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001573 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001574 // operations from the previous activity. We need to ensure that all queued operations
1575 // are executed before any synchronous binding work is done.
1576 mHandler.flush();
1577
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 // Divide the set of loaded items into those that we are binding synchronously, and
1579 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001580 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1582 // arise from that.
1583 onlyBindAllApps();
1584 }
1585
Joe Onorato36115782010-06-17 13:28:48 -04001586 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001587 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001588 if (mStopped) {
1589 return;
1590 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001591 mIsLoaderTaskRunning = true;
1592 }
Joe Onorato36115782010-06-17 13:28:48 -04001593 // Optimize for end-user experience: if the Launcher is up and // running with the
1594 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1595 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001596 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001597 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001598 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001599
Joe Onorato36115782010-06-17 13:28:48 -04001600 if (mStopped) {
1601 break keep_running;
1602 }
1603
Joe Onorato36115782010-06-17 13:28:48 -04001604 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001605
1606 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001607 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1608 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001610
Joe Onorato36115782010-06-17 13:28:48 -04001611 // Clear out this reference, otherwise we end up holding it until all of the
1612 // callback runnables are done.
1613 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614
Joe Onorato36115782010-06-17 13:28:48 -04001615 synchronized (mLock) {
1616 // If we are still the last one to be scheduled, remove ourselves.
1617 if (mLoaderTask == this) {
1618 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001621 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
Joe Onorato36115782010-06-17 13:28:48 -04001623 }
1624
1625 public void stopLocked() {
1626 synchronized (LoaderTask.this) {
1627 mStopped = true;
1628 this.notify();
1629 }
1630 }
1631
1632 /**
1633 * Gets the callbacks object. If we've been stopped, or if the launcher object
1634 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1635 * object that was around when the deferred message was scheduled, and if there's
1636 * a new Callbacks object around then also return null. This will save us from
1637 * calling onto it with data that will be ignored.
1638 */
1639 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1640 synchronized (mLock) {
1641 if (mStopped) {
1642 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001643 }
Joe Onorato36115782010-06-17 13:28:48 -04001644
1645 if (mCallbacks == null) {
1646 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001647 }
Joe Onorato36115782010-06-17 13:28:48 -04001648
1649 final Callbacks callbacks = mCallbacks.get();
1650 if (callbacks != oldCallbacks) {
1651 return null;
1652 }
1653 if (callbacks == null) {
1654 Log.w(TAG, "no mCallbacks");
1655 return null;
1656 }
1657
1658 return callbacks;
1659 }
1660 }
1661
1662 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001663 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001664 LauncherAppState app = LauncherAppState.getInstance();
1665 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001666 final int countX = (int) grid.numColumns;
1667 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001668
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001670 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001671 // Return early if we detect that an item is under the hotseat button
1672 if (mCallbacks == null ||
1673 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001674 Log.e(TAG, "Error loading shortcut into hotseat " + item
1675 + " into position (" + item.screenId + ":" + item.cellX + ","
1676 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001677 return false;
1678 }
1679
Dan Sandler295ae182013-12-10 16:05:47 -05001680 final ItemInfo[][] hotseatItems =
1681 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1682
Adam Cohenae4409d2013-11-26 10:34:59 -08001683 if (item.screenId >= grid.numHotseatIcons) {
1684 Log.e(TAG, "Error loading shortcut " + item
1685 + " into hotseat position " + item.screenId
1686 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1687 + ")");
1688 return false;
1689 }
1690
Dan Sandler295ae182013-12-10 16:05:47 -05001691 if (hotseatItems != null) {
1692 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 Log.e(TAG, "Error loading shortcut into hotseat " + item
1694 + " into position (" + item.screenId + ":" + item.cellX + ","
1695 + item.cellY + ") occupied by "
1696 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1697 [(int) item.screenId][0]);
1698 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001699 } else {
1700 hotseatItems[(int) item.screenId][0] = item;
1701 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001702 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001703 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001704 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 items[(int) item.screenId][0] = item;
1706 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001707 return true;
1708 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001709 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1710 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001711 return true;
1712 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001713
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001715 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 occupied.put(item.screenId, items);
1717 }
1718
Dan Sandler295ae182013-12-10 16:05:47 -05001719 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001720 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1721 item.cellX < 0 || item.cellY < 0 ||
1722 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1723 Log.e(TAG, "Error loading shortcut " + item
1724 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1725 + item.cellX + "," + item.cellY
1726 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1727 return false;
1728 }
1729
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001730 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001731 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1732 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001734 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001735 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001736 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001737 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001738 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001739 return false;
1740 }
1741 }
1742 }
1743 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1744 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001745 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001746 }
1747 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001748
Joe Onorato36115782010-06-17 13:28:48 -04001749 return true;
1750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751
Winson Chungba9c37f2013-08-30 14:11:37 -07001752 /** Clears all the sBg data structures */
1753 private void clearSBgDataStructures() {
1754 synchronized (sBgLock) {
1755 sBgWorkspaceItems.clear();
1756 sBgAppWidgets.clear();
1757 sBgFolders.clear();
1758 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001759 sBgWorkspaceScreens.clear();
1760 }
1761 }
1762
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001763 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001764 // Log to disk
1765 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1766
Joe Onorato36115782010-06-17 13:28:48 -04001767 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768
Joe Onorato36115782010-06-17 13:28:48 -04001769 final Context context = mContext;
1770 final ContentResolver contentResolver = context.getContentResolver();
1771 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001772 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001773 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1774 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001775 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001776
Winson Chung892c74d2013-08-22 16:15:50 -07001777 LauncherAppState app = LauncherAppState.getInstance();
1778 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1779 int countX = (int) grid.numColumns;
1780 int countY = (int) grid.numRows;
1781
Dan Sandlerd5024042014-01-09 15:01:33 -05001782 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1783 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1784 LauncherAppState.getLauncherProvider().deleteDatabase();
1785 }
1786
1787 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1788 // append the user's Launcher2 shortcuts
1789 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1790 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1791 } else {
1792 // Make sure the default workspace is loaded
1793 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001794 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001795 }
Adam Cohene25af792013-06-06 23:08:25 -07001796
Winson Chung2abf94d2012-07-18 18:16:38 -07001797 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001798 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001799 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001800 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001801
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001802 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001803 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001804 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001805 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001806 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001807
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 // +1 for the hotseat (it can be larger than the workspace)
1809 // Load workspace in reverse order to ensure that latest items are loaded first (and
1810 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001811 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 try {
1814 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1815 final int intentIndex = c.getColumnIndexOrThrow
1816 (LauncherSettings.Favorites.INTENT);
1817 final int titleIndex = c.getColumnIndexOrThrow
1818 (LauncherSettings.Favorites.TITLE);
1819 final int iconTypeIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.ICON_TYPE);
1821 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1822 final int iconPackageIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.ICON_PACKAGE);
1824 final int iconResourceIndex = c.getColumnIndexOrThrow(
1825 LauncherSettings.Favorites.ICON_RESOURCE);
1826 final int containerIndex = c.getColumnIndexOrThrow(
1827 LauncherSettings.Favorites.CONTAINER);
1828 final int itemTypeIndex = c.getColumnIndexOrThrow(
1829 LauncherSettings.Favorites.ITEM_TYPE);
1830 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001832 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1833 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 final int screenIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.SCREEN);
1836 final int cellXIndex = c.getColumnIndexOrThrow
1837 (LauncherSettings.Favorites.CELLX);
1838 final int cellYIndex = c.getColumnIndexOrThrow
1839 (LauncherSettings.Favorites.CELLY);
1840 final int spanXIndex = c.getColumnIndexOrThrow
1841 (LauncherSettings.Favorites.SPANX);
1842 final int spanYIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001844 final int rankIndex = c.getColumnIndexOrThrow(
1845 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001846 final int restoredIndex = c.getColumnIndexOrThrow(
1847 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001848 final int profileIdIndex = c.getColumnIndexOrThrow(
1849 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001850 final int optionsIndex = c.getColumnIndexOrThrow(
1851 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852
Sunny Goyal7f834d22015-04-21 10:10:23 -07001853 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1854 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1855 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1856 }
1857
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 ShortcutInfo info;
1859 String intentDescription;
1860 LauncherAppWidgetInfo appWidgetInfo;
1861 int container;
1862 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001863 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001865 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 while (!mStopped && c.moveToNext()) {
1868 try {
1869 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001870 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001871 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 switch (itemType) {
1874 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1875 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001876 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001878 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001879 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001880 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001881 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001882 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001883 if (user == null) {
1884 // User has been deleted remove the item.
1885 itemsToRemove.add(id);
1886 continue;
1887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 try {
1889 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001890 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001891 if (cn != null && cn.getPackageName() != null) {
1892 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1893 cn.getPackageName(), user);
1894 boolean validComponent = validPkg &&
1895 launcherApps.isActivityEnabledForProfile(cn, user);
1896
1897 if (validComponent) {
1898 if (restored) {
1899 // no special handling necessary for this item
1900 restoredRows.add(id);
1901 restored = false;
1902 }
1903 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001904 intent = null;
1905 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1906 // We allow auto install apps to have their intent
1907 // updated after an install.
1908 intent = manager.getLaunchIntentForPackage(
1909 cn.getPackageName());
1910 if (intent != null) {
1911 ContentValues values = new ContentValues();
1912 values.put(LauncherSettings.Favorites.INTENT,
1913 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001914 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001915 }
1916 }
1917
1918 if (intent == null) {
1919 // The app is installed but the component is no
1920 // longer available.
1921 Launcher.addDumpLog(TAG,
1922 "Invalid component removed: " + cn, true);
1923 itemsToRemove.add(id);
1924 continue;
1925 } else {
1926 // no special handling necessary for this item
1927 restoredRows.add(id);
1928 restored = false;
1929 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001930 } else if (restored) {
1931 // Package is not yet available but might be
1932 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001933 Launcher.addDumpLog(TAG,
1934 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001935
1936 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1937 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001938 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001939 // App restore has started. Update the flag
1940 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1941 ContentValues values = new ContentValues();
1942 values.put(LauncherSettings.Favorites.RESTORED,
1943 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001944 updateItem(id, values);
1945 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1946 // This is a common app. Try to replace this.
1947 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1948 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1949 if (parser.findDefaultApp()) {
1950 // Default app found. Replace it.
1951 intent = parser.parsedIntent;
1952 cn = intent.getComponent();
1953 ContentValues values = parser.parsedValues;
1954 values.put(LauncherSettings.Favorites.RESTORED, 0);
1955 updateItem(id, values);
1956 restored = false;
1957 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001958
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001959 } else if (REMOVE_UNRESTORED_ICONS) {
1960 Launcher.addDumpLog(TAG,
1961 "Unrestored package removed: " + cn, true);
1962 itemsToRemove.add(id);
1963 continue;
1964 }
Sunny Goyal94485362014-09-18 16:13:58 -07001965 } else if (REMOVE_UNRESTORED_ICONS) {
1966 Launcher.addDumpLog(TAG,
1967 "Unrestored package removed: " + cn, true);
1968 itemsToRemove.add(id);
1969 continue;
1970 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001971 } else if (launcherApps.isAppEnabled(
1972 manager, cn.getPackageName(),
1973 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1974 // Package is present but not available.
1975 allowMissingTarget = true;
1976 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1977 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001978 // SdCard is not ready yet. Package might get available,
1979 // once it is ready.
1980 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1981 + " (check again later)", true);
1982 HashSet<String> pkgs = sPendingPackages.get(user);
1983 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001984 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001985 sPendingPackages.put(user, pkgs);
1986 }
1987 pkgs.add(cn.getPackageName());
1988 allowMissingTarget = true;
1989 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001990
1991 } else {
1992 // Do not wait for external media load anymore.
1993 // Log the invalid package, and remove it
1994 Launcher.addDumpLog(TAG,
1995 "Invalid package removed: " + cn, true);
1996 itemsToRemove.add(id);
1997 continue;
Winson Chungee055712013-07-30 14:46:24 -07001998 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001999 } else if (cn == null) {
2000 // For shortcuts with no component, keep them as they are
2001 restoredRows.add(id);
2002 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002003 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002005 Launcher.addDumpLog(TAG,
2006 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 continue;
2008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009
Sunny Goyal34b65272015-03-11 16:56:52 -07002010 container = c.getInt(containerIndex);
2011 boolean useLowResIcon = container >= 0 &&
2012 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2013
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002014 if (itemReplaced) {
2015 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002016 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002017 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002018 } else {
2019 // Don't replace items for other profiles.
2020 itemsToRemove.add(id);
2021 continue;
2022 }
2023 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002024 if (user.equals(UserHandleCompat.myUserHandle())) {
2025 Launcher.addDumpLog(TAG,
2026 "constructing info for partially restored package",
2027 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002028 info = getRestoredItemInfo(c, titleIndex, intent,
2029 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002030 intent = getRestoredItemIntent(c, context, intent);
2031 } else {
2032 // Don't restore items for other profiles.
2033 itemsToRemove.add(id);
2034 continue;
2035 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002036 } else if (itemType ==
2037 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002038 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002039 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 } else {
2041 info = getShortcutInfo(c, context, iconTypeIndex,
2042 iconPackageIndex, iconResourceIndex, iconIndex,
2043 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 // App shortcuts that used to be automatically added to Launcher
2046 // didn't always have the correct intent flags set, so do that
2047 // here
2048 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002049 intent.getCategories() != null &&
2050 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002051 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 intent.addFlags(
2053 Intent.FLAG_ACTIVITY_NEW_TASK |
2054 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2055 }
Michael Jurka96879562012-03-22 05:54:33 -07002056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002057
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002059 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002062 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 info.cellX = c.getInt(cellXIndex);
2064 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002065 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002066 info.spanX = 1;
2067 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002068 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002069 info.isDisabled = disabledState;
2070 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2071 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2072 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002073
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002075 if (!checkItemPlacement(occupied, info)) {
2076 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 break;
2078 }
2079
Sunny Goyal756adbc2015-04-16 15:20:51 -07002080 if (restored) {
2081 ComponentName cn = info.getTargetComponent();
2082 if (cn != null) {
2083 Integer progress = installingPkgs.get(cn.getPackageName());
2084 if (progress != null) {
2085 info.setInstallProgress(progress);
2086 } else {
2087 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2088 }
2089 }
2090 }
2091
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 switch (container) {
2093 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2094 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2095 sBgWorkspaceItems.add(info);
2096 break;
2097 default:
2098 // Item is in a user folder
2099 FolderInfo folderInfo =
2100 findOrMakeFolder(sBgFolders, container);
2101 folderInfo.add(info);
2102 break;
2103 }
2104 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002105 } else {
2106 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 }
2108 break;
2109
2110 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2111 id = c.getLong(idIndex);
2112 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2113
Sunny Goyala508e4f2015-05-21 09:33:57 -07002114 // Do not trim the folder label, as is was set by the user.
2115 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002119 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 folderInfo.cellX = c.getInt(cellXIndex);
2121 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002122 folderInfo.spanX = 1;
2123 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002124 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125
Daniel Sandler8802e962010-05-26 16:28:16 -04002126 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002127 if (!checkItemPlacement(occupied, folderInfo)) {
2128 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002129 break;
2130 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002131
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2134 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2135 sBgWorkspaceItems.add(folderInfo);
2136 break;
Joe Onorato36115782010-06-17 13:28:48 -04002137 }
Joe Onorato17a89222011-02-08 17:26:11 -08002138
Chris Wrenf4d08112014-01-16 18:13:56 -05002139 if (restored) {
2140 // no special handling required for restored folders
2141 restoredRows.add(id);
2142 }
2143
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2145 sBgFolders.put(folderInfo.id, folderInfo);
2146 break;
2147
2148 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002149 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002151 boolean customWidget = itemType ==
2152 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2153
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002155 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002156 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002157 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002158 user = allUsers.get(serialNumber);
2159 if (user == null) {
2160 itemsToRemove.add(id);
2161 continue;
2162 }
2163
Sunny Goyalff572272014-07-23 13:58:07 -07002164 final ComponentName component =
2165 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002166
Sunny Goyal651077b2014-06-30 14:15:31 -07002167 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002168 final boolean isIdValid = (restoreStatus &
2169 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002170 final boolean wasProviderReady = (restoreStatus &
2171 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002172
Adam Cohen59400422014-03-05 18:07:04 -08002173 final LauncherAppWidgetProviderInfo provider =
2174 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002175 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002176 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002177
2178 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002179 if (!isSafeMode && !customWidget &&
2180 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002182 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002183
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002185 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 itemsToRemove.add(id);
2187 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002188 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002189 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2190 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002191
2192 if (!customWidget) {
2193 int[] minSpan =
2194 Launcher.getMinSpanForWidget(context, provider);
2195 appWidgetInfo.minSpanX = minSpan[0];
2196 appWidgetInfo.minSpanY = minSpan[1];
2197 }
Sunny Goyalff572272014-07-23 13:58:07 -07002198
2199 int status = restoreStatus;
2200 if (!wasProviderReady) {
2201 // If provider was not previously ready, update the
2202 // status and UI flag.
2203
2204 // Id would be valid only if the widget restore broadcast was received.
2205 if (isIdValid) {
2206 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2207 } else {
2208 status &= ~LauncherAppWidgetInfo
2209 .FLAG_PROVIDER_NOT_READY;
2210 }
2211 }
2212 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002213 } else {
2214 Log.v(TAG, "Widget restore pending id=" + id
2215 + " appWidgetId=" + appWidgetId
2216 + " status =" + restoreStatus);
2217 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002218 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002219 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002220 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002221
2222 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2223 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002224 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002225 // App restore has started. Update the flag
2226 appWidgetInfo.restoreStatus |=
2227 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002228 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002229 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002230 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002231 itemsToRemove.add(id);
2232 continue;
2233 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002234
2235 appWidgetInfo.installProgress =
2236 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002237 }
Sunny Goyalff572272014-07-23 13:58:07 -07002238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 appWidgetInfo.cellX = c.getInt(cellXIndex);
2242 appWidgetInfo.cellY = c.getInt(cellYIndex);
2243 appWidgetInfo.spanX = c.getInt(spanXIndex);
2244 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002245
Adam Cohen59400422014-03-05 18:07:04 -08002246 if (!customWidget) {
2247 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2248 appWidgetInfo.minSpanX = minSpan[0];
2249 appWidgetInfo.minSpanY = minSpan[1];
2250 }
2251
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 container = c.getInt(containerIndex);
2253 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2254 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2255 Log.e(TAG, "Widget found where container != " +
2256 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2257 continue;
2258 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259
Adam Cohene25af792013-06-06 23:08:25 -07002260 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002262 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2263 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 break;
2265 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002266
Adam Cohen59400422014-03-05 18:07:04 -08002267 if (!customWidget) {
2268 String providerName =
2269 appWidgetInfo.providerName.flattenToString();
2270 if (!providerName.equals(savedProvider) ||
2271 (appWidgetInfo.restoreStatus != restoreStatus)) {
2272 ContentValues values = new ContentValues();
2273 values.put(
2274 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2275 providerName);
2276 values.put(LauncherSettings.Favorites.RESTORED,
2277 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002278 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002279 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002280 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2282 sBgAppWidgets.add(appWidgetInfo);
2283 }
Joe Onorato36115782010-06-17 13:28:48 -04002284 break;
2285 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002287 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 }
2290 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002291 if (c != null) {
2292 c.close();
2293 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 }
2295
Winson Chungba9c37f2013-08-30 14:11:37 -07002296 // Break early if we've stopped loading
2297 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002298 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002299 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002300 }
2301
Sunny Goyal1936ec02015-05-12 11:03:04 -07002302 // Remove any empty folder
2303 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2304 for (ItemInfo item: sBgItemsIdMap) {
2305 long container = item.container;
2306 if (emptyFolders.containsKey(container)) {
2307 emptyFolders.remove(container);
2308 }
2309 }
2310 for (FolderInfo folder : emptyFolders) {
2311 long folderId = folder.id;
2312 sBgFolders.remove(folderId);
2313 sBgItemsIdMap.remove(folderId);
2314 sBgWorkspaceItems.remove(folder);
2315 itemsToRemove.add(folderId);
2316 }
2317
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002318 if (itemsToRemove.size() > 0) {
2319 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002320 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 // Remove dead items
2322 for (long id : itemsToRemove) {
2323 if (DEBUG_LOADERS) {
2324 Log.d(TAG, "Removed id = " + id);
2325 }
2326 // Don't notify content observers
2327 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002328 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 } catch (RemoteException e) {
2330 Log.w(TAG, "Could not remove id = " + id);
2331 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002332 }
2333 }
2334
Chris Wrenf4d08112014-01-16 18:13:56 -05002335 if (restoredRows.size() > 0) {
2336 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002337 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002338 // Update restored items that no longer require special handling
2339 try {
2340 StringBuilder selectionBuilder = new StringBuilder();
2341 selectionBuilder.append(LauncherSettings.Favorites._ID);
2342 selectionBuilder.append(" IN (");
2343 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2344 selectionBuilder.append(")");
2345 ContentValues values = new ContentValues();
2346 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002347 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002348 values, selectionBuilder.toString(), null);
2349 } catch (RemoteException e) {
2350 Log.w(TAG, "Could not update restored rows");
2351 }
2352 }
2353
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002354 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2355 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002356 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002357 null, sWorker);
2358 }
2359
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002360 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2361 // Log to disk
2362 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2363 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002364
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002365 // Remove any empty screens
2366 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002367 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002368 long screenId = item.screenId;
2369 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2370 unusedScreens.contains(screenId)) {
2371 unusedScreens.remove(screenId);
2372 }
2373 }
2374
2375 // If there are any empty screens remove them, and update.
2376 if (unusedScreens.size() != 0) {
2377 // Log to disk
2378 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2379 TextUtils.join(", ", unusedScreens), true);
2380
2381 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002382 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002383 }
2384
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 if (DEBUG_LOADERS) {
2386 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2387 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002389 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002391
Sunny Goyale2df0622015-04-24 11:27:00 -07002392 for (int i = 0; i < nScreens; i++) {
2393 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002394 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395 line += " | ";
2396 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002397 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002398 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002399 if (x < screen.length && y < screen[x].length) {
2400 line += (screen[x][y] != null) ? "#" : ".";
2401 } else {
2402 line += "!";
2403 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 }
Joe Onorato36115782010-06-17 13:28:48 -04002405 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 }
Joe Onorato36115782010-06-17 13:28:48 -04002409 }
Adam Cohene25af792013-06-06 23:08:25 -07002410 }
2411
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002412 /**
2413 * Partially updates the item without any notification. Must be called on the worker thread.
2414 */
2415 private void updateItem(long itemId, ContentValues update) {
2416 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002417 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002418 update,
2419 BaseColumns._ID + "= ?",
2420 new String[]{Long.toString(itemId)});
2421 }
2422
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 /** Filters the set of items who are directly or indirectly (via another container) on the
2424 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002425 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 ArrayList<ItemInfo> allWorkspaceItems,
2427 ArrayList<ItemInfo> currentScreenItems,
2428 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002429 // Purge any null ItemInfos
2430 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2431 while (iter.hasNext()) {
2432 ItemInfo i = iter.next();
2433 if (i == null) {
2434 iter.remove();
2435 }
2436 }
2437
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 // Order the set of items by their containers first, this allows use to walk through the
2439 // list sequentially, build up a list of containers that are in the specified screen,
2440 // as well as all items in those containers.
2441 Set<Long> itemsOnScreen = new HashSet<Long>();
2442 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2443 @Override
2444 public int compare(ItemInfo lhs, ItemInfo rhs) {
2445 return (int) (lhs.container - rhs.container);
2446 }
2447 });
2448 for (ItemInfo info : allWorkspaceItems) {
2449 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002450 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 currentScreenItems.add(info);
2452 itemsOnScreen.add(info.id);
2453 } else {
2454 otherScreenItems.add(info);
2455 }
2456 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2457 currentScreenItems.add(info);
2458 itemsOnScreen.add(info.id);
2459 } else {
2460 if (itemsOnScreen.contains(info.container)) {
2461 currentScreenItems.add(info);
2462 itemsOnScreen.add(info.id);
2463 } else {
2464 otherScreenItems.add(info);
2465 }
2466 }
2467 }
2468 }
2469
2470 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002471 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 ArrayList<LauncherAppWidgetInfo> appWidgets,
2473 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2474 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475
2476 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002477 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002479 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 currentScreenWidgets.add(widget);
2481 } else {
2482 otherScreenWidgets.add(widget);
2483 }
2484 }
2485 }
2486
2487 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002488 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002489 LongArrayMap<ItemInfo> itemsIdMap,
2490 LongArrayMap<FolderInfo> folders,
2491 LongArrayMap<FolderInfo> currentScreenFolders,
2492 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493
Sunny Goyale2df0622015-04-24 11:27:00 -07002494 int total = folders.size();
2495 for (int i = 0; i < total; i++) {
2496 long id = folders.keyAt(i);
2497 FolderInfo folder = folders.valueAt(i);
2498
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002500 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002502 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002503 currentScreenFolders.put(id, folder);
2504 } else {
2505 otherScreenFolders.put(id, folder);
2506 }
2507 }
2508 }
2509
2510 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2511 * right) */
2512 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002513 final LauncherAppState app = LauncherAppState.getInstance();
2514 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 // XXX: review this
2516 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002517 @Override
2518 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002519 int cellCountX = (int) grid.numColumns;
2520 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002521 int screenOffset = cellCountX * cellCountY;
2522 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002523 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002524 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002525 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002526 rhs.cellY * cellCountX + rhs.cellX);
2527 return (int) (lr - rr);
2528 }
2529 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002531
Adam Cohendcd297f2013-06-18 13:13:40 -07002532 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2533 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002534 final Runnable r = new Runnable() {
2535 @Override
2536 public void run() {
2537 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2538 if (callbacks != null) {
2539 callbacks.bindScreens(orderedScreens);
2540 }
2541 }
2542 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002543 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002544 }
2545
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2547 final ArrayList<ItemInfo> workspaceItems,
2548 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002549 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002551
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 final boolean postOnMainThread = (deferredBindRunnables != null);
2553
2554 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002555 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002557 final int start = i;
2558 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 final Runnable r = new Runnable() {
2560 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002562 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002563 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002564 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2565 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002566 }
2567 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568 };
2569 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002570 synchronized (deferredBindRunnables) {
2571 deferredBindRunnables.add(r);
2572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002574 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 }
Joe Onorato36115782010-06-17 13:28:48 -04002576 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577
2578 // Bind the folders
2579 if (!folders.isEmpty()) {
2580 final Runnable r = new Runnable() {
2581 public void run() {
2582 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2583 if (callbacks != null) {
2584 callbacks.bindFolders(folders);
2585 }
2586 }
2587 };
2588 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002589 synchronized (deferredBindRunnables) {
2590 deferredBindRunnables.add(r);
2591 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002593 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 }
2595 }
2596
2597 // Bind the widgets, one at a time
2598 N = appWidgets.size();
2599 for (int i = 0; i < N; i++) {
2600 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2601 final Runnable r = new Runnable() {
2602 public void run() {
2603 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2604 if (callbacks != null) {
2605 callbacks.bindAppWidget(widget);
2606 }
2607 }
2608 };
2609 if (postOnMainThread) {
2610 deferredBindRunnables.add(r);
2611 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002612 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002613 }
2614 }
2615 }
2616
2617 /**
2618 * Binds all loaded data to actual views on the main thread.
2619 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002620 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621 final long t = SystemClock.uptimeMillis();
2622 Runnable r;
2623
2624 // Don't use these two variables in any of the callback runnables.
2625 // Otherwise we hold a reference to them.
2626 final Callbacks oldCallbacks = mCallbacks.get();
2627 if (oldCallbacks == null) {
2628 // This launcher has exited and nobody bothered to tell us. Just bail.
2629 Log.w(TAG, "LoaderTask running with no launcher");
2630 return;
2631 }
2632
Winson Chung9b9fb962013-11-15 15:39:34 -08002633 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002634 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2635 ArrayList<LauncherAppWidgetInfo> appWidgets =
2636 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002637 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002638
2639 final LongArrayMap<FolderInfo> folders;
2640 final LongArrayMap<ItemInfo> itemsIdMap;
2641
Winson Chung2abf94d2012-07-18 18:16:38 -07002642 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 workspaceItems.addAll(sBgWorkspaceItems);
2644 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002645 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002646
2647 folders = sBgFolders.clone();
2648 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002649 }
2650
Derek Prothro7aff3992013-12-10 14:00:37 -05002651 final boolean isLoadingSynchronously =
2652 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002653 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002654 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002655 if (currScreen >= orderedScreenIds.size()) {
2656 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002657 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002658 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002659 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002660 final long currentScreenId = currentScreen < 0
2661 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002662
2663 // Load all the items that are on the current page first (and in the process, unbind
2664 // all the existing workspace items before we call startBinding() below.
2665 unbindWorkspaceItemsOnMainThread();
2666
2667 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2669 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2670 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2671 new ArrayList<LauncherAppWidgetInfo>();
2672 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2673 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002674 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2675 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002676
Winson Chung9b9fb962013-11-15 15:39:34 -08002677 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002679 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002681 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002682 otherFolders);
2683 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2684 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2685
2686 // Tell the workspace that we're about to start binding items
2687 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002688 public void run() {
2689 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2690 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002691 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002692 }
2693 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002695 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002696
Adam Cohendcd297f2013-06-18 13:13:40 -07002697 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2698
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002699 // Load items on the current page
2700 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2701 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002702 if (isLoadingSynchronously) {
2703 r = new Runnable() {
2704 public void run() {
2705 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002706 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002707 callbacks.onPageBoundSynchronously(currentScreen);
2708 }
2709 }
2710 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002711 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002712 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002713
Winson Chung4a2afa32012-07-19 14:53:05 -07002714 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2715 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002716 synchronized (mDeferredBindRunnables) {
2717 mDeferredBindRunnables.clear();
2718 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002719 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002720 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002721
2722 // Tell the workspace that we're done binding items
2723 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002724 public void run() {
2725 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2726 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002727 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002728 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002729
Winson Chung98e030b2012-05-07 16:01:11 -07002730 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002731 if (DEBUG_LOADERS) {
2732 Log.d(TAG, "bound workspace in "
2733 + (SystemClock.uptimeMillis()-t) + "ms");
2734 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002735
2736 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002737 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002738 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002739 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002740 synchronized (mDeferredBindRunnables) {
2741 mDeferredBindRunnables.add(r);
2742 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002743 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002744 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002745 }
Joe Onorato36115782010-06-17 13:28:48 -04002746 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002747
Joe Onorato36115782010-06-17 13:28:48 -04002748 private void loadAndBindAllApps() {
2749 if (DEBUG_LOADERS) {
2750 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2751 }
2752 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002753 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002754 synchronized (LoaderTask.this) {
2755 if (mStopped) {
2756 return;
2757 }
2758 }
Sunny Goyal77919b92015-05-06 16:53:21 -07002759 updateAllAppsIconsCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002760 synchronized (LoaderTask.this) {
2761 if (mStopped) {
2762 return;
2763 }
2764 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002765 }
Joe Onorato36115782010-06-17 13:28:48 -04002766 } else {
2767 onlyBindAllApps();
2768 }
2769 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002770
Joe Onorato36115782010-06-17 13:28:48 -04002771 private void onlyBindAllApps() {
2772 final Callbacks oldCallbacks = mCallbacks.get();
2773 if (oldCallbacks == null) {
2774 // This launcher has exited and nobody bothered to tell us. Just bail.
2775 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2776 return;
2777 }
2778
2779 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002780 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002781 final ArrayList<AppInfo> list
2782 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002783 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002784 public void run() {
2785 final long t = SystemClock.uptimeMillis();
2786 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2787 if (callbacks != null) {
2788 callbacks.bindAllApplications(list);
2789 }
2790 if (DEBUG_LOADERS) {
2791 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2792 + (SystemClock.uptimeMillis()-t) + "ms");
2793 }
2794 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002795 };
2796 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002797 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002798 r.run();
2799 } else {
2800 mHandler.post(r);
2801 }
Hyunyoung Song219d0482015-05-07 12:51:42 -07002802 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2803 false /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
2805
Winson Chung64359a52013-07-08 17:17:08 -07002806 private void loadAllApps() {
2807 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002808
Joe Onorato36115782010-06-17 13:28:48 -04002809 final Callbacks oldCallbacks = mCallbacks.get();
2810 if (oldCallbacks == null) {
2811 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002812 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002813 return;
2814 }
2815
Kenny Guyed131872014-04-30 03:02:21 +01002816 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2817
Winson Chung64359a52013-07-08 17:17:08 -07002818 // Clear the list of apps
2819 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002820 for (UserHandleCompat user : profiles) {
2821 // Query for the set of apps
2822 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002823 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002824 if (DEBUG_LOADERS) {
2825 Log.d(TAG, "getActivityList took "
2826 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2827 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2828 }
2829 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002830 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002831 if (apps == null || apps.isEmpty()) {
2832 return;
2833 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002834
Kenny Guyed131872014-04-30 03:02:21 +01002835 // Create the ApplicationInfos
2836 for (int i = 0; i < apps.size(); i++) {
2837 LauncherActivityInfoCompat app = apps.get(i);
2838 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002839 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002840 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002841
Sunny Goyal756a28a2015-04-23 17:07:55 -07002842 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2843 if (heuristic != null) {
2844 runAfterBindCompletes(new Runnable() {
2845
2846 @Override
2847 public void run() {
2848 heuristic.processUserApps(apps);
2849 }
2850 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002851 }
Winson Chung64359a52013-07-08 17:17:08 -07002852 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002853 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002854 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2855 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002856
2857 // Post callback on main thread
2858 mHandler.post(new Runnable() {
2859 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002860
Winson Chung64359a52013-07-08 17:17:08 -07002861 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002862 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002863 if (callbacks != null) {
2864 callbacks.bindAllApplications(added);
2865 if (DEBUG_LOADERS) {
2866 Log.d(TAG, "bound " + added.size() + " apps in "
2867 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2868 }
2869 } else {
2870 Log.i(TAG, "not binding apps: no Launcher activity");
2871 }
2872 }
2873 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002874 // Cleanup any data stored for a deleted user.
2875 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002876
Hyunyoung Song219d0482015-05-07 12:51:42 -07002877 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2878 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002879 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002880 Log.d(TAG, "Icons processed in "
2881 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002882 }
2883 }
2884
Sunny Goyal77919b92015-05-06 16:53:21 -07002885 private void updateAllAppsIconsCache() {
2886 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2887
2888 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2889 // Query for the set of apps
2890 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2891 // Fail if we don't have any apps
2892 // TODO: Fix this. Only fail for the current user.
2893 if (apps == null || apps.isEmpty()) {
2894 return;
2895 }
2896
2897 // Update icon cache
2898 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2899
2900 // If any package icon has changed (app was updated while launcher was dead),
2901 // update the corresponding shortcuts.
2902 if (!updatedPackages.isEmpty()) {
2903 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2904 synchronized (sBgLock) {
2905 for (ItemInfo info : sBgItemsIdMap) {
2906 if (info instanceof ShortcutInfo && user.equals(info.user)
2907 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2908 ShortcutInfo si = (ShortcutInfo) info;
2909 ComponentName cn = si.getTargetComponent();
2910 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2911 si.updateIcon(mIconCache);
2912 updatedShortcuts.add(si);
2913 }
2914 }
2915 }
2916 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2917 }
2918
2919 if (!updatedShortcuts.isEmpty()) {
2920 final UserHandleCompat userFinal = user;
2921 mHandler.post(new Runnable() {
2922
2923 public void run() {
2924 Callbacks cb = getCallback();
2925 if (cb != null) {
2926 cb.bindShortcutsChanged(updatedShortcuts,
2927 new ArrayList<ShortcutInfo>(), userFinal);
2928 }
2929 }
2930 });
2931 }
2932 }
2933 }
2934 if (!updatedApps.isEmpty()) {
2935 mHandler.post(new Runnable() {
2936
2937 public void run() {
2938 Callbacks cb = getCallback();
2939 if (cb != null) {
2940 cb.bindAppsUpdated(updatedApps);
2941 }
2942 }
2943 });
2944 }
2945 }
2946
Joe Onoratobe386092009-11-17 17:32:16 -08002947 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002948 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002949 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002950 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2951 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2952 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2953 }
Joe Onorato36115782010-06-17 13:28:48 -04002954 }
2955 }
2956
2957 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002958 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002959 }
2960
Adam Cohen091440a2015-03-18 14:16:05 -07002961 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002962
2963 @Override
2964 public void onReceive(Context context, Intent intent) {
2965 synchronized (sBgLock) {
2966 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2967 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002968 final PackageManager manager = context.getPackageManager();
2969 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2970 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002971 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2972 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002973 packagesRemoved.clear();
2974 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002975 for (String pkg : entry.getValue()) {
2976 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002977 boolean packageOnSdcard = launcherApps.isAppEnabled(
2978 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2979 if (packageOnSdcard) {
2980 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2981 packagesUnavailable.add(pkg);
2982 } else {
2983 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2984 packagesRemoved.add(pkg);
2985 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002986 }
2987 }
2988 if (!packagesRemoved.isEmpty()) {
2989 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2990 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2991 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002992 if (!packagesUnavailable.isEmpty()) {
2993 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2994 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2995 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002996 }
Sunny Goyal34942622014-08-29 17:20:55 -07002997 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002998 }
2999 }
3000 }
3001
Joe Onorato36115782010-06-17 13:28:48 -04003002 private class PackageUpdatedTask implements Runnable {
3003 int mOp;
3004 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003005 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003006
3007 public static final int OP_NONE = 0;
3008 public static final int OP_ADD = 1;
3009 public static final int OP_UPDATE = 2;
3010 public static final int OP_REMOVE = 3; // uninstlled
3011 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3012
3013
Kenny Guyed131872014-04-30 03:02:21 +01003014 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003015 mOp = op;
3016 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003017 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003018 }
3019
3020 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003021 if (!mHasLoaderCompletedOnce) {
3022 // Loader has not yet run.
3023 return;
3024 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003025 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003026
3027 final String[] packages = mPackages;
3028 final int N = packages.length;
3029 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003031 for (int i=0; i<N; i++) {
3032 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003033 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003034 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003035 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003036
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003037 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3038 if (heuristic != null) {
3039 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003040 }
Joe Onorato36115782010-06-17 13:28:48 -04003041 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003042 }
Joe Onorato36115782010-06-17 13:28:48 -04003043 case OP_UPDATE:
3044 for (int i=0; i<N; i++) {
3045 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003046 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003047 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003048 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003049 }
3050 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003051 case OP_REMOVE: {
3052 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3053 if (heuristic != null) {
3054 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003055 }
Joe Onorato36115782010-06-17 13:28:48 -04003056 for (int i=0; i<N; i++) {
3057 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003058 mIconCache.removeIconsForPkg(packages[i], mUser);
3059 }
3060 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003061 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003062 case OP_UNAVAILABLE:
3063 for (int i=0; i<N; i++) {
3064 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3065 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003066 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003067 }
3068 break;
3069 }
3070
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003071 ArrayList<AppInfo> added = null;
3072 ArrayList<AppInfo> modified = null;
3073 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003074
Adam Cohen487f7dd2012-06-28 18:12:10 -07003075 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003076 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003077 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003078 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003079 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003080 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003081 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003082 }
Winson Chung5d55f332012-07-16 20:45:03 -07003083 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003084 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003085 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003086 }
3087
Sunny Goyale0f58d72014-11-10 18:05:31 -08003088 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003089 if (callbacks == null) {
3090 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3091 return;
3092 }
3093
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3095 new HashMap<ComponentName, AppInfo>();
3096
Joe Onorato36115782010-06-17 13:28:48 -04003097 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003098 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 for (AppInfo ai : added) {
3100 addedOrUpdatedApps.put(ai.componentName, ai);
3101 }
Joe Onorato36115782010-06-17 13:28:48 -04003102 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003103
Joe Onorato36115782010-06-17 13:28:48 -04003104 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003105 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003106 for (AppInfo ai : modified) {
3107 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003108 }
3109
Joe Onorato36115782010-06-17 13:28:48 -04003110 mHandler.post(new Runnable() {
3111 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003112 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003113 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003114 callbacks.bindAppsUpdated(modifiedFinal);
3115 }
3116 }
3117 });
3118 }
Winson Chung83892cc2013-05-01 16:53:33 -07003119
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003121 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3123 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3124 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3125
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003126 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003127 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003128 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3130 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 boolean infoUpdated = false;
3132 boolean shortcutUpdated = false;
3133
3134 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003135 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003137 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3138 si.iconResource.resourceName, mIconCache, context);
3139 if (icon != null) {
3140 si.setIcon(icon);
3141 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003143 }
3144 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145
3146 ComponentName cn = si.getTargetComponent();
3147 if (cn != null && packageSet.contains(cn.getPackageName())) {
3148 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3149
3150 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3152 // Auto install icon
3153 PackageManager pm = context.getPackageManager();
3154 ResolveInfo matched = pm.resolveActivity(
3155 new Intent(Intent.ACTION_MAIN)
3156 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3157 PackageManager.MATCH_DEFAULT_ONLY);
3158 if (matched == null) {
3159 // Try to find the best match activity.
3160 Intent intent = pm.getLaunchIntentForPackage(
3161 cn.getPackageName());
3162 if (intent != null) {
3163 cn = intent.getComponent();
3164 appInfo = addedOrUpdatedApps.get(cn);
3165 }
3166
3167 if ((intent == null) || (appInfo == null)) {
3168 removedShortcuts.add(si);
3169 continue;
3170 }
3171 si.promisedIntent = intent;
3172 }
3173 }
3174
3175 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003176 if (appInfo != null) {
3177 si.flags = appInfo.flags;
3178 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179
Sunny Goyal756adbc2015-04-16 15:20:51 -07003180 si.intent = si.promisedIntent;
3181 si.promisedIntent = null;
3182 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003183 infoUpdated = true;
3184 si.updateIcon(mIconCache);
3185 }
3186
3187 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3188 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3189 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003190 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003191 si.contentDescription = appInfo.contentDescription;
3192 infoUpdated = true;
3193 }
3194
3195 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3196 // Since package was just updated, the target must be available now.
3197 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3198 shortcutUpdated = true;
3199 }
3200 }
3201
3202 if (infoUpdated || shortcutUpdated) {
3203 updatedShortcuts.add(si);
3204 }
3205 if (infoUpdated) {
3206 updateItemInDatabase(context, si);
3207 }
3208 } else if (info instanceof LauncherAppWidgetInfo) {
3209 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3210 if (mUser.equals(widgetInfo.user)
3211 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3212 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3213 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3214 widgets.add(widgetInfo);
3215 updateItemInDatabase(context, widgetInfo);
3216 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003217 }
3218 }
3219 }
3220
Sunny Goyal4390ace2014-10-13 11:33:11 -07003221 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3222 mHandler.post(new Runnable() {
3223
3224 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003225 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003226 if (callbacks == cb && cb != null) {
3227 callbacks.bindShortcutsChanged(
3228 updatedShortcuts, removedShortcuts, mUser);
3229 }
3230 }
3231 });
3232 if (!removedShortcuts.isEmpty()) {
3233 deleteItemsFromDatabase(context, removedShortcuts);
3234 }
3235 }
3236 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003237 mHandler.post(new Runnable() {
3238 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003239 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003240 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003241 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003242 }
3243 }
3244 });
3245 }
3246 }
3247
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 final ArrayList<String> removedPackageNames =
3249 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003250 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 // Mark all packages in the broadcast to be removed
3252 removedPackageNames.addAll(Arrays.asList(packages));
3253 } else if (mOp == OP_UPDATE) {
3254 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003255 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003256 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003258 }
3259 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003260 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003261
Winson Chungdf95eb12013-10-16 14:57:07 -07003262 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003263 final int removeReason;
3264 if (mOp == OP_UNAVAILABLE) {
3265 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3266 } else {
3267 // Remove all the components associated with this package
3268 for (String pn : removedPackageNames) {
3269 deletePackageFromDatabase(context, pn, mUser);
3270 }
3271 // Remove all the specific components
3272 for (AppInfo a : removedApps) {
3273 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3274 deleteItemsFromDatabase(context, infos);
3275 }
3276 removeReason = 0;
3277 }
3278
Winson Chungdf95eb12013-10-16 14:57:07 -07003279 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003280 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003281 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003282 mHandler.post(new Runnable() {
3283 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003284 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003285 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003286 callbacks.bindComponentsRemoved(
3287 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003288 }
3289 }
3290 });
Joe Onoratobe386092009-11-17 17:32:16 -08003291 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003292
3293 // onProvidersChanged method (API >= 17) already refreshed the widget list
3294 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3295
Adam Cohen4caf2982013-08-20 18:54:31 -07003296 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003297 mHandler.post(new Runnable() {
3298 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003299 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003300 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003301 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003302 }
3303 }
3304 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 }
3306 }
3307
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003308 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3309 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003310 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003311 if (sBgWidgetProviders == null || refresh) {
3312 sBgWidgetProviders = new HashMap<>();
3313 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3314 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003315
Robin Lee26ace122015-03-16 19:41:43 +00003316 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3317 for (AppWidgetProviderInfo pInfo : widgets) {
3318 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3319 UserHandleCompat user = wm.getUser(info);
3320 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3321 }
3322
3323 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3324 for (CustomAppWidget widget : customWidgets) {
3325 info = new LauncherAppWidgetProviderInfo(context, widget);
3326 UserHandleCompat user = wm.getUser(info);
3327 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3328 }
Adam Cohen59400422014-03-05 18:07:04 -08003329 }
Adam Cohen59400422014-03-05 18:07:04 -08003330 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3331 }
3332 }
3333
Robin Lee26ace122015-03-16 19:41:43 +00003334 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3335 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003336 synchronized (sBgLock) {
3337 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003338 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003339 }
Robin Lee26ace122015-03-16 19:41:43 +00003340 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003341 }
3342 }
3343
Hyunyoung Song227239e2015-05-04 18:17:35 -07003344 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3345 final boolean refresh) {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003346 runOnWorkerThread(new Runnable(){
3347 @Override
3348 public void run() {
Hyunyoung Song227239e2015-05-04 18:17:35 -07003349 final ArrayList<Object> list = getWidgetsAndShortcuts(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003350 mHandler.post(new Runnable() {
3351 @Override
3352 public void run() {
3353 Callbacks cb = getCallback();
3354 if (callbacks == cb && cb != null) {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003355 callbacks.bindAllPackages(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003356 }
3357 }
3358 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003359 // update the Widget entries inside DB on the worker thread.
3360 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003361 }
3362 });
3363 }
3364
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003365 /**
3366 * Returns a list of ResolveInfos/AppWidgetInfos.
3367 *
3368 * @see #loadAndBindWidgetsAndShortcuts
3369 */
3370 private ArrayList<Object> getWidgetsAndShortcuts(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));
Michael Jurkac402cd92013-05-20 15:49:32 +02003376 return widgetsAndShortcuts;
3377 }
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 Goyal34942622014-08-29 17:20:55 -07003410 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003411 int promiseType, boolean useLowResIcon) {
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 Goyal34b65272015-03-11 16:56:52 -07003414 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003415
3416 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3417 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3418 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003419 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003420 }
Sunny Goyal34942622014-08-29 17:20:55 -07003421 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3422 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003423 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003424 }
Sunny Goyal34942622014-08-29 17:20:55 -07003425 } else {
3426 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3427 }
3428
Winson Chung82b016c2015-05-08 17:00:10 -07003429 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003430 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003431 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003432 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003433 return info;
3434 }
3435
3436 /**
3437 * Make an Intent object for a restored application or shortcut item that points
3438 * to the market page for the item.
3439 */
Adam Cohen091440a2015-03-18 14:16:05 -07003440 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003441 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003442 return getMarketIntent(componentName.getPackageName());
3443 }
3444
3445 static Intent getMarketIntent(String packageName) {
3446 return new Intent(Intent.ACTION_VIEW)
3447 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003448 .scheme("market")
3449 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003450 .appendQueryParameter("id", packageName)
3451 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003452 }
3453
3454 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003455 * Make an ShortcutInfo object for a shortcut that is an application.
3456 *
3457 * If c is not null, then it will be used to fill in missing data like the title and icon.
3458 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003459 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003460 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003461 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003462 if (user == null) {
3463 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003464 return null;
3465 }
3466
Kenny Guyed131872014-04-30 03:02:21 +01003467 ComponentName componentName = intent.getComponent();
3468 if (componentName == null) {
3469 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3470 return null;
3471 }
3472
3473 Intent newIntent = new Intent(intent.getAction(), null);
3474 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3475 newIntent.setComponent(componentName);
3476 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003477 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003478 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3479 return null;
3480 }
3481
3482 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003483 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003484 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3485 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3486 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003487 }
3488
Joe Onorato56d82912010-03-07 14:32:10 -05003489 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003490 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003491 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003492 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003493
Joe Onorato56d82912010-03-07 14:32:10 -05003494 // fall back to the class name of the activity
3495 if (info.title == null) {
3496 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003497 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003498
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003500 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003501 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003502 if (lai != null) {
3503 info.flags = AppInfo.initFlags(lai);
3504 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003506 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003507
Sunny Goyale2df0622015-04-24 11:27:00 -07003508 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003509 ItemInfoFilter f) {
3510 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3511 for (ItemInfo i : infos) {
3512 if (i instanceof ShortcutInfo) {
3513 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003514 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003515 if (cn != null && f.filterItem(null, info, cn)) {
3516 filtered.add(info);
3517 }
3518 } else if (i instanceof FolderInfo) {
3519 FolderInfo info = (FolderInfo) i;
3520 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003521 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003522 if (cn != null && f.filterItem(info, s, cn)) {
3523 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003524 }
3525 }
Winson Chung64359a52013-07-08 17:17:08 -07003526 } else if (i instanceof LauncherAppWidgetInfo) {
3527 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3528 ComponentName cn = info.providerName;
3529 if (cn != null && f.filterItem(null, info, cn)) {
3530 filtered.add(info);
3531 }
Winson Chung8a435102012-08-30 17:16:53 -07003532 }
3533 }
Winson Chung64359a52013-07-08 17:17:08 -07003534 return new ArrayList<ItemInfo>(filtered);
3535 }
3536
Adam Cohen091440a2015-03-18 14:16:05 -07003537 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003538 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003539 ItemInfoFilter filter = new ItemInfoFilter() {
3540 @Override
3541 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003542 if (info.user == null) {
3543 return cn.equals(cname);
3544 } else {
3545 return cn.equals(cname) && info.user.equals(user);
3546 }
Winson Chung64359a52013-07-08 17:17:08 -07003547 }
3548 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003549 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003550 }
3551
Sunny Goyal1a745e82014-10-02 15:58:31 -07003552 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003553 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 */
Adam Cohen091440a2015-03-18 14:16:05 -07003555 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003556 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3557 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558
Joe Onorato56d82912010-03-07 14:32:10 -05003559 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003560 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003561 // Non-app shortcuts are only supported for current user.
3562 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003565 // TODO: If there's an explicit component and we can't install that, delete it.
3566
Winson Chung82b016c2015-05-08 17:00:10 -07003567 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003568
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 int iconType = c.getInt(iconTypeIndex);
3570 switch (iconType) {
3571 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3572 String packageName = c.getString(iconPackageIndex);
3573 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003574 info.customIcon = false;
3575 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003576 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003577 // the db
3578 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003579 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003580 }
3581 // the fallback icon
3582 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003583 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003584 info.usingFallbackIcon = true;
3585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 break;
3587 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003588 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003589 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003590 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003591 info.customIcon = false;
3592 info.usingFallbackIcon = true;
3593 } else {
3594 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 break;
3597 default:
Kenny Guyed131872014-04-30 03:02:21 +01003598 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003599 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 info.customIcon = false;
3601 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003602 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003603 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 return info;
3605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003606
Sunny Goyal2350bc92014-10-14 16:42:54 -07003607 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003608 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3609 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3610 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3611
Adam Cohend9198822011-11-22 16:42:47 -08003612 if (intent == null) {
3613 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3614 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3615 return null;
3616 }
3617
Joe Onorato0589f0f2010-02-08 13:44:00 -08003618 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003619 boolean customIcon = false;
3620 ShortcutIconResource iconResource = null;
3621
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003622 if (bitmap instanceof Bitmap) {
3623 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003624 customIcon = true;
3625 } else {
3626 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003627 if (extra instanceof ShortcutIconResource) {
3628 iconResource = (ShortcutIconResource) extra;
3629 icon = Utilities.createIconBitmap(iconResource.packageName,
3630 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003631 }
3632 }
3633
Michael Jurkac9d95c52011-08-29 14:03:34 -07003634 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003635
Kenny Guyed131872014-04-30 03:02:21 +01003636 // Only support intents for current user for now. Intents sent from other
3637 // users wouldn't get here without intent forwarding anyway.
3638 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003639 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003640 icon = mIconCache.getDefaultIcon(info.user);
3641 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003642 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003643 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003644
Winson Chung82b016c2015-05-08 17:00:10 -07003645 info.title = Utilities.trim(name);
3646 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003647 info.intent = intent;
3648 info.customIcon = customIcon;
3649 info.iconResource = iconResource;
3650
3651 return info;
3652 }
3653
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003655 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 * or make a new one.
3657 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003658 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 // See if a placeholder was created for us already
3660 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003661 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003663 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 folders.put(id, folderInfo);
3665 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003666 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 }
3668
Joe Onoratobe386092009-11-17 17:32:16 -08003669
Sunny Goyal651077b2014-06-30 14:15:31 -07003670 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3671 return (provider != null) && (provider.provider != null)
3672 && (provider.provider.getPackageName() != null);
3673 }
3674
Joe Onoratobe386092009-11-17 17:32:16 -08003675 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003676 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003677 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3678 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3679 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3680 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003681 if (mLoaderTask != null) {
3682 mLoaderTask.dumpState();
3683 } else {
3684 Log.d(TAG, "mLoaderTask=null");
3685 }
Joe Onoratobe386092009-11-17 17:32:16 -08003686 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003687
3688 public Callbacks getCallback() {
3689 return mCallbacks != null ? mCallbacks.get() : null;
3690 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003691
3692 /**
3693 * @return {@link FolderInfo} if its already loaded.
3694 */
3695 public FolderInfo findFolderById(Long folderId) {
3696 synchronized (sBgLock) {
3697 return sBgFolders.get(folderId);
3698 }
3699 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003700
3701 /**
3702 * @return the looper for the worker thread which can be used to start background tasks.
3703 */
3704 public static Looper getWorkerLooper() {
3705 return sWorkerThread.getLooper();
3706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003707}