blob: 497213895498455f98273ae548e4a5598b661d25 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070036import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070058import com.android.launcher3.config.ProviderConfig;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070059import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000060import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070061import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070062import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080065
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.lang.ref.WeakReference;
67import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070068import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070070import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070071import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.Collections;
73import java.util.Comparator;
74import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070075import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070076import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070078import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070079import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081/**
82 * Maintains in-memory state of the Launcher. It is expected that there should be only one
83 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070084 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 */
Kenny Guyed131872014-04-30 03:02:21 +010086public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010087 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080088 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040089 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070090 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040091
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070093
Dan Sandlerd5024042014-01-09 15:01:33 -050094 public static final int LOADER_FLAG_NONE = 0;
95 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
96 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
97
Joe Onorato36115782010-06-17 13:28:48 -040098 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050099 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800100
Adam Cohen091440a2015-03-18 14:16:05 -0700101 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800102 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final LauncherAppState mApp;
105 @Thunk final Object mLock = new Object();
106 @Thunk DeferredHandler mHandler = new DeferredHandler();
107 @Thunk LoaderTask mLoaderTask;
108 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700109 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
142 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700152 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700164 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 // sBgWorkspaceScreens is the ordered set of workspace screens.
167 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
168
Adam Cohen59400422014-03-05 18:07:04 -0800169 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000170 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800171
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700172 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700173 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
174 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700175
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176 // </ only access in worker thread >
177
Adam Cohen091440a2015-03-18 14:16:05 -0700178 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk final LauncherAppsCompat mLauncherApps;
181 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700184 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public int getCurrentWorkspaceScreen();
186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700191 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800192 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700195 public void bindAppsAdded(ArrayList<Long> newScreens,
196 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700197 ArrayList<ItemInfo> addAnimated,
198 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
201 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700204 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700205 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700206 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100207 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700208 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700210 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chung64359a52013-07-08 17:17:08 -0700213 public interface ItemInfoFilter {
214 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
215 }
216
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800218 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219
Winson Chungee055712013-07-30 14:46:24 -0700220 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700221 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400222 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
223 // resource string.
224 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
225 ProviderInfo providerInfo =
226 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
227 ProviderInfo redirectProvider =
228 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400231 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 if (mOldContentProviderExists) {
234 Log.d(TAG, "Old launcher provider exists.");
235 } else {
236 Log.d(TAG, "Old launcher provider does not exist.");
237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700241 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Kenny Guyed131872014-04-30 03:02:21 +0100244 mLauncherApps = LauncherAppsCompat.getInstance(context);
245 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 }
247
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700248 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
249 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 if (sWorkerThread.getThreadId() == Process.myTid()) {
252 // If we are on the worker thread, post onto the main handler
253 mHandler.post(r);
254 } else {
255 r.run();
256 }
257 }
258
259 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
260 * posted on the worker thread handler. */
261 private static void runOnWorkerThread(Runnable r) {
262 if (sWorkerThread.getThreadId() == Process.myTid()) {
263 r.run();
264 } else {
265 // If we are not on the worker thread, then post to the worker handler
266 sWorker.post(r);
267 }
268 }
269
Sunny Goyal756a28a2015-04-23 17:07:55 -0700270 /**
271 * Runs the specified runnable after the loader is complete
272 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700273 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700274 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
275 synchronized (mBindCompleteRunnables) {
276 mBindCompleteRunnables.add(r);
277 }
278 } else {
279 runOnWorkerThread(r);
280 }
281 }
282
Winson Chunge43a1e72014-01-15 10:33:02 -0800283 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
284 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800285 }
286
Sunny Goyal756adbc2015-04-16 15:20:51 -0700287 public void setPackageState(final PackageInstallInfo installInfo) {
288 Runnable updateRunnable = new Runnable() {
289
290 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500291 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700292 synchronized (sBgLock) {
293 final HashSet<ItemInfo> updates = new HashSet<>();
294
295 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
296 // Ignore install success events as they are handled by Package add events.
297 return;
298 }
299
Sunny Goyale2df0622015-04-24 11:27:00 -0700300 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700301 if (info instanceof ShortcutInfo) {
302 ShortcutInfo si = (ShortcutInfo) info;
303 ComponentName cn = si.getTargetComponent();
304 if (si.isPromise() && (cn != null)
305 && installInfo.packageName.equals(cn.getPackageName())) {
306 si.setInstallProgress(installInfo.progress);
307
308 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
309 // Mark this info as broken.
310 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
311 }
312 updates.add(si);
313 }
314 }
315 }
316
317 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
318 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
319 widget.installProgress = installInfo.progress;
320 updates.add(widget);
321 }
322 }
323
324 if (!updates.isEmpty()) {
325 // Push changes to the callback.
326 Runnable r = new Runnable() {
327 public void run() {
328 Callbacks callbacks = getCallback();
329 if (callbacks != null) {
330 callbacks.bindRestoreItemsChange(updates);
331 }
332 }
333 };
334 mHandler.post(r);
335 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500336 }
337 }
338 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500340 }
341
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 /**
343 * Updates the icons and label of all pending icons for the provided package name.
344 */
345 public void updateSessionDisplayInfo(final String packageName) {
346 Runnable updateRunnable = new Runnable() {
347
348 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700349 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700350 synchronized (sBgLock) {
351 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
352 final UserHandleCompat user = UserHandleCompat.myUserHandle();
353
Sunny Goyale2df0622015-04-24 11:27:00 -0700354 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700355 if (info instanceof ShortcutInfo) {
356 ShortcutInfo si = (ShortcutInfo) info;
357 ComponentName cn = si.getTargetComponent();
358 if (si.isPromise() && (cn != null)
359 && packageName.equals(cn.getPackageName())) {
360 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
361 // For auto install apps update the icon as well as label.
362 mIconCache.getTitleAndIcon(si,
363 si.promisedIntent, user,
364 si.shouldUseLowResIcon());
365 } else {
366 // Only update the icon for restored apps.
367 si.updateIcon(mIconCache);
368 }
369 updates.add(si);
370 }
371 }
372 }
373
374 if (!updates.isEmpty()) {
375 // Push changes to the callback.
376 Runnable r = new Runnable() {
377 public void run() {
378 Callbacks callbacks = getCallback();
379 if (callbacks != null) {
380 callbacks.bindShortcutsChanged(updates,
381 new ArrayList<ShortcutInfo>(), user);
382 }
383 }
384 };
385 mHandler.post(r);
386 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700387 }
388 }
389 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700390 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700391 }
392
Adam Cohen76a47a12014-02-05 11:47:43 -0800393 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800394 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800395
396 if (allAppsApps == null) {
397 throw new RuntimeException("allAppsApps must not be null");
398 }
399 if (allAppsApps.isEmpty()) {
400 return;
401 }
402
403 // Process the newly added applications and add them to the database first
404 Runnable r = new Runnable() {
405 public void run() {
406 runOnMainThread(new Runnable() {
407 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800408 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800409 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500410 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800411 }
412 }
413 });
414 }
415 };
416 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700417 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800418
Sunny Goyala9116722015-04-29 13:55:58 -0700419 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 int[] xy, int spanX, int spanY) {
421 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700422 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
423 final int xCount = (int) profile.numColumns;
424 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800425 boolean[][] occupied = new boolean[xCount][yCount];
426 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700427 for (ItemInfo r : occupiedPos) {
428 int right = r.cellX + r.spanX;
429 int bottom = r.cellY + r.spanY;
430 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
431 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 occupied[x][y] = true;
433 }
434 }
435 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800436 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700437 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 }
439
440 /**
441 * Find a position on the screen for the given size or adds a new screen.
442 * @return screenId and the coordinates for the item.
443 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700444 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 ArrayList<Long> workspaceScreens,
447 ArrayList<Long> addedWorkspaceScreensFinal,
448 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700449 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450
Sunny Goyala9116722015-04-29 13:55:58 -0700451 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700452 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700453 synchronized (sBgLock) {
454 for (ItemInfo info : sBgItemsIdMap) {
455 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
456 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
457 if (items == null) {
458 items = new ArrayList<>();
459 screenItems.put(info.screenId, items);
460 }
461 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
465
466 // Find appropriate space for the item.
467 long screenId = 0;
468 int[] cordinates = new int[2];
469 boolean found = false;
470
471 int screenCount = workspaceScreens.size();
472 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700473 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800474 if (preferredScreenIndex < screenCount) {
475 screenId = workspaceScreens.get(preferredScreenIndex);
476 found = findNextAvailableIconSpaceInScreen(
477 screenItems.get(screenId), cordinates, spanX, spanY);
478 }
479
480 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700481 // Search on any of the screens starting from the first screen.
482 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 screenId = workspaceScreens.get(screen);
484 if (findNextAvailableIconSpaceInScreen(
485 screenItems.get(screenId), cordinates, spanX, spanY)) {
486 // We found a space for it
487 found = true;
488 break;
489 }
490 }
491 }
492
493 if (!found) {
494 // Still no position found. Add a new screen to the end.
495 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
496
497 // Save the screen id for binding in the workspace
498 workspaceScreens.add(screenId);
499 addedWorkspaceScreensFinal.add(screenId);
500
501 // If we still can't find an empty space, then God help us all!!!
502 if (!findNextAvailableIconSpaceInScreen(
503 screenItems.get(screenId), cordinates, spanX, spanY)) {
504 throw new RuntimeException("Can't find space to add the item");
505 }
506 }
507 return Pair.create(screenId, cordinates);
508 }
509
510 /**
511 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800512 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700513 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700514 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800516 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700517 return;
Winson Chung997a9232013-07-24 15:33:46 -0700518 }
Winson Chung64359a52013-07-08 17:17:08 -0700519 // Process the newly added applications and add them to the database first
520 Runnable r = new Runnable() {
521 public void run() {
522 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
523 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
524
Winson Chung76828c82013-08-19 15:43:29 -0700525 // Get the list of workspace screens. We need to append to this list and
526 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
527 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800528 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700529 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700531 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700533 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700535 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 }
Winson Chung76828c82013-08-19 15:43:29 -0700537
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700539 Pair<Long, int[]> coords = findSpaceForItem(context,
540 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 1, 1);
542 long screenId = coords.first;
543 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700544
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 ItemInfo itemInfo;
546 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
547 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800548 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700549 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700550 } else {
551 throw new RuntimeException("Unexpected info type");
552 }
Winson Chung94d67682013-09-25 16:29:40 -0700553
Winson Chung64359a52013-07-08 17:17:08 -0700554 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700555 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700556 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700557 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700558 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700559 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700560 }
561 }
562
Winson Chung76828c82013-08-19 15:43:29 -0700563 // Update the workspace screens
564 updateWorkspaceScreenOrder(context, workspaceScreens);
565
Adam Cohen76a47a12014-02-05 11:47:43 -0800566 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700567 runOnMainThread(new Runnable() {
568 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800569 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700570 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700571 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
572 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700573 if (!addedShortcutsFinal.isEmpty()) {
574 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
575 long lastScreenId = info.screenId;
576 for (ItemInfo i : addedShortcutsFinal) {
577 if (i.screenId == lastScreenId) {
578 addAnimated.add(i);
579 } else {
580 addNotAnimated.add(i);
581 }
Winson Chung997a9232013-07-24 15:33:46 -0700582 }
583 }
Winson Chungd64d1762013-08-20 14:37:16 -0700584 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800585 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700586 }
Winson Chung64359a52013-07-08 17:17:08 -0700587 }
Winson Chung997a9232013-07-24 15:33:46 -0700588 });
589 }
Winson Chung64359a52013-07-08 17:17:08 -0700590 }
591 };
592 runOnWorkerThread(r);
593 }
594
Sunny Goyald33860f2015-04-23 16:02:20 -0700595 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700596 if (sWorkerThread.getThreadId() == Process.myTid()) {
597 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
598 "main thread");
599 }
600
601 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400602 synchronized (mDeferredBindRunnables) {
603 mDeferredBindRunnables.clear();
604 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700605
606 // Remove any queued UI runnables
607 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700608 // Unbind all the workspace items
609 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700610 }
611
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700613 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700614 // Ensure that we don't use the same workspace items data structure on the main thread
615 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700616 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700617 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700618 tmpItems.addAll(sBgWorkspaceItems);
619 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700620 }
621 Runnable r = new Runnable() {
622 @Override
623 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700624 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 item.unbind();
626 }
627 }
628 };
629 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700630 }
631
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 /**
633 * Adds an item to the DB if it was not created previously, or move it to a new
634 * <container, screen, cellX, cellY>
635 */
636 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700637 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 if (item.container == ItemInfo.NO_ID) {
639 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700640 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 } else {
642 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
645 }
646
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700647 static void checkItemInfoLocked(
648 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
649 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
650 if (modelItem != null && item != modelItem) {
651 // check all the data is consistent
652 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
653 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
654 ShortcutInfo shortcut = (ShortcutInfo) item;
655 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
656 modelShortcut.intent.filterEquals(shortcut.intent) &&
657 modelShortcut.id == shortcut.id &&
658 modelShortcut.itemType == shortcut.itemType &&
659 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700661 modelShortcut.cellX == shortcut.cellX &&
662 modelShortcut.cellY == shortcut.cellY &&
663 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700664 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700665 // For all intents and purposes, this is the same object
666 return;
667 }
668 }
669
670 // the modelItem needs to match up perfectly with item if our model is
671 // to be consistent with the database-- for now, just require
672 // modelItem == item or the equality check above
673 String msg = "item: " + ((item != null) ? item.toString() : "null") +
674 "modelItem: " +
675 ((modelItem != null) ? modelItem.toString() : "null") +
676 "Error: ItemInfo passed to checkItemInfo doesn't match original";
677 RuntimeException e = new RuntimeException(msg);
678 if (stackTrace != null) {
679 e.setStackTrace(stackTrace);
680 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800681 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700682 }
683 }
684
Michael Jurka816474f2012-06-25 14:49:02 -0700685 static void checkItemInfo(final ItemInfo item) {
686 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
687 final long itemId = item.id;
688 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700689 public void run() {
690 synchronized (sBgLock) {
691 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700692 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700693 }
694 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700695 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700696 }
697
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
699 final ItemInfo item, final String callingFunction) {
700 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700701 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700702 final ContentResolver cr = context.getContentResolver();
703
Adam Cohen487f7dd2012-06-28 18:12:10 -0700704 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705 Runnable r = new Runnable() {
706 public void run() {
707 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700708 updateItemArrays(item, itemId, stackTrace);
709 }
710 };
711 runOnWorkerThread(r);
712 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
715 final ArrayList<ItemInfo> items, final String callingFunction) {
716 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700717
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
719 Runnable r = new Runnable() {
720 public void run() {
721 ArrayList<ContentProviderOperation> ops =
722 new ArrayList<ContentProviderOperation>();
723 int count = items.size();
724 for (int i = 0; i < count; i++) {
725 ItemInfo item = items.get(i);
726 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700727 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700729
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700730 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
731 updateItemArrays(item, itemId, stackTrace);
732
733 }
734 try {
735 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
736 } catch (Exception e) {
737 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700738 }
739 }
740 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700741 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700742 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700743
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
745 // Lock on mBgLock *after* the db operation
746 synchronized (sBgLock) {
747 checkItemInfoLocked(itemId, item, stackTrace);
748
749 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
750 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
751 // Item is in a folder, make sure this folder exists
752 if (!sBgFolders.containsKey(item.container)) {
753 // An items container is being set to a that of an item which is not in
754 // the list of Folders.
755 String msg = "item: " + item + " container being set to: " +
756 item.container + ", not in the list of folders";
757 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 }
759 }
760
761 // Items are added/removed from the corresponding FolderInfo elsewhere, such
762 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
763 // that are on the desktop, as appropriate
764 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800765 if (modelItem != null &&
766 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
767 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 switch (modelItem.itemType) {
769 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
770 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
771 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
772 if (!sBgWorkspaceItems.contains(modelItem)) {
773 sBgWorkspaceItems.add(modelItem);
774 }
775 break;
776 default:
777 break;
778 }
779 } else {
780 sBgWorkspaceItems.remove(modelItem);
781 }
782 }
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700787 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700788 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791 item.cellX = cellX;
792 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700793
Winson Chung3d503fb2011-07-13 17:25:49 -0700794 // We store hotseat items in canonical form which is this orientation invariant position
795 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700797 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700799 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802
803 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
806 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800807 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809
Michael Jurkac9d95c52011-08-29 14:03:34 -0700810 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700811 }
812
813 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
815 * cellX, cellY have already been updated on the ItemInfos.
816 */
817 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
818 final long container, final int screen) {
819
820 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
821 int count = items.size();
822
823 for (int i = 0; i < count; i++) {
824 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 item.container = container;
826
827 // We store hotseat items in canonical form which is this orientation invariant position
828 // in the hotseat
829 if (context instanceof Launcher && screen < 0 &&
830 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832 item.cellY);
833 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835 }
836
837 final ContentValues values = new ContentValues();
838 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
839 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
840 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800841 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700843
844 contentValues.add(values);
845 }
846 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
847 }
848
849 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700850 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800851 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700854 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800855 item.cellX = cellX;
856 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 item.spanX = spanX;
858 item.spanY = spanY;
859
860 // We store hotseat items in canonical form which is this orientation invariant position
861 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700865 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 }
Adam Cohend4844c32011-02-18 19:25:06 -0800868
Adam Cohend4844c32011-02-18 19:25:06 -0800869 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800870 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
872 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800873 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700874 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
875 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800877
Michael Jurka816474f2012-06-25 14:49:02 -0700878 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700879 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700880
881 /**
882 * Update an item to the database in a specified container.
883 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700884 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700885 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100886 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700887 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800888 }
889
Sunny Goyal756a28a2015-04-23 17:07:55 -0700890 private void assertWorkspaceLoaded() {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700891 if (ProviderConfig.IS_DOGFOOD_BUILD && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700892 throw new RuntimeException("Trying to add shortcut while loader is running");
893 }
894 }
895
Adam Cohend4844c32011-02-18 19:25:06 -0800896 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700897 * Returns true if the shortcuts already exists on the workspace. This must be called after
898 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700900 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
901 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700902 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 if (intent.getComponent() != null) {
904 // If component is not null, an intent with null package will produce
905 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700906 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 intentWithPkg = intent.toUri(0);
909 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700910 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700911 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
912 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700913 }
914 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700915 intentWithPkg = intent.toUri(0);
916 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700917 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700918
919 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700920 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700921 if (item instanceof ShortcutInfo) {
922 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700923 Intent targetIntent = info.promisedIntent == null
924 ? info.intent : info.promisedIntent;
925 if (targetIntent != null && info.user.equals(user)) {
926 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700927 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
928 return true;
929 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700930 }
931 }
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700934 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700935 }
936
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 /**
938 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
939 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700940 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 final ContentResolver cr = context.getContentResolver();
942 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
943 "_id=? and (itemType=? or itemType=?)",
944 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700945 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700946
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 try {
948 if (c.moveToFirst()) {
949 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
950 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
951 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
952 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
953 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
954 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700955 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 FolderInfo folderInfo = null;
958 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700959 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
960 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700962 }
963
Sunny Goyala508e4f2015-05-21 09:33:57 -0700964 // Do not trim the folder label, as is was set by the user.
965 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 folderInfo.id = id;
967 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700968 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700969 folderInfo.cellX = c.getInt(cellXIndex);
970 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700971 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972
973 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700974 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 } finally {
976 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700977 }
978
979 return null;
980 }
981
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 /**
983 * Add an item to the database in a specified container. Sets the container, screen, cellX and
984 * cellY fields of the item. Also assigns an ID to the item.
985 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700986 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700987 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 item.cellX = cellX;
990 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 // We store hotseat items in canonical form which is this orientation invariant position
992 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700995 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999
1000 final ContentValues values = new ContentValues();
1001 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001002 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003
Michael Jurka414300a2013-08-27 15:42:35 +02001004 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001005 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001006
Jason Monk8e19cf22014-03-20 15:06:57 -04001007 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001008 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001010 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001013 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001014 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 sBgItemsIdMap.put(item.id, item);
1016 switch (item.itemType) {
1017 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1018 sBgFolders.put(item.id, (FolderInfo) item);
1019 // Fall through
1020 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1021 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1022 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1023 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1024 sBgWorkspaceItems.add(item);
1025 } else {
1026 if (!sBgFolders.containsKey(item.container)) {
1027 // Adding an item to a folder that doesn't exist.
1028 String msg = "adding item: " + item + " to a folder that " +
1029 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001030 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001031 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001032 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001033 break;
1034 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1035 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1036 break;
1037 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001038 }
1039 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001040 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001041 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001042 }
1043
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001045 * Creates a new unique child id, for a given cell span across all layouts.
1046 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001047 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001048 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001050 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001051 }
1052
Sunny Goyal34942622014-08-29 17:20:55 -07001053 private static ArrayList<ItemInfo> getItemsByPackageName(
1054 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001055 ItemInfoFilter filter = new ItemInfoFilter() {
1056 @Override
1057 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1058 return cn.getPackageName().equals(pn) && info.user.equals(user);
1059 }
1060 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001061 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001062 }
1063
1064 /**
1065 * Removes all the items from the database corresponding to the specified package.
1066 */
1067 static void deletePackageFromDatabase(Context context, final String pn,
1068 final UserHandleCompat user) {
1069 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001070 }
1071
1072 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001073 * Removes the specified item from the database
1074 * @param context
1075 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001076 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001077 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001078 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1079 items.add(item);
1080 deleteItemsFromDatabase(context, items);
1081 }
1082
1083 /**
1084 * Removes the specified items from the database
1085 * @param context
1086 * @param item
1087 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001088 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001090 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001091 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001092 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001093 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001094 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001096 // Lock on mBgLock *after* the db operation
1097 synchronized (sBgLock) {
1098 switch (item.itemType) {
1099 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1100 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001101 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001102 if (info.container == item.id) {
1103 // We are deleting a folder which still contains items that
1104 // think they are contained by that folder.
1105 String msg = "deleting a folder (" + item + ") which still " +
1106 "contains items (" + info + ")";
1107 Log.e(TAG, msg);
1108 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001110 sBgWorkspaceItems.remove(item);
1111 break;
1112 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1113 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1114 sBgWorkspaceItems.remove(item);
1115 break;
1116 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1117 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1118 break;
1119 }
1120 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122 }
1123 }
Michael Jurka83df1882011-08-31 20:59:26 -07001124 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 }
1127
1128 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001129 * Update the order of the workspace screens in the database. The array list contains
1130 * a list of screen ids in the order that they should appear.
1131 */
Winson Chungc9168342013-06-26 14:54:55 -07001132 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001133 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001134 final ContentResolver cr = context.getContentResolver();
1135 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1136
1137 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001138 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 while (iter.hasNext()) {
1140 long id = iter.next();
1141 if (id < 0) {
1142 iter.remove();
1143 }
1144 }
1145
1146 Runnable r = new Runnable() {
1147 @Override
1148 public void run() {
Yura085c8532014-02-11 15:15:29 +00001149 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001151 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001152 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 for (int i = 0; i < count; i++) {
1154 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001155 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001156 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1157 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001158 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001159 }
Yura085c8532014-02-11 15:15:29 +00001160
1161 try {
1162 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1163 } catch (Exception ex) {
1164 throw new RuntimeException(ex);
1165 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001166
Winson Chungba9c37f2013-08-30 14:11:37 -07001167 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001168 sBgWorkspaceScreens.clear();
1169 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001170 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 }
1172 };
1173 runOnWorkerThread(r);
1174 }
1175
1176 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 * Remove the contents of the specified folder from the database
1178 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001179 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 final ContentResolver cr = context.getContentResolver();
1181
Michael Jurkac9d95c52011-08-29 14:03:34 -07001182 Runnable r = new Runnable() {
1183 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001184 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001186 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 sBgItemsIdMap.remove(info.id);
1188 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 sBgWorkspaceItems.remove(info);
1190 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001191
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001192 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001195 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 for (ItemInfo childInfo : info.contents) {
1197 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001199 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001200 }
1201 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 }
1204
1205 /**
1206 * Set this as the current Launcher activity object for the loader.
1207 */
1208 public void initialize(Callbacks callbacks) {
1209 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001210 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1211 // workspace to prevent leaking Launcher activities on orientation change.
1212 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 mCallbacks = new WeakReference<Callbacks>(callbacks);
1214 }
1215 }
1216
Kenny Guyed131872014-04-30 03:02:21 +01001217 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001218 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001219 int op = PackageUpdatedTask.OP_UPDATE;
1220 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1221 user));
1222 }
1223
1224 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001225 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001226 int op = PackageUpdatedTask.OP_REMOVE;
1227 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1228 user));
1229 }
1230
1231 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001232 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001233 int op = PackageUpdatedTask.OP_ADD;
1234 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1235 user));
1236 }
1237
1238 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001239 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001240 boolean replacing) {
1241 if (!replacing) {
1242 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1243 user));
1244 if (mAppsCanBeOnRemoveableStorage) {
1245 // Only rebind if we support removable storage. It catches the
1246 // case where
1247 // apps on the external sd card need to be reloaded
1248 startLoaderFromBackground();
1249 }
1250 } else {
1251 // If we are replacing then just update the packages in the list
1252 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1253 packageNames, user));
1254 }
1255 }
1256
1257 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001258 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001259 boolean replacing) {
1260 if (!replacing) {
1261 enqueuePackageUpdated(new PackageUpdatedTask(
1262 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1263 user));
1264 }
Kenny Guyed131872014-04-30 03:02:21 +01001265 }
1266
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001267 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1269 * ACTION_PACKAGE_CHANGED.
1270 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001271 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001272 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001273 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001274
Joe Onorato36115782010-06-17 13:28:48 -04001275 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001276 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001277 // If we have changed locale we need to clear out the labels in all apps/workspace.
1278 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001279 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1280 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001281 Callbacks callbacks = getCallback();
1282 if (callbacks != null) {
1283 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001284 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001285 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1286 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1287 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001288 }
1289 }
1290
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001291 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001292 resetLoadedState(true, true);
1293
Reena Lee93f824a2011-09-23 17:20:28 -07001294 // Do this here because if the launcher activity is running it will be restarted.
1295 // If it's not running startLoaderFromBackground will merely tell it that it needs
1296 // to reload.
1297 startLoaderFromBackground();
1298 }
1299
Winson Chungf0c6ae02012-03-21 16:10:31 -07001300 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1301 synchronized (mLock) {
1302 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1303 // mWorkspaceLoaded to true later
1304 stopLoaderLocked();
1305 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1306 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1307 }
1308 }
1309
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001310 /**
1311 * When the launcher is in the background, it's possible for it to miss paired
1312 * configuration changes. So whenever we trigger the loader from the background
1313 * tell the launcher that it needs to re-run the loader when it comes back instead
1314 * of doing it now.
1315 */
1316 public void startLoaderFromBackground() {
1317 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001318 Callbacks callbacks = getCallback();
1319 if (callbacks != null) {
1320 // Only actually run the loader if they're not paused.
1321 if (!callbacks.setLoadOnResume()) {
1322 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001323 }
1324 }
1325 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001326 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001327 }
Joe Onorato36115782010-06-17 13:28:48 -04001328 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001329
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001330 /**
1331 * If there is already a loader task running, tell it to stop.
1332 */
1333 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001334 LoaderTask oldTask = mLoaderTask;
1335 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001336 oldTask.stopLocked();
1337 }
Reena Lee93f824a2011-09-23 17:20:28 -07001338 }
1339
Adam Cohen1a85c582014-09-30 09:48:49 -07001340 public boolean isCurrentCallbacks(Callbacks callbacks) {
1341 return (mCallbacks != null && mCallbacks.get() == callbacks);
1342 }
1343
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001344 public void startLoader(int synchronousBindPage) {
1345 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001346 }
1347
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001348 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001349 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1350 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001351 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001352 // Clear any deferred bind-runnables from the synchronized load process
1353 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001354 synchronized (mDeferredBindRunnables) {
1355 mDeferredBindRunnables.clear();
1356 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001357
Joe Onorato36115782010-06-17 13:28:48 -04001358 // Don't bother to start the thread if we know it's not going to do anything
1359 if (mCallbacks != null && mCallbacks.get() != null) {
1360 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001361 stopLoaderLocked();
1362 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001363 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001364 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1366 } else {
1367 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1368 sWorker.post(mLoaderTask);
1369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 }
1372 }
1373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 void bindRemainingSynchronousPages() {
1375 // Post the remaining side pages to be loaded
1376 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001377 Runnable[] deferredBindRunnables = null;
1378 synchronized (mDeferredBindRunnables) {
1379 deferredBindRunnables = mDeferredBindRunnables.toArray(
1380 new Runnable[mDeferredBindRunnables.size()]);
1381 mDeferredBindRunnables.clear();
1382 }
1383 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001384 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001385 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001387
1388 // Run all the bind complete runnables after workspace is bound.
1389 if (!mBindCompleteRunnables.isEmpty()) {
1390 synchronized (mBindCompleteRunnables) {
1391 for (final Runnable r : mBindCompleteRunnables) {
1392 runOnWorkerThread(r);
1393 }
1394 mBindCompleteRunnables.clear();
1395 }
1396 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 }
1398
Joe Onorato36115782010-06-17 13:28:48 -04001399 public void stopLoader() {
1400 synchronized (mLock) {
1401 if (mLoaderTask != null) {
1402 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 }
1404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001406
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001407 /**
1408 * Loads the workspace screen ids in an ordered list.
1409 */
Adam Cohen091440a2015-03-18 14:16:05 -07001410 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001411 final ContentResolver contentResolver = context.getContentResolver();
1412 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001413
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001414 // Get screens ordered by rank.
1415 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1416 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1417 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001418 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001419 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001420 while (sc.moveToNext()) {
1421 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001422 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001423 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001424 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1425 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001426 }
1427 }
1428 } finally {
1429 sc.close();
1430 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001431 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001432 }
1433
Michael Jurkac57b7a82011-08-09 22:02:20 -07001434 public boolean isAllAppsLoaded() {
1435 return mAllAppsLoaded;
1436 }
1437
Winson Chung36a62fe2012-05-06 18:04:42 -07001438 boolean isLoadingWorkspace() {
1439 synchronized (mLock) {
1440 if (mLoaderTask != null) {
1441 return mLoaderTask.isLoadingWorkspace();
1442 }
1443 }
1444 return false;
1445 }
1446
Joe Onorato36115782010-06-17 13:28:48 -04001447 /**
1448 * Runnable for the thread that loads the contents of the launcher:
1449 * - workspace icons
1450 * - widgets
1451 * - all apps icons
1452 */
1453 private class LoaderTask implements Runnable {
1454 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001455 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001456 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001457 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001458 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001459
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001460 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001461 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001462 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 }
1464
Winson Chung36a62fe2012-05-06 18:04:42 -07001465 boolean isLoadingWorkspace() {
1466 return mIsLoadingAndBindingWorkspace;
1467 }
1468
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001469 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001470 mIsLoadingAndBindingWorkspace = true;
1471
Joe Onorato36115782010-06-17 13:28:48 -04001472 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001473 if (DEBUG_LOADERS) {
1474 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001476
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001477 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001478 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001479 synchronized (LoaderTask.this) {
1480 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001481 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001482 }
1483 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 }
1485 }
1486
Joe Onorato36115782010-06-17 13:28:48 -04001487 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001488 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001489 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001490
Joe Onorato36115782010-06-17 13:28:48 -04001491 private void waitForIdle() {
1492 // Wait until the either we're stopped or the other threads are done.
1493 // This way we don't start loading all apps until the workspace has settled
1494 // down.
1495 synchronized (LoaderTask.this) {
1496 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001497
Joe Onorato36115782010-06-17 13:28:48 -04001498 mHandler.postIdle(new Runnable() {
1499 public void run() {
1500 synchronized (LoaderTask.this) {
1501 mLoadAndBindStepFinished = true;
1502 if (DEBUG_LOADERS) {
1503 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001504 }
Joe Onorato36115782010-06-17 13:28:48 -04001505 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001506 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001507 }
Joe Onorato36115782010-06-17 13:28:48 -04001508 });
1509
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001510 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001511 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001512 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1513 // wait no longer than 1sec at a time
1514 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001515 } catch (InterruptedException ex) {
1516 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001517 }
1518 }
Joe Onorato36115782010-06-17 13:28:48 -04001519 if (DEBUG_LOADERS) {
1520 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001521 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001522 + "ms for previous step to finish binding");
1523 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001524 }
Joe Onorato36115782010-06-17 13:28:48 -04001525 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001526
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001528 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 // Ensure that we have a valid page index to load synchronously
1530 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1531 "valid page index");
1532 }
1533 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1534 // Ensure that we don't try and bind a specified page when the pages have not been
1535 // loaded already (we should load everything asynchronously in that case)
1536 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1537 }
1538 synchronized (mLock) {
1539 if (mIsLoaderTaskRunning) {
1540 // Ensure that we are never running the background loading at this point since
1541 // we also touch the background collections
1542 throw new RuntimeException("Error! Background loading is already running");
1543 }
1544 }
1545
1546 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1547 // data structures, we can't allow any other thread to touch that data, but because
1548 // this call is synchronous, we can get away with not locking).
1549
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001550 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001551 // operations from the previous activity. We need to ensure that all queued operations
1552 // are executed before any synchronous binding work is done.
1553 mHandler.flush();
1554
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 // Divide the set of loaded items into those that we are binding synchronously, and
1556 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001557 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001558 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1559 // arise from that.
1560 onlyBindAllApps();
1561 }
1562
Joe Onorato36115782010-06-17 13:28:48 -04001563 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001564 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001565 if (mStopped) {
1566 return;
1567 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001568 mIsLoaderTaskRunning = true;
1569 }
Joe Onorato36115782010-06-17 13:28:48 -04001570 // Optimize for end-user experience: if the Launcher is up and // running with the
1571 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1572 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001573 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001574 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001575 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001576
Joe Onorato36115782010-06-17 13:28:48 -04001577 if (mStopped) {
1578 break keep_running;
1579 }
1580
Joe Onorato36115782010-06-17 13:28:48 -04001581 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001582
1583 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001584 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1585 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587
Joe Onorato36115782010-06-17 13:28:48 -04001588 // Clear out this reference, otherwise we end up holding it until all of the
1589 // callback runnables are done.
1590 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591
Joe Onorato36115782010-06-17 13:28:48 -04001592 synchronized (mLock) {
1593 // If we are still the last one to be scheduled, remove ourselves.
1594 if (mLoaderTask == this) {
1595 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001597 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001598 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
Joe Onorato36115782010-06-17 13:28:48 -04001600 }
1601
1602 public void stopLocked() {
1603 synchronized (LoaderTask.this) {
1604 mStopped = true;
1605 this.notify();
1606 }
1607 }
1608
1609 /**
1610 * Gets the callbacks object. If we've been stopped, or if the launcher object
1611 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1612 * object that was around when the deferred message was scheduled, and if there's
1613 * a new Callbacks object around then also return null. This will save us from
1614 * calling onto it with data that will be ignored.
1615 */
1616 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1617 synchronized (mLock) {
1618 if (mStopped) {
1619 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001620 }
Joe Onorato36115782010-06-17 13:28:48 -04001621
1622 if (mCallbacks == null) {
1623 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001624 }
Joe Onorato36115782010-06-17 13:28:48 -04001625
1626 final Callbacks callbacks = mCallbacks.get();
1627 if (callbacks != oldCallbacks) {
1628 return null;
1629 }
1630 if (callbacks == null) {
1631 Log.w(TAG, "no mCallbacks");
1632 return null;
1633 }
1634
1635 return callbacks;
1636 }
1637 }
1638
1639 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001640 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001641 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001642 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1643 final int countX = (int) profile.numColumns;
1644 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001645
Adam Cohendcd297f2013-06-18 13:13:40 -07001646 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001647 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001648 // Return early if we detect that an item is under the hotseat button
1649 if (mCallbacks == null ||
1650 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001651 Log.e(TAG, "Error loading shortcut into hotseat " + item
1652 + " into position (" + item.screenId + ":" + item.cellX + ","
1653 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001654 return false;
1655 }
1656
Dan Sandler295ae182013-12-10 16:05:47 -05001657 final ItemInfo[][] hotseatItems =
1658 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1659
Adam Cohen2e6da152015-05-06 11:42:25 -07001660 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001661 Log.e(TAG, "Error loading shortcut " + item
1662 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001663 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001664 + ")");
1665 return false;
1666 }
1667
Dan Sandler295ae182013-12-10 16:05:47 -05001668 if (hotseatItems != null) {
1669 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001670 Log.e(TAG, "Error loading shortcut into hotseat " + item
1671 + " into position (" + item.screenId + ":" + item.cellX + ","
1672 + item.cellY + ") occupied by "
1673 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1674 [(int) item.screenId][0]);
1675 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001676 } else {
1677 hotseatItems[(int) item.screenId][0] = item;
1678 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001680 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001681 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001682 items[(int) item.screenId][0] = item;
1683 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001684 return true;
1685 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001686 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1687 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001688 return true;
1689 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001690
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001692 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 occupied.put(item.screenId, items);
1694 }
1695
Dan Sandler295ae182013-12-10 16:05:47 -05001696 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001697 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1698 item.cellX < 0 || item.cellY < 0 ||
1699 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1700 Log.e(TAG, "Error loading shortcut " + item
1701 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1702 + item.cellX + "," + item.cellY
1703 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1704 return false;
1705 }
1706
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001707 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001708 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1709 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001710 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001711 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001712 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001713 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001714 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001715 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001716 return false;
1717 }
1718 }
1719 }
1720 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1721 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001722 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001723 }
1724 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001725
Joe Onorato36115782010-06-17 13:28:48 -04001726 return true;
1727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728
Winson Chungba9c37f2013-08-30 14:11:37 -07001729 /** Clears all the sBg data structures */
1730 private void clearSBgDataStructures() {
1731 synchronized (sBgLock) {
1732 sBgWorkspaceItems.clear();
1733 sBgAppWidgets.clear();
1734 sBgFolders.clear();
1735 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001736 sBgWorkspaceScreens.clear();
1737 }
1738 }
1739
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001740 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001741 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742
Joe Onorato36115782010-06-17 13:28:48 -04001743 final Context context = mContext;
1744 final ContentResolver contentResolver = context.getContentResolver();
1745 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001746 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001747 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1748 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001749 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001750
Winson Chung892c74d2013-08-22 16:15:50 -07001751 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001752 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1753 int countX = (int) profile.numColumns;
1754 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001755
Dan Sandlerd5024042014-01-09 15:01:33 -05001756 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1757 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1758 LauncherAppState.getLauncherProvider().deleteDatabase();
1759 }
1760
1761 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1762 // append the user's Launcher2 shortcuts
1763 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1764 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1765 } else {
1766 // Make sure the default workspace is loaded
1767 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001768 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001769 }
Adam Cohene25af792013-06-06 23:08:25 -07001770
Winson Chung2abf94d2012-07-18 18:16:38 -07001771 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001772 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001773 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001774 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001775
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001777 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001778 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001779 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001780 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001781
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001782 // +1 for the hotseat (it can be larger than the workspace)
1783 // Load workspace in reverse order to ensure that latest items are loaded first (and
1784 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001785 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 try {
1788 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1789 final int intentIndex = c.getColumnIndexOrThrow
1790 (LauncherSettings.Favorites.INTENT);
1791 final int titleIndex = c.getColumnIndexOrThrow
1792 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 final int containerIndex = c.getColumnIndexOrThrow(
1794 LauncherSettings.Favorites.CONTAINER);
1795 final int itemTypeIndex = c.getColumnIndexOrThrow(
1796 LauncherSettings.Favorites.ITEM_TYPE);
1797 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001799 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 final int screenIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.SCREEN);
1803 final int cellXIndex = c.getColumnIndexOrThrow
1804 (LauncherSettings.Favorites.CELLX);
1805 final int cellYIndex = c.getColumnIndexOrThrow
1806 (LauncherSettings.Favorites.CELLY);
1807 final int spanXIndex = c.getColumnIndexOrThrow
1808 (LauncherSettings.Favorites.SPANX);
1809 final int spanYIndex = c.getColumnIndexOrThrow(
1810 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001811 final int rankIndex = c.getColumnIndexOrThrow(
1812 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001813 final int restoredIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001815 final int profileIdIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001817 final int optionsIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001819 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001820
Sunny Goyal7f834d22015-04-21 10:10:23 -07001821 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1822 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1823 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1824 }
1825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 ShortcutInfo info;
1827 String intentDescription;
1828 LauncherAppWidgetInfo appWidgetInfo;
1829 int container;
1830 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001831 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001833 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 while (!mStopped && c.moveToNext()) {
1836 try {
1837 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001838 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001839 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001840 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 switch (itemType) {
1843 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1844 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001845 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001847 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001848 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001849 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001850 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001851 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001852 if (user == null) {
1853 // User has been deleted remove the item.
1854 itemsToRemove.add(id);
1855 continue;
1856 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 try {
1858 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001859 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001860 if (cn != null && cn.getPackageName() != null) {
1861 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1862 cn.getPackageName(), user);
1863 boolean validComponent = validPkg &&
1864 launcherApps.isActivityEnabledForProfile(cn, user);
1865
1866 if (validComponent) {
1867 if (restored) {
1868 // no special handling necessary for this item
1869 restoredRows.add(id);
1870 restored = false;
1871 }
1872 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001873 intent = null;
1874 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1875 // We allow auto install apps to have their intent
1876 // updated after an install.
1877 intent = manager.getLaunchIntentForPackage(
1878 cn.getPackageName());
1879 if (intent != null) {
1880 ContentValues values = new ContentValues();
1881 values.put(LauncherSettings.Favorites.INTENT,
1882 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001883 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001884 }
1885 }
1886
1887 if (intent == null) {
1888 // The app is installed but the component is no
1889 // longer available.
1890 Launcher.addDumpLog(TAG,
1891 "Invalid component removed: " + cn, true);
1892 itemsToRemove.add(id);
1893 continue;
1894 } else {
1895 // no special handling necessary for this item
1896 restoredRows.add(id);
1897 restored = false;
1898 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001899 } else if (restored) {
1900 // Package is not yet available but might be
1901 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001902 Launcher.addDumpLog(TAG,
1903 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001904
1905 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1906 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001907 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001908 // App restore has started. Update the flag
1909 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1910 ContentValues values = new ContentValues();
1911 values.put(LauncherSettings.Favorites.RESTORED,
1912 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 updateItem(id, values);
1914 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1915 // This is a common app. Try to replace this.
1916 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1917 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1918 if (parser.findDefaultApp()) {
1919 // Default app found. Replace it.
1920 intent = parser.parsedIntent;
1921 cn = intent.getComponent();
1922 ContentValues values = parser.parsedValues;
1923 values.put(LauncherSettings.Favorites.RESTORED, 0);
1924 updateItem(id, values);
1925 restored = false;
1926 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001927
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001928 } else if (REMOVE_UNRESTORED_ICONS) {
1929 Launcher.addDumpLog(TAG,
1930 "Unrestored package removed: " + cn, true);
1931 itemsToRemove.add(id);
1932 continue;
1933 }
Sunny Goyal94485362014-09-18 16:13:58 -07001934 } else if (REMOVE_UNRESTORED_ICONS) {
1935 Launcher.addDumpLog(TAG,
1936 "Unrestored package removed: " + cn, true);
1937 itemsToRemove.add(id);
1938 continue;
1939 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001940 } else if (launcherApps.isAppEnabled(
1941 manager, cn.getPackageName(),
1942 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1943 // Package is present but not available.
1944 allowMissingTarget = true;
1945 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1946 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001947 // SdCard is not ready yet. Package might get available,
1948 // once it is ready.
1949 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1950 + " (check again later)", true);
1951 HashSet<String> pkgs = sPendingPackages.get(user);
1952 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001953 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001954 sPendingPackages.put(user, pkgs);
1955 }
1956 pkgs.add(cn.getPackageName());
1957 allowMissingTarget = true;
1958 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001959
1960 } else {
1961 // Do not wait for external media load anymore.
1962 // Log the invalid package, and remove it
1963 Launcher.addDumpLog(TAG,
1964 "Invalid package removed: " + cn, true);
1965 itemsToRemove.add(id);
1966 continue;
Winson Chungee055712013-07-30 14:46:24 -07001967 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001968 } else if (cn == null) {
1969 // For shortcuts with no component, keep them as they are
1970 restoredRows.add(id);
1971 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001972 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001974 Launcher.addDumpLog(TAG,
1975 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 continue;
1977 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978
Sunny Goyal34b65272015-03-11 16:56:52 -07001979 boolean useLowResIcon = container >= 0 &&
1980 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1981
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001982 if (itemReplaced) {
1983 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001984 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001985 cursorIconInfo.iconIndex, titleIndex,
1986 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001987 } else {
1988 // Don't replace items for other profiles.
1989 itemsToRemove.add(id);
1990 continue;
1991 }
1992 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001993 if (user.equals(UserHandleCompat.myUserHandle())) {
1994 Launcher.addDumpLog(TAG,
1995 "constructing info for partially restored package",
1996 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001997 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001998 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001999 intent = getRestoredItemIntent(c, context, intent);
2000 } else {
2001 // Don't restore items for other profiles.
2002 itemsToRemove.add(id);
2003 continue;
2004 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002005 } else if (itemType ==
2006 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002007 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002008 cursorIconInfo.iconIndex, titleIndex,
2009 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002011 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002012
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 // App shortcuts that used to be automatically added to Launcher
2014 // didn't always have the correct intent flags set, so do that
2015 // here
2016 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002017 intent.getCategories() != null &&
2018 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002019 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 intent.addFlags(
2021 Intent.FLAG_ACTIVITY_NEW_TASK |
2022 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2023 }
Michael Jurka96879562012-03-22 05:54:33 -07002024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002027 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 info.cellX = c.getInt(cellXIndex);
2032 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002033 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002034 info.spanX = 1;
2035 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002036 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002037 if (info.promisedIntent != null) {
2038 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2039 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002040 info.isDisabled = disabledState;
2041 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2042 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2043 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002046 if (!checkItemPlacement(occupied, info)) {
2047 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 break;
2049 }
2050
Sunny Goyal756adbc2015-04-16 15:20:51 -07002051 if (restored) {
2052 ComponentName cn = info.getTargetComponent();
2053 if (cn != null) {
2054 Integer progress = installingPkgs.get(cn.getPackageName());
2055 if (progress != null) {
2056 info.setInstallProgress(progress);
2057 } else {
2058 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2059 }
2060 }
2061 }
2062
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 switch (container) {
2064 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2065 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2066 sBgWorkspaceItems.add(info);
2067 break;
2068 default:
2069 // Item is in a user folder
2070 FolderInfo folderInfo =
2071 findOrMakeFolder(sBgFolders, container);
2072 folderInfo.add(info);
2073 break;
2074 }
2075 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002076 } else {
2077 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 }
2079 break;
2080
2081 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2082 id = c.getLong(idIndex);
2083 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2084
Sunny Goyala508e4f2015-05-21 09:33:57 -07002085 // Do not trim the folder label, as is was set by the user.
2086 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 folderInfo.cellX = c.getInt(cellXIndex);
2091 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002092 folderInfo.spanX = 1;
2093 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002094 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095
Daniel Sandler8802e962010-05-26 16:28:16 -04002096 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002097 if (!checkItemPlacement(occupied, folderInfo)) {
2098 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002099 break;
2100 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002101
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2104 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2105 sBgWorkspaceItems.add(folderInfo);
2106 break;
Joe Onorato36115782010-06-17 13:28:48 -04002107 }
Joe Onorato17a89222011-02-08 17:26:11 -08002108
Chris Wrenf4d08112014-01-16 18:13:56 -05002109 if (restored) {
2110 // no special handling required for restored folders
2111 restoredRows.add(id);
2112 }
2113
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2115 sBgFolders.put(folderInfo.id, folderInfo);
2116 break;
2117
2118 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002119 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002121 boolean customWidget = itemType ==
2122 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2123
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002125 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002126 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002127 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002128 user = allUsers.get(serialNumber);
2129 if (user == null) {
2130 itemsToRemove.add(id);
2131 continue;
2132 }
2133
Sunny Goyalff572272014-07-23 13:58:07 -07002134 final ComponentName component =
2135 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002136
Sunny Goyal651077b2014-06-30 14:15:31 -07002137 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002138 final boolean isIdValid = (restoreStatus &
2139 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002140 final boolean wasProviderReady = (restoreStatus &
2141 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002142
Adam Cohen59400422014-03-05 18:07:04 -08002143 final LauncherAppWidgetProviderInfo provider =
2144 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002145 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002146 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002147
2148 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002149 if (!isSafeMode && !customWidget &&
2150 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002151 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002152 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002153
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002155 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 itemsToRemove.add(id);
2157 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002158 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002159 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2160 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002161
Sunny Goyalff572272014-07-23 13:58:07 -07002162 int status = restoreStatus;
2163 if (!wasProviderReady) {
2164 // If provider was not previously ready, update the
2165 // status and UI flag.
2166
2167 // Id would be valid only if the widget restore broadcast was received.
2168 if (isIdValid) {
2169 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2170 } else {
2171 status &= ~LauncherAppWidgetInfo
2172 .FLAG_PROVIDER_NOT_READY;
2173 }
2174 }
2175 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 } else {
2177 Log.v(TAG, "Widget restore pending id=" + id
2178 + " appWidgetId=" + appWidgetId
2179 + " status =" + restoreStatus);
2180 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002181 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002182 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002183 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002184
2185 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2186 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002187 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002188 // App restore has started. Update the flag
2189 appWidgetInfo.restoreStatus |=
2190 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002191 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002192 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002193 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002194 itemsToRemove.add(id);
2195 continue;
2196 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002197
2198 appWidgetInfo.installProgress =
2199 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002200 }
Sunny Goyalff572272014-07-23 13:58:07 -07002201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 appWidgetInfo.cellX = c.getInt(cellXIndex);
2205 appWidgetInfo.cellY = c.getInt(cellYIndex);
2206 appWidgetInfo.spanX = c.getInt(spanXIndex);
2207 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002208 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2211 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2212 Log.e(TAG, "Widget found where container != " +
2213 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2214 continue;
2215 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002217 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002219 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2220 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 break;
2222 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002223
Adam Cohen59400422014-03-05 18:07:04 -08002224 if (!customWidget) {
2225 String providerName =
2226 appWidgetInfo.providerName.flattenToString();
2227 if (!providerName.equals(savedProvider) ||
2228 (appWidgetInfo.restoreStatus != restoreStatus)) {
2229 ContentValues values = new ContentValues();
2230 values.put(
2231 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2232 providerName);
2233 values.put(LauncherSettings.Favorites.RESTORED,
2234 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002235 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002236 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002237 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2239 sBgAppWidgets.add(appWidgetInfo);
2240 }
Joe Onorato36115782010-06-17 13:28:48 -04002241 break;
2242 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002244 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002245 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 }
2247 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002248 if (c != null) {
2249 c.close();
2250 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 }
2252
Winson Chungba9c37f2013-08-30 14:11:37 -07002253 // Break early if we've stopped loading
2254 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002255 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002256 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002257 }
2258
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002261 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2262 Utilities.createDbSelectionQuery(
2263 LauncherSettings.Favorites._ID, itemsToRemove), null);
2264 if (DEBUG_LOADERS) {
2265 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2266 LauncherSettings.Favorites._ID, itemsToRemove));
2267 }
2268
2269 // Remove any empty folder
2270 for (long folderId : LauncherAppState.getLauncherProvider()
2271 .deleteEmptyFolders()) {
2272 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2273 sBgFolders.remove(folderId);
2274 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002275 }
2276 }
2277
Chris Wrenf4d08112014-01-16 18:13:56 -05002278 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002279 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002280 ContentValues values = new ContentValues();
2281 values.put(LauncherSettings.Favorites.RESTORED, 0);
2282 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2283 Utilities.createDbSelectionQuery(
2284 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002285 }
2286
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002287 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2288 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002289 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002290 null, sWorker);
2291 }
2292
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002293 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002294
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002295 // Remove any empty screens
2296 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002297 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002298 long screenId = item.screenId;
2299 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2300 unusedScreens.contains(screenId)) {
2301 unusedScreens.remove(screenId);
2302 }
2303 }
2304
2305 // If there are any empty screens remove them, and update.
2306 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002307 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 }
2310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 if (DEBUG_LOADERS) {
2312 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2313 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002315 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002317
Sunny Goyale2df0622015-04-24 11:27:00 -07002318 for (int i = 0; i < nScreens; i++) {
2319 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002320 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 line += " | ";
2322 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002323 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002324 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002325 if (x < screen.length && y < screen[x].length) {
2326 line += (screen[x][y] != null) ? "#" : ".";
2327 } else {
2328 line += "!";
2329 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 }
Joe Onorato36115782010-06-17 13:28:48 -04002331 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 }
Joe Onorato36115782010-06-17 13:28:48 -04002335 }
Adam Cohene25af792013-06-06 23:08:25 -07002336 }
2337
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002338 /**
2339 * Partially updates the item without any notification. Must be called on the worker thread.
2340 */
2341 private void updateItem(long itemId, ContentValues update) {
2342 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002343 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002344 update,
2345 BaseColumns._ID + "= ?",
2346 new String[]{Long.toString(itemId)});
2347 }
2348
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 /** Filters the set of items who are directly or indirectly (via another container) on the
2350 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002351 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 ArrayList<ItemInfo> allWorkspaceItems,
2353 ArrayList<ItemInfo> currentScreenItems,
2354 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002355 // Purge any null ItemInfos
2356 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2357 while (iter.hasNext()) {
2358 ItemInfo i = iter.next();
2359 if (i == null) {
2360 iter.remove();
2361 }
2362 }
2363
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 // Order the set of items by their containers first, this allows use to walk through the
2365 // list sequentially, build up a list of containers that are in the specified screen,
2366 // as well as all items in those containers.
2367 Set<Long> itemsOnScreen = new HashSet<Long>();
2368 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2369 @Override
2370 public int compare(ItemInfo lhs, ItemInfo rhs) {
2371 return (int) (lhs.container - rhs.container);
2372 }
2373 });
2374 for (ItemInfo info : allWorkspaceItems) {
2375 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002376 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 currentScreenItems.add(info);
2378 itemsOnScreen.add(info.id);
2379 } else {
2380 otherScreenItems.add(info);
2381 }
2382 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2383 currentScreenItems.add(info);
2384 itemsOnScreen.add(info.id);
2385 } else {
2386 if (itemsOnScreen.contains(info.container)) {
2387 currentScreenItems.add(info);
2388 itemsOnScreen.add(info.id);
2389 } else {
2390 otherScreenItems.add(info);
2391 }
2392 }
2393 }
2394 }
2395
2396 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002397 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002398 ArrayList<LauncherAppWidgetInfo> appWidgets,
2399 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2400 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002401
2402 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002403 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002405 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 currentScreenWidgets.add(widget);
2407 } else {
2408 otherScreenWidgets.add(widget);
2409 }
2410 }
2411 }
2412
2413 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002414 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002415 LongArrayMap<ItemInfo> itemsIdMap,
2416 LongArrayMap<FolderInfo> folders,
2417 LongArrayMap<FolderInfo> currentScreenFolders,
2418 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419
Sunny Goyale2df0622015-04-24 11:27:00 -07002420 int total = folders.size();
2421 for (int i = 0; i < total; i++) {
2422 long id = folders.keyAt(i);
2423 FolderInfo folder = folders.valueAt(i);
2424
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002426 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002428 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 currentScreenFolders.put(id, folder);
2430 } else {
2431 otherScreenFolders.put(id, folder);
2432 }
2433 }
2434 }
2435
2436 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2437 * right) */
2438 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002439 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002440 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002441 final int screenCols = profile.numColumns;
2442 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002444 @Override
2445 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002446 if (lhs.container == rhs.container) {
2447 // Within containers, order by their spatial position in that container
2448 switch ((int) lhs.container) {
2449 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2450 long lr = (lhs.screenId * screenCellCount +
2451 lhs.cellY * screenCols + lhs.cellX);
2452 long rr = (rhs.screenId * screenCellCount +
2453 rhs.cellY * screenCols + rhs.cellX);
2454 return (int) (lr - rr);
2455 }
2456 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2457 // We currently use the screen id as the rank
2458 return (int) (lhs.screenId - rhs.screenId);
2459 }
2460 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002461 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002462 throw new RuntimeException("Unexpected container type when " +
2463 "sorting workspace items.");
2464 }
2465 return 0;
2466 }
2467 } else {
2468 // Between containers, order by hotseat, desktop
2469 return (int) (lhs.container - rhs.container);
2470 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002471 }
2472 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002474
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2476 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 final Runnable r = new Runnable() {
2478 @Override
2479 public void run() {
2480 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2481 if (callbacks != null) {
2482 callbacks.bindScreens(orderedScreens);
2483 }
2484 }
2485 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002486 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002487 }
2488
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2490 final ArrayList<ItemInfo> workspaceItems,
2491 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002492 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 final boolean postOnMainThread = (deferredBindRunnables != null);
2496
2497 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002498 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002500 final int start = i;
2501 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 final Runnable r = new Runnable() {
2503 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002504 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002505 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002506 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002507 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2508 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 }
2510 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 };
2512 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002513 synchronized (deferredBindRunnables) {
2514 deferredBindRunnables.add(r);
2515 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002517 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 }
Joe Onorato36115782010-06-17 13:28:48 -04002519 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520
2521 // Bind the folders
2522 if (!folders.isEmpty()) {
2523 final Runnable r = new Runnable() {
2524 public void run() {
2525 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2526 if (callbacks != null) {
2527 callbacks.bindFolders(folders);
2528 }
2529 }
2530 };
2531 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002532 synchronized (deferredBindRunnables) {
2533 deferredBindRunnables.add(r);
2534 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002536 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 }
2538 }
2539
2540 // Bind the widgets, one at a time
2541 N = appWidgets.size();
2542 for (int i = 0; i < N; i++) {
2543 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2544 final Runnable r = new Runnable() {
2545 public void run() {
2546 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2547 if (callbacks != null) {
2548 callbacks.bindAppWidget(widget);
2549 }
2550 }
2551 };
2552 if (postOnMainThread) {
2553 deferredBindRunnables.add(r);
2554 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002555 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 }
2557 }
2558 }
2559
2560 /**
2561 * Binds all loaded data to actual views on the main thread.
2562 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002563 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 final long t = SystemClock.uptimeMillis();
2565 Runnable r;
2566
2567 // Don't use these two variables in any of the callback runnables.
2568 // Otherwise we hold a reference to them.
2569 final Callbacks oldCallbacks = mCallbacks.get();
2570 if (oldCallbacks == null) {
2571 // This launcher has exited and nobody bothered to tell us. Just bail.
2572 Log.w(TAG, "LoaderTask running with no launcher");
2573 return;
2574 }
2575
Winson Chung9b9fb962013-11-15 15:39:34 -08002576 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2578 ArrayList<LauncherAppWidgetInfo> appWidgets =
2579 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002580 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002581
2582 final LongArrayMap<FolderInfo> folders;
2583 final LongArrayMap<ItemInfo> itemsIdMap;
2584
Winson Chung2abf94d2012-07-18 18:16:38 -07002585 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002586 workspaceItems.addAll(sBgWorkspaceItems);
2587 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002588 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002589
2590 folders = sBgFolders.clone();
2591 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 }
2593
Derek Prothro7aff3992013-12-10 14:00:37 -05002594 final boolean isLoadingSynchronously =
2595 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002596 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002597 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002598 if (currScreen >= orderedScreenIds.size()) {
2599 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002600 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002601 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002602 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002603 final long currentScreenId = currentScreen < 0
2604 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002605
2606 // Load all the items that are on the current page first (and in the process, unbind
2607 // all the existing workspace items before we call startBinding() below.
2608 unbindWorkspaceItemsOnMainThread();
2609
2610 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2612 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2613 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2614 new ArrayList<LauncherAppWidgetInfo>();
2615 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2616 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002617 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2618 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002619
Winson Chung9b9fb962013-11-15 15:39:34 -08002620 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002622 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002624 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 otherFolders);
2626 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2627 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2628
2629 // Tell the workspace that we're about to start binding items
2630 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002631 public void run() {
2632 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2633 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002634 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002635 }
2636 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002638 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002639
Adam Cohendcd297f2013-06-18 13:13:40 -07002640 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2641
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002642 // Load items on the current page
2643 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2644 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002645 if (isLoadingSynchronously) {
2646 r = new Runnable() {
2647 public void run() {
2648 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002649 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002650 callbacks.onPageBoundSynchronously(currentScreen);
2651 }
2652 }
2653 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002654 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002655 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002656
Winson Chung4a2afa32012-07-19 14:53:05 -07002657 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2658 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002659 synchronized (mDeferredBindRunnables) {
2660 mDeferredBindRunnables.clear();
2661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002663 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664
2665 // Tell the workspace that we're done binding items
2666 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002667 public void run() {
2668 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2669 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002670 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002672
Winson Chung98e030b2012-05-07 16:01:11 -07002673 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002674 if (DEBUG_LOADERS) {
2675 Log.d(TAG, "bound workspace in "
2676 + (SystemClock.uptimeMillis()-t) + "ms");
2677 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002678
2679 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002680 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002681 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002682 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002683 synchronized (mDeferredBindRunnables) {
2684 mDeferredBindRunnables.add(r);
2685 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002686 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002687 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002688 }
Joe Onorato36115782010-06-17 13:28:48 -04002689 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002690
Joe Onorato36115782010-06-17 13:28:48 -04002691 private void loadAndBindAllApps() {
2692 if (DEBUG_LOADERS) {
2693 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2694 }
2695 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002696 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002697 synchronized (LoaderTask.this) {
2698 if (mStopped) {
2699 return;
2700 }
2701 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002702 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002703 synchronized (LoaderTask.this) {
2704 if (mStopped) {
2705 return;
2706 }
2707 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002708 }
Joe Onorato36115782010-06-17 13:28:48 -04002709 } else {
2710 onlyBindAllApps();
2711 }
2712 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002713
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002714 private void updateIconCache() {
2715 // Ignore packages which have a promise icon.
2716 HashSet<String> packagesToIgnore = new HashSet<>();
2717 synchronized (sBgLock) {
2718 for (ItemInfo info : sBgItemsIdMap) {
2719 if (info instanceof ShortcutInfo) {
2720 ShortcutInfo si = (ShortcutInfo) info;
2721 if (si.isPromise() && si.getTargetComponent() != null) {
2722 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2723 }
2724 } else if (info instanceof LauncherAppWidgetInfo) {
2725 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2726 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2727 packagesToIgnore.add(lawi.providerName.getPackageName());
2728 }
2729 }
2730 }
2731 }
2732 mIconCache.updateDbIcons(packagesToIgnore);
2733 }
2734
Joe Onorato36115782010-06-17 13:28:48 -04002735 private void onlyBindAllApps() {
2736 final Callbacks oldCallbacks = mCallbacks.get();
2737 if (oldCallbacks == null) {
2738 // This launcher has exited and nobody bothered to tell us. Just bail.
2739 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2740 return;
2741 }
2742
2743 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002744 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002745 final ArrayList<AppInfo> list
2746 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002747 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002748 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002749 public void run() {
2750 final long t = SystemClock.uptimeMillis();
2751 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2752 if (callbacks != null) {
2753 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002754 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002755 }
2756 if (DEBUG_LOADERS) {
2757 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2758 + (SystemClock.uptimeMillis()-t) + "ms");
2759 }
2760 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002761 };
2762 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002763 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002764 r.run();
2765 } else {
2766 mHandler.post(r);
2767 }
Joe Onorato36115782010-06-17 13:28:48 -04002768 }
2769
Winson Chung64359a52013-07-08 17:17:08 -07002770 private void loadAllApps() {
2771 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002772
Joe Onorato36115782010-06-17 13:28:48 -04002773 final Callbacks oldCallbacks = mCallbacks.get();
2774 if (oldCallbacks == null) {
2775 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002776 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002777 return;
2778 }
2779
Kenny Guyed131872014-04-30 03:02:21 +01002780 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2781
Winson Chung64359a52013-07-08 17:17:08 -07002782 // Clear the list of apps
2783 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002784 for (UserHandleCompat user : profiles) {
2785 // Query for the set of apps
2786 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002787 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002788 if (DEBUG_LOADERS) {
2789 Log.d(TAG, "getActivityList took "
2790 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2791 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2792 }
2793 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002794 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002795 if (apps == null || apps.isEmpty()) {
2796 return;
2797 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002798
Kenny Guyed131872014-04-30 03:02:21 +01002799 // Create the ApplicationInfos
2800 for (int i = 0; i < apps.size(); i++) {
2801 LauncherActivityInfoCompat app = apps.get(i);
2802 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002803 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002804 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002805
Sunny Goyal756a28a2015-04-23 17:07:55 -07002806 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2807 if (heuristic != null) {
2808 runAfterBindCompletes(new Runnable() {
2809
2810 @Override
2811 public void run() {
2812 heuristic.processUserApps(apps);
2813 }
2814 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002815 }
Winson Chung64359a52013-07-08 17:17:08 -07002816 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002817 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002818 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2819 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002820
2821 // Post callback on main thread
2822 mHandler.post(new Runnable() {
2823 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002824
Winson Chung64359a52013-07-08 17:17:08 -07002825 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002826 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002827 if (callbacks != null) {
2828 callbacks.bindAllApplications(added);
2829 if (DEBUG_LOADERS) {
2830 Log.d(TAG, "bound " + added.size() + " apps in "
2831 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2832 }
2833 } else {
2834 Log.i(TAG, "not binding apps: no Launcher activity");
2835 }
2836 }
2837 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002838 // Cleanup any data stored for a deleted user.
2839 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002840
Hyunyoung Song219d0482015-05-07 12:51:42 -07002841 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2842 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002843 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002844 Log.d(TAG, "Icons processed in "
2845 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002846 }
2847 }
2848
2849 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002850 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002851 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002852 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2853 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2854 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2855 }
Joe Onorato36115782010-06-17 13:28:48 -04002856 }
2857 }
2858
Sunny Goyal75b0f552015-05-20 21:57:06 -07002859 /**
2860 * Called when the icons for packages have been updated in the icon cache.
2861 */
2862 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2863 final Callbacks callbacks = getCallback();
2864 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2865 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2866
2867 // If any package icon has changed (app was updated while launcher was dead),
2868 // update the corresponding shortcuts.
2869 synchronized (sBgLock) {
2870 for (ItemInfo info : sBgItemsIdMap) {
2871 if (info instanceof ShortcutInfo && user.equals(info.user)
2872 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2873 ShortcutInfo si = (ShortcutInfo) info;
2874 ComponentName cn = si.getTargetComponent();
2875 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2876 si.updateIcon(mIconCache);
2877 updatedShortcuts.add(si);
2878 }
2879 }
2880 }
2881 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2882 }
2883
2884 if (!updatedShortcuts.isEmpty()) {
2885 final UserHandleCompat userFinal = user;
2886 mHandler.post(new Runnable() {
2887
2888 public void run() {
2889 Callbacks cb = getCallback();
2890 if (cb != null && callbacks == cb) {
2891 cb.bindShortcutsChanged(updatedShortcuts,
2892 new ArrayList<ShortcutInfo>(), userFinal);
2893 }
2894 }
2895 });
2896 }
2897
2898 if (!updatedApps.isEmpty()) {
2899 mHandler.post(new Runnable() {
2900
2901 public void run() {
2902 Callbacks cb = getCallback();
2903 if (cb != null && callbacks == cb) {
2904 cb.bindAppsUpdated(updatedApps);
2905 }
2906 }
2907 });
2908 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002909
2910 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2911 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002912 }
2913
Joe Onorato36115782010-06-17 13:28:48 -04002914 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002915 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002916 }
2917
Adam Cohen091440a2015-03-18 14:16:05 -07002918 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002919
2920 @Override
2921 public void onReceive(Context context, Intent intent) {
2922 synchronized (sBgLock) {
2923 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2924 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002925 final PackageManager manager = context.getPackageManager();
2926 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2927 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2929 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002930 packagesRemoved.clear();
2931 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002932 for (String pkg : entry.getValue()) {
2933 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 boolean packageOnSdcard = launcherApps.isAppEnabled(
2935 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2936 if (packageOnSdcard) {
2937 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2938 packagesUnavailable.add(pkg);
2939 } else {
2940 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2941 packagesRemoved.add(pkg);
2942 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002943 }
2944 }
2945 if (!packagesRemoved.isEmpty()) {
2946 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2947 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2948 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002949 if (!packagesUnavailable.isEmpty()) {
2950 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2951 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2952 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953 }
Sunny Goyal34942622014-08-29 17:20:55 -07002954 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002955 }
2956 }
2957 }
2958
Joe Onorato36115782010-06-17 13:28:48 -04002959 private class PackageUpdatedTask implements Runnable {
2960 int mOp;
2961 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002962 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002963
2964 public static final int OP_NONE = 0;
2965 public static final int OP_ADD = 1;
2966 public static final int OP_UPDATE = 2;
2967 public static final int OP_REMOVE = 3; // uninstlled
2968 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2969
2970
Kenny Guyed131872014-04-30 03:02:21 +01002971 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002972 mOp = op;
2973 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002974 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002975 }
2976
2977 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002978 if (!mHasLoaderCompletedOnce) {
2979 // Loader has not yet run.
2980 return;
2981 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002982 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002983
2984 final String[] packages = mPackages;
2985 final int N = packages.length;
2986 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002987 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002988 for (int i=0; i<N; i++) {
2989 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002990 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002991 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002992 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002993
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002994 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2995 if (heuristic != null) {
2996 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002997 }
Joe Onorato36115782010-06-17 13:28:48 -04002998 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002999 }
Joe Onorato36115782010-06-17 13:28:48 -04003000 case OP_UPDATE:
3001 for (int i=0; i<N; i++) {
3002 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003003 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003004 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003005 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003006 }
3007 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003008 case OP_REMOVE: {
3009 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3010 if (heuristic != null) {
3011 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003012 }
Joe Onorato36115782010-06-17 13:28:48 -04003013 for (int i=0; i<N; i++) {
3014 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003015 mIconCache.removeIconsForPkg(packages[i], mUser);
3016 }
3017 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003018 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003019 case OP_UNAVAILABLE:
3020 for (int i=0; i<N; i++) {
3021 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3022 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003023 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003024 }
3025 break;
3026 }
3027
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003028 ArrayList<AppInfo> added = null;
3029 ArrayList<AppInfo> modified = null;
3030 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003031
Adam Cohen487f7dd2012-06-28 18:12:10 -07003032 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003033 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003034 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003035 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003036 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003037 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003038 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003039 }
Winson Chung5d55f332012-07-16 20:45:03 -07003040 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003041 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003042 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003043 }
3044
Sunny Goyale0f58d72014-11-10 18:05:31 -08003045 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003046 if (callbacks == null) {
3047 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3048 return;
3049 }
3050
Sunny Goyal4390ace2014-10-13 11:33:11 -07003051 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3052 new HashMap<ComponentName, AppInfo>();
3053
Joe Onorato36115782010-06-17 13:28:48 -04003054 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003055 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003056 for (AppInfo ai : added) {
3057 addedOrUpdatedApps.put(ai.componentName, ai);
3058 }
Joe Onorato36115782010-06-17 13:28:48 -04003059 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003060
Joe Onorato36115782010-06-17 13:28:48 -04003061 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003062 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063 for (AppInfo ai : modified) {
3064 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003065 }
3066
Joe Onorato36115782010-06-17 13:28:48 -04003067 mHandler.post(new Runnable() {
3068 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003069 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003070 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003071 callbacks.bindAppsUpdated(modifiedFinal);
3072 }
3073 }
3074 });
3075 }
Winson Chung83892cc2013-05-01 16:53:33 -07003076
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003078 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3080 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3081 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3082
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003083 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003085 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3087 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088 boolean infoUpdated = false;
3089 boolean shortcutUpdated = false;
3090
3091 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003092 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003093 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003094 Bitmap icon = Utilities.createIconBitmap(
3095 si.iconResource.packageName,
3096 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003097 if (icon != null) {
3098 si.setIcon(icon);
3099 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003101 }
3102 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003103
3104 ComponentName cn = si.getTargetComponent();
3105 if (cn != null && packageSet.contains(cn.getPackageName())) {
3106 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3107
3108 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3110 // Auto install icon
3111 PackageManager pm = context.getPackageManager();
3112 ResolveInfo matched = pm.resolveActivity(
3113 new Intent(Intent.ACTION_MAIN)
3114 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3115 PackageManager.MATCH_DEFAULT_ONLY);
3116 if (matched == null) {
3117 // Try to find the best match activity.
3118 Intent intent = pm.getLaunchIntentForPackage(
3119 cn.getPackageName());
3120 if (intent != null) {
3121 cn = intent.getComponent();
3122 appInfo = addedOrUpdatedApps.get(cn);
3123 }
3124
3125 if ((intent == null) || (appInfo == null)) {
3126 removedShortcuts.add(si);
3127 continue;
3128 }
3129 si.promisedIntent = intent;
3130 }
3131 }
3132
3133 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003134 if (appInfo != null) {
3135 si.flags = appInfo.flags;
3136 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003137
Sunny Goyal756adbc2015-04-16 15:20:51 -07003138 si.intent = si.promisedIntent;
3139 si.promisedIntent = null;
3140 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003141 infoUpdated = true;
3142 si.updateIcon(mIconCache);
3143 }
3144
3145 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3146 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3147 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003148 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003149 si.contentDescription = appInfo.contentDescription;
3150 infoUpdated = true;
3151 }
3152
3153 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3154 // Since package was just updated, the target must be available now.
3155 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3156 shortcutUpdated = true;
3157 }
3158 }
3159
3160 if (infoUpdated || shortcutUpdated) {
3161 updatedShortcuts.add(si);
3162 }
3163 if (infoUpdated) {
3164 updateItemInDatabase(context, si);
3165 }
3166 } else if (info instanceof LauncherAppWidgetInfo) {
3167 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3168 if (mUser.equals(widgetInfo.user)
3169 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3170 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3171 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3172 widgets.add(widgetInfo);
3173 updateItemInDatabase(context, widgetInfo);
3174 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003175 }
3176 }
3177 }
3178
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3180 mHandler.post(new Runnable() {
3181
3182 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003183 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003184 if (callbacks == cb && cb != null) {
3185 callbacks.bindShortcutsChanged(
3186 updatedShortcuts, removedShortcuts, mUser);
3187 }
3188 }
3189 });
3190 if (!removedShortcuts.isEmpty()) {
3191 deleteItemsFromDatabase(context, removedShortcuts);
3192 }
3193 }
3194 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003195 mHandler.post(new Runnable() {
3196 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003197 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003198 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003199 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003200 }
3201 }
3202 });
3203 }
3204 }
3205
Winson Chungdf95eb12013-10-16 14:57:07 -07003206 final ArrayList<String> removedPackageNames =
3207 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003208 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003209 // Mark all packages in the broadcast to be removed
3210 removedPackageNames.addAll(Arrays.asList(packages));
3211 } else if (mOp == OP_UPDATE) {
3212 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003213 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003214 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003215 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003216 }
3217 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003218 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003219
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003221 final int removeReason;
3222 if (mOp == OP_UNAVAILABLE) {
3223 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3224 } else {
3225 // Remove all the components associated with this package
3226 for (String pn : removedPackageNames) {
3227 deletePackageFromDatabase(context, pn, mUser);
3228 }
3229 // Remove all the specific components
3230 for (AppInfo a : removedApps) {
3231 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3232 deleteItemsFromDatabase(context, infos);
3233 }
3234 removeReason = 0;
3235 }
3236
Winson Chungdf95eb12013-10-16 14:57:07 -07003237 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003238 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003240 mHandler.post(new Runnable() {
3241 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003242 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003243 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003244 callbacks.bindComponentsRemoved(
3245 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003246 }
3247 }
3248 });
Joe Onoratobe386092009-11-17 17:32:16 -08003249 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003250
3251 // onProvidersChanged method (API >= 17) already refreshed the widget list
3252 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3253
Adam Cohen4caf2982013-08-20 18:54:31 -07003254 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003255 mHandler.post(new Runnable() {
3256 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003257 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003258 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003259 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003260 }
3261 }
3262 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 }
3264 }
3265
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003266 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3267 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003268 ArrayList<LauncherAppWidgetProviderInfo> results =
3269 new ArrayList<LauncherAppWidgetProviderInfo>();
3270 try {
3271 synchronized (sBgLock) {
3272 if (sBgWidgetProviders == null || refresh) {
3273 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3274 = new HashMap<>();
3275 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3276 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003277
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003278 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3279 for (AppWidgetProviderInfo pInfo : widgets) {
3280 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3281 UserHandleCompat user = wm.getUser(info);
3282 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3283 }
Robin Lee26ace122015-03-16 19:41:43 +00003284
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003285 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3286 for (CustomAppWidget widget : customWidgets) {
3287 info = new LauncherAppWidgetProviderInfo(context, widget);
3288 UserHandleCompat user = wm.getUser(info);
3289 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3290 }
3291 // Replace the global list at the very end, so that if there is an exception,
3292 // previously loaded provider list is used.
3293 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003294 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003295 results.addAll(sBgWidgetProviders.values());
3296 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003297 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003298 } catch (Exception e) {
3299 if (e.getCause() instanceof TransactionTooLargeException) {
3300 // the returned value may be incomplete and will not be refreshed until the next
3301 // time Launcher starts.
3302 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3303 // onResume is called to refresh the widget provider list.
3304 synchronized (sBgLock) {
3305 if (sBgWidgetProviders != null) {
3306 results.addAll(sBgWidgetProviders.values());
3307 }
3308 return results;
3309 }
3310 } else {
3311 throw e;
3312 }
Adam Cohen59400422014-03-05 18:07:04 -08003313 }
3314 }
3315
Robin Lee26ace122015-03-16 19:41:43 +00003316 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3317 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003318 synchronized (sBgLock) {
3319 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003320 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003321 }
Robin Lee26ace122015-03-16 19:41:43 +00003322 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003323 }
3324 }
3325
Hyunyoung Song227239e2015-05-04 18:17:35 -07003326 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3327 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003328
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003329 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003330 @Override
3331 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003332 updateWidgetsModel(context, refresh);
3333 final WidgetsModel model = mBgWidgetsModel.clone();
3334
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003335 mHandler.post(new Runnable() {
3336 @Override
3337 public void run() {
3338 Callbacks cb = getCallback();
3339 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003340 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 }
3342 }
3343 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003344 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003345 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3346 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003347 }
3348 });
3349 }
3350
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003351 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003352 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003353 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003354 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003355 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003356 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003357 PackageManager packageManager = context.getPackageManager();
3358 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003359 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003360 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3361 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003362 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003363 }
3364
Adam Cohen091440a2015-03-18 14:16:05 -07003365 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003366 UserHandleCompat user) {
3367 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3368 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003369 }
Adam Cohen556f6132014-01-15 15:18:08 -08003370
Kenny Guyed131872014-04-30 03:02:21 +01003371 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3372 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003373 if (cn == null) {
3374 return false;
3375 }
Kenny Guyed131872014-04-30 03:02:21 +01003376 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3377 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003378 return false;
3379 }
Kenny Guyed131872014-04-30 03:02:21 +01003380 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003381 }
3382
Adam Cohena28b78e2014-05-20 17:03:04 -07003383 public static boolean isValidPackage(Context context, String packageName,
3384 UserHandleCompat user) {
3385 if (packageName == null) {
3386 return false;
3387 }
3388 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3389 return launcherApps.isPackageEnabledForProfile(packageName, user);
3390 }
3391
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003393 * Make an ShortcutInfo object for a restored application or shortcut item that points
3394 * to a package that is not yet installed on the system.
3395 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003396 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003397 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003398 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003399 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003400
3401 Bitmap icon = iconInfo.loadIcon(c, info, context);
3402 // the fallback icon
3403 if (icon == null) {
3404 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3405 } else {
3406 info.setIcon(icon);
3407 }
Sunny Goyal34942622014-08-29 17:20:55 -07003408
3409 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003410 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003411 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003412 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003413 }
Sunny Goyal34942622014-08-29 17:20:55 -07003414 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3415 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003416 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003417 }
Sunny Goyal34942622014-08-29 17:20:55 -07003418 } else {
3419 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3420 }
3421
Winson Chung82b016c2015-05-08 17:00:10 -07003422 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003423 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003424 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003425 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003426 return info;
3427 }
3428
3429 /**
3430 * Make an Intent object for a restored application or shortcut item that points
3431 * to the market page for the item.
3432 */
Adam Cohen091440a2015-03-18 14:16:05 -07003433 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003434 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003435 return getMarketIntent(componentName.getPackageName());
3436 }
3437
3438 static Intent getMarketIntent(String packageName) {
3439 return new Intent(Intent.ACTION_VIEW)
3440 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003441 .scheme("market")
3442 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003443 .appendQueryParameter("id", packageName)
3444 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003445 }
3446
3447 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003448 * Make an ShortcutInfo object for a shortcut that is an application.
3449 *
3450 * If c is not null, then it will be used to fill in missing data like the title and icon.
3451 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003452 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003453 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003454 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003455 if (user == null) {
3456 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003457 return null;
3458 }
3459
Kenny Guyed131872014-04-30 03:02:21 +01003460 ComponentName componentName = intent.getComponent();
3461 if (componentName == null) {
3462 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3463 return null;
3464 }
3465
3466 Intent newIntent = new Intent(intent.getAction(), null);
3467 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3468 newIntent.setComponent(componentName);
3469 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003470 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003471 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3472 return null;
3473 }
3474
3475 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003476 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003477 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3478 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3479 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003480 }
3481
Joe Onorato56d82912010-03-07 14:32:10 -05003482 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003483 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003484 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003485 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003486
Joe Onorato56d82912010-03-07 14:32:10 -05003487 // fall back to the class name of the activity
3488 if (info.title == null) {
3489 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003490 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003491
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003493 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003494 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003495 if (lai != null) {
3496 info.flags = AppInfo.initFlags(lai);
3497 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003499 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003500
Sunny Goyale2df0622015-04-24 11:27:00 -07003501 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003502 ItemInfoFilter f) {
3503 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3504 for (ItemInfo i : infos) {
3505 if (i instanceof ShortcutInfo) {
3506 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003507 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003508 if (cn != null && f.filterItem(null, info, cn)) {
3509 filtered.add(info);
3510 }
3511 } else if (i instanceof FolderInfo) {
3512 FolderInfo info = (FolderInfo) i;
3513 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003514 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003515 if (cn != null && f.filterItem(info, s, cn)) {
3516 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003517 }
3518 }
Winson Chung64359a52013-07-08 17:17:08 -07003519 } else if (i instanceof LauncherAppWidgetInfo) {
3520 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3521 ComponentName cn = info.providerName;
3522 if (cn != null && f.filterItem(null, info, cn)) {
3523 filtered.add(info);
3524 }
Winson Chung8a435102012-08-30 17:16:53 -07003525 }
3526 }
Winson Chung64359a52013-07-08 17:17:08 -07003527 return new ArrayList<ItemInfo>(filtered);
3528 }
3529
Adam Cohen091440a2015-03-18 14:16:05 -07003530 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003531 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003532 ItemInfoFilter filter = new ItemInfoFilter() {
3533 @Override
3534 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003535 if (info.user == null) {
3536 return cn.equals(cname);
3537 } else {
3538 return cn.equals(cname) && info.user.equals(user);
3539 }
Winson Chung64359a52013-07-08 17:17:08 -07003540 }
3541 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003542 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003543 }
3544
Sunny Goyal1a745e82014-10-02 15:58:31 -07003545 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003546 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547 */
Adam Cohen091440a2015-03-18 14:16:05 -07003548 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003549 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003550 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003551 // Non-app shortcuts are only supported for current user.
3552 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003555 // TODO: If there's an explicit component and we can't install that, delete it.
3556
Winson Chung82b016c2015-05-08 17:00:10 -07003557 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003558
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003559 Bitmap icon = iconInfo.loadIcon(c, info, context);
3560 // the fallback icon
3561 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003562 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003563 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003565 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 return info;
3567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003568
Sunny Goyal2350bc92014-10-14 16:42:54 -07003569 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003570 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3571 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3572 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3573
Adam Cohend9198822011-11-22 16:42:47 -08003574 if (intent == null) {
3575 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3576 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3577 return null;
3578 }
3579
Joe Onorato0589f0f2010-02-08 13:44:00 -08003580 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003581 boolean customIcon = false;
3582 ShortcutIconResource iconResource = null;
3583
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003584 if (bitmap instanceof Bitmap) {
3585 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 customIcon = true;
3587 } else {
3588 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003589 if (extra instanceof ShortcutIconResource) {
3590 iconResource = (ShortcutIconResource) extra;
3591 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003592 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003593 }
3594 }
3595
Michael Jurkac9d95c52011-08-29 14:03:34 -07003596 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003597
Kenny Guyed131872014-04-30 03:02:21 +01003598 // Only support intents for current user for now. Intents sent from other
3599 // users wouldn't get here without intent forwarding anyway.
3600 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003601 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003602 icon = mIconCache.getDefaultIcon(info.user);
3603 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003604 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003605 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003606
Winson Chung82b016c2015-05-08 17:00:10 -07003607 info.title = Utilities.trim(name);
3608 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003609 info.intent = intent;
3610 info.customIcon = customIcon;
3611 info.iconResource = iconResource;
3612
3613 return info;
3614 }
3615
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003617 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 * or make a new one.
3619 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003620 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 // See if a placeholder was created for us already
3622 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003623 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003625 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 folders.put(id, folderInfo);
3627 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003628 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 }
3630
Joe Onoratobe386092009-11-17 17:32:16 -08003631
Sunny Goyal651077b2014-06-30 14:15:31 -07003632 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3633 return (provider != null) && (provider.provider != null)
3634 && (provider.provider.getPackageName() != null);
3635 }
3636
Joe Onoratobe386092009-11-17 17:32:16 -08003637 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003638 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003639 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3640 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3641 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3642 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003643 if (mLoaderTask != null) {
3644 mLoaderTask.dumpState();
3645 } else {
3646 Log.d(TAG, "mLoaderTask=null");
3647 }
Joe Onoratobe386092009-11-17 17:32:16 -08003648 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003649
3650 public Callbacks getCallback() {
3651 return mCallbacks != null ? mCallbacks.get() : null;
3652 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003653
3654 /**
3655 * @return {@link FolderInfo} if its already loaded.
3656 */
3657 public FolderInfo findFolderById(Long folderId) {
3658 synchronized (sBgLock) {
3659 return sBgFolders.get(folderId);
3660 }
3661 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003662
3663 /**
3664 * @return the looper for the worker thread which can be used to start background tasks.
3665 */
3666 public static Looper getWorkerLooper() {
3667 return sWorkerThread.getLooper();
3668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003669}