blob: 8233ea2a2fd3ff5b7d8ade8b0e58ef7f90665400 [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070058import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000059import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070060import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070061import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070062import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070063import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080064
Michael Jurkac2f801e2011-07-12 14:19:46 -070065import java.lang.ref.WeakReference;
66import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070067import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070069import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070070import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.Collections;
72import java.util.Comparator;
73import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070074import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070075import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070077import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080/**
81 * Maintains in-memory state of the Launcher. It is expected that there should be only one
82 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070083 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 */
Kenny Guyed131872014-04-30 03:02:21 +010085public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010086 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040088 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070089 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070092
Dan Sandlerd5024042014-01-09 15:01:33 -050093 public static final int LOADER_FLAG_NONE = 0;
94 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
95 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
96
Joe Onorato36115782010-06-17 13:28:48 -040097 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050098 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080099
Adam Cohen091440a2015-03-18 14:16:05 -0700100 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800101 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final LauncherAppState mApp;
104 @Thunk final Object mLock = new Object();
105 @Thunk DeferredHandler mHandler = new DeferredHandler();
106 @Thunk LoaderTask mLoaderTask;
107 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700108 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Jason Monkbbe1e242014-05-16 17:37:34 -0400110 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700113 static {
114 sWorkerThread.start();
115 }
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 // We start off with everything not loaded. After that, we assume that
119 // our monitoring of the package manager provides all updates and we never
120 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk boolean mWorkspaceLoaded;
122 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // When we are loading pages synchronously, we can't just post the binding of items on the side
125 // pages as this delays the rotation process. Instead, we wait for a callback from the first
126 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
127 // a normal load, we also clear this set of Runnables.
128 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
129
Sunny Goyal756a28a2015-04-23 17:07:55 -0700130 /**
131 * Set of runnables to be called on the background thread after the workspace binding
132 * is complete.
133 */
134 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
135
Adam Cohen091440a2015-03-18 14:16:05 -0700136 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700139 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700140 // Entire list of widgets.
141 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700151 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700163 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 // sBgWorkspaceScreens is the ordered set of workspace screens.
166 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
167
Adam Cohen59400422014-03-05 18:07:04 -0800168 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000169 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk final LauncherAppsCompat mLauncherApps;
180 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
185 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
187 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700188 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700190 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700199 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
200 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
201 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700202 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700203 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700204 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700205 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100206 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700207 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700209 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Winson Chungee055712013-07-30 14:46:24 -0700219 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700220 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400221 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
222 // resource string.
223 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
224 ProviderInfo providerInfo =
225 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
226 ProviderInfo redirectProvider =
227 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700228
229 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400230 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 if (mOldContentProviderExists) {
233 Log.d(TAG, "Old launcher provider exists.");
234 } else {
235 Log.d(TAG, "Old launcher provider does not exist.");
236 }
237
Daniel Sandlere4f98912013-06-25 15:13:26 -0400238 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100239 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700240 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Kenny Guyed131872014-04-30 03:02:21 +0100243 mLauncherApps = LauncherAppsCompat.getInstance(context);
244 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
248 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 // If we are on the worker thread, post onto the main handler
252 mHandler.post(r);
253 } else {
254 r.run();
255 }
256 }
257
258 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
259 * posted on the worker thread handler. */
260 private static void runOnWorkerThread(Runnable r) {
261 if (sWorkerThread.getThreadId() == Process.myTid()) {
262 r.run();
263 } else {
264 // If we are not on the worker thread, then post to the worker handler
265 sWorker.post(r);
266 }
267 }
268
Sunny Goyal756a28a2015-04-23 17:07:55 -0700269 /**
270 * Runs the specified runnable after the loader is complete
271 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700272 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700273 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
274 synchronized (mBindCompleteRunnables) {
275 mBindCompleteRunnables.add(r);
276 }
277 } else {
278 runOnWorkerThread(r);
279 }
280 }
281
Winson Chunge43a1e72014-01-15 10:33:02 -0800282 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
283 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800284 }
285
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 public void setPackageState(final PackageInstallInfo installInfo) {
287 Runnable updateRunnable = new Runnable() {
288
289 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 synchronized (sBgLock) {
292 final HashSet<ItemInfo> updates = new HashSet<>();
293
294 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
295 // Ignore install success events as they are handled by Package add events.
296 return;
297 }
298
Sunny Goyale2df0622015-04-24 11:27:00 -0700299 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700300 if (info instanceof ShortcutInfo) {
301 ShortcutInfo si = (ShortcutInfo) info;
302 ComponentName cn = si.getTargetComponent();
303 if (si.isPromise() && (cn != null)
304 && installInfo.packageName.equals(cn.getPackageName())) {
305 si.setInstallProgress(installInfo.progress);
306
307 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
308 // Mark this info as broken.
309 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
310 }
311 updates.add(si);
312 }
313 }
314 }
315
316 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
317 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
318 widget.installProgress = installInfo.progress;
319 updates.add(widget);
320 }
321 }
322
323 if (!updates.isEmpty()) {
324 // Push changes to the callback.
325 Runnable r = new Runnable() {
326 public void run() {
327 Callbacks callbacks = getCallback();
328 if (callbacks != null) {
329 callbacks.bindRestoreItemsChange(updates);
330 }
331 }
332 };
333 mHandler.post(r);
334 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500335 }
336 }
337 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500339 }
340
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 /**
342 * Updates the icons and label of all pending icons for the provided package name.
343 */
344 public void updateSessionDisplayInfo(final String packageName) {
345 Runnable updateRunnable = new Runnable() {
346
347 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700348 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700349 synchronized (sBgLock) {
350 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
351 final UserHandleCompat user = UserHandleCompat.myUserHandle();
352
Sunny Goyale2df0622015-04-24 11:27:00 -0700353 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700354 if (info instanceof ShortcutInfo) {
355 ShortcutInfo si = (ShortcutInfo) info;
356 ComponentName cn = si.getTargetComponent();
357 if (si.isPromise() && (cn != null)
358 && packageName.equals(cn.getPackageName())) {
359 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
360 // For auto install apps update the icon as well as label.
361 mIconCache.getTitleAndIcon(si,
362 si.promisedIntent, user,
363 si.shouldUseLowResIcon());
364 } else {
365 // Only update the icon for restored apps.
366 si.updateIcon(mIconCache);
367 }
368 updates.add(si);
369 }
370 }
371 }
372
373 if (!updates.isEmpty()) {
374 // Push changes to the callback.
375 Runnable r = new Runnable() {
376 public void run() {
377 Callbacks callbacks = getCallback();
378 if (callbacks != null) {
379 callbacks.bindShortcutsChanged(updates,
380 new ArrayList<ShortcutInfo>(), user);
381 }
382 }
383 };
384 mHandler.post(r);
385 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700386 }
387 }
388 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700389 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700390 }
391
Adam Cohen76a47a12014-02-05 11:47:43 -0800392 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800393 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800394
395 if (allAppsApps == null) {
396 throw new RuntimeException("allAppsApps must not be null");
397 }
398 if (allAppsApps.isEmpty()) {
399 return;
400 }
401
402 // Process the newly added applications and add them to the database first
403 Runnable r = new Runnable() {
404 public void run() {
405 runOnMainThread(new Runnable() {
406 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800407 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800408 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500409 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 }
411 }
412 });
413 }
414 };
415 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700416 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800417
Sunny Goyala9116722015-04-29 13:55:58 -0700418 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 int[] xy, int spanX, int spanY) {
420 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700421 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
422 final int xCount = (int) profile.numColumns;
423 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 boolean[][] occupied = new boolean[xCount][yCount];
425 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700426 for (ItemInfo r : occupiedPos) {
427 int right = r.cellX + r.spanX;
428 int bottom = r.cellY + r.spanY;
429 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
430 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 occupied[x][y] = true;
432 }
433 }
434 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800435 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700436 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 }
438
439 /**
440 * Find a position on the screen for the given size or adds a new screen.
441 * @return screenId and the coordinates for the item.
442 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700443 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 ArrayList<Long> workspaceScreens,
446 ArrayList<Long> addedWorkspaceScreensFinal,
447 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700448 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449
Sunny Goyala9116722015-04-29 13:55:58 -0700450 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700451 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700452 synchronized (sBgLock) {
453 for (ItemInfo info : sBgItemsIdMap) {
454 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
455 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
456 if (items == null) {
457 items = new ArrayList<>();
458 screenItems.put(info.screenId, items);
459 }
460 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
464
465 // Find appropriate space for the item.
466 long screenId = 0;
467 int[] cordinates = new int[2];
468 boolean found = false;
469
470 int screenCount = workspaceScreens.size();
471 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700472 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800473 if (preferredScreenIndex < screenCount) {
474 screenId = workspaceScreens.get(preferredScreenIndex);
475 found = findNextAvailableIconSpaceInScreen(
476 screenItems.get(screenId), cordinates, spanX, spanY);
477 }
478
479 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700480 // Search on any of the screens starting from the first screen.
481 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 screenId = workspaceScreens.get(screen);
483 if (findNextAvailableIconSpaceInScreen(
484 screenItems.get(screenId), cordinates, spanX, spanY)) {
485 // We found a space for it
486 found = true;
487 break;
488 }
489 }
490 }
491
492 if (!found) {
493 // Still no position found. Add a new screen to the end.
494 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
495
496 // Save the screen id for binding in the workspace
497 workspaceScreens.add(screenId);
498 addedWorkspaceScreensFinal.add(screenId);
499
500 // If we still can't find an empty space, then God help us all!!!
501 if (!findNextAvailableIconSpaceInScreen(
502 screenItems.get(screenId), cordinates, spanX, spanY)) {
503 throw new RuntimeException("Can't find space to add the item");
504 }
505 }
506 return Pair.create(screenId, cordinates);
507 }
508
509 /**
510 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700513 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800515 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700516 return;
Winson Chung997a9232013-07-24 15:33:46 -0700517 }
Winson Chung64359a52013-07-08 17:17:08 -0700518 // Process the newly added applications and add them to the database first
519 Runnable r = new Runnable() {
520 public void run() {
521 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
522 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
523
Winson Chung76828c82013-08-19 15:43:29 -0700524 // Get the list of workspace screens. We need to append to this list and
525 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
526 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800527 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700528 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700530 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700532 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700534 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 }
Winson Chung76828c82013-08-19 15:43:29 -0700536
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700538 Pair<Long, int[]> coords = findSpaceForItem(context,
539 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 1, 1);
541 long screenId = coords.first;
542 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700543
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 ItemInfo itemInfo;
545 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
546 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800547 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700549 } else {
550 throw new RuntimeException("Unexpected info type");
551 }
Winson Chung94d67682013-09-25 16:29:40 -0700552
Winson Chung64359a52013-07-08 17:17:08 -0700553 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700554 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700555 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700556 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700557 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700558 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700559 }
560 }
561
Winson Chung76828c82013-08-19 15:43:29 -0700562 // Update the workspace screens
563 updateWorkspaceScreenOrder(context, workspaceScreens);
564
Adam Cohen76a47a12014-02-05 11:47:43 -0800565 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700566 runOnMainThread(new Runnable() {
567 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800568 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700569 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700570 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
571 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700572 if (!addedShortcutsFinal.isEmpty()) {
573 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
574 long lastScreenId = info.screenId;
575 for (ItemInfo i : addedShortcutsFinal) {
576 if (i.screenId == lastScreenId) {
577 addAnimated.add(i);
578 } else {
579 addNotAnimated.add(i);
580 }
Winson Chung997a9232013-07-24 15:33:46 -0700581 }
582 }
Winson Chungd64d1762013-08-20 14:37:16 -0700583 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800584 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700585 }
Winson Chung64359a52013-07-08 17:17:08 -0700586 }
Winson Chung997a9232013-07-24 15:33:46 -0700587 });
588 }
Winson Chung64359a52013-07-08 17:17:08 -0700589 }
590 };
591 runOnWorkerThread(r);
592 }
593
Sunny Goyald33860f2015-04-23 16:02:20 -0700594 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700595 if (sWorkerThread.getThreadId() == Process.myTid()) {
596 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
597 "main thread");
598 }
599
600 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400601 synchronized (mDeferredBindRunnables) {
602 mDeferredBindRunnables.clear();
603 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700604
605 // Remove any queued UI runnables
606 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700607 // Unbind all the workspace items
608 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700609 }
610
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700612 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700613 // Ensure that we don't use the same workspace items data structure on the main thread
614 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700615 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700616 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700617 tmpItems.addAll(sBgWorkspaceItems);
618 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700619 }
620 Runnable r = new Runnable() {
621 @Override
622 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700623 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 item.unbind();
625 }
626 }
627 };
628 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700629 }
630
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 /**
632 * Adds an item to the DB if it was not created previously, or move it to a new
633 * <container, screen, cellX, cellY>
634 */
635 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 if (item.container == ItemInfo.NO_ID) {
638 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700639 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 } else {
641 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644 }
645
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 static void checkItemInfoLocked(
647 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
648 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
649 if (modelItem != null && item != modelItem) {
650 // check all the data is consistent
651 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
652 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
653 ShortcutInfo shortcut = (ShortcutInfo) item;
654 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
655 modelShortcut.intent.filterEquals(shortcut.intent) &&
656 modelShortcut.id == shortcut.id &&
657 modelShortcut.itemType == shortcut.itemType &&
658 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700659 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700660 modelShortcut.cellX == shortcut.cellX &&
661 modelShortcut.cellY == shortcut.cellY &&
662 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700663 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 // For all intents and purposes, this is the same object
665 return;
666 }
667 }
668
669 // the modelItem needs to match up perfectly with item if our model is
670 // to be consistent with the database-- for now, just require
671 // modelItem == item or the equality check above
672 String msg = "item: " + ((item != null) ? item.toString() : "null") +
673 "modelItem: " +
674 ((modelItem != null) ? modelItem.toString() : "null") +
675 "Error: ItemInfo passed to checkItemInfo doesn't match original";
676 RuntimeException e = new RuntimeException(msg);
677 if (stackTrace != null) {
678 e.setStackTrace(stackTrace);
679 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800680 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 }
682 }
683
Michael Jurka816474f2012-06-25 14:49:02 -0700684 static void checkItemInfo(final ItemInfo item) {
685 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
686 final long itemId = item.id;
687 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700688 public void run() {
689 synchronized (sBgLock) {
690 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700691 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700692 }
693 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700694 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700695 }
696
Michael Jurkac9d95c52011-08-29 14:03:34 -0700697 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
698 final ItemInfo item, final String callingFunction) {
699 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700700 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701 final ContentResolver cr = context.getContentResolver();
702
Adam Cohen487f7dd2012-06-28 18:12:10 -0700703 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704 Runnable r = new Runnable() {
705 public void run() {
706 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 updateItemArrays(item, itemId, stackTrace);
708 }
709 };
710 runOnWorkerThread(r);
711 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
714 final ArrayList<ItemInfo> items, final String callingFunction) {
715 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700716
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700717 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
718 Runnable r = new Runnable() {
719 public void run() {
720 ArrayList<ContentProviderOperation> ops =
721 new ArrayList<ContentProviderOperation>();
722 int count = items.size();
723 for (int i = 0; i < count; i++) {
724 ItemInfo item = items.get(i);
725 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700726 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700727 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700728
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
730 updateItemArrays(item, itemId, stackTrace);
731
732 }
733 try {
734 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
735 } catch (Exception e) {
736 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700737 }
738 }
739 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700740 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700741 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700742
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
744 // Lock on mBgLock *after* the db operation
745 synchronized (sBgLock) {
746 checkItemInfoLocked(itemId, item, stackTrace);
747
748 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
749 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
750 // Item is in a folder, make sure this folder exists
751 if (!sBgFolders.containsKey(item.container)) {
752 // An items container is being set to a that of an item which is not in
753 // the list of Folders.
754 String msg = "item: " + item + " container being set to: " +
755 item.container + ", not in the list of folders";
756 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757 }
758 }
759
760 // Items are added/removed from the corresponding FolderInfo elsewhere, such
761 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
762 // that are on the desktop, as appropriate
763 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800764 if (modelItem != null &&
765 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
766 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 switch (modelItem.itemType) {
768 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
769 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
770 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
771 if (!sBgWorkspaceItems.contains(modelItem)) {
772 sBgWorkspaceItems.add(modelItem);
773 }
774 break;
775 default:
776 break;
777 }
778 } else {
779 sBgWorkspaceItems.remove(modelItem);
780 }
781 }
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700786 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700787 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 item.cellX = cellX;
791 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 // We store hotseat items in canonical form which is this orientation invariant position
794 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801
802 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
805 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800806 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Michael Jurkac9d95c52011-08-29 14:03:34 -0700809 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700810 }
811
812 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
814 * cellX, cellY have already been updated on the ItemInfos.
815 */
816 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
817 final long container, final int screen) {
818
819 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
820 int count = items.size();
821
822 for (int i = 0; i < count; i++) {
823 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 item.container = container;
825
826 // We store hotseat items in canonical form which is this orientation invariant position
827 // in the hotseat
828 if (context instanceof Launcher && screen < 0 &&
829 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 item.cellY);
832 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700833 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700834 }
835
836 final ContentValues values = new ContentValues();
837 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
838 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
839 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800840 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842
843 contentValues.add(values);
844 }
845 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
846 }
847
848 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800850 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700853 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800854 item.cellX = cellX;
855 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 item.spanX = spanX;
857 item.spanY = spanY;
858
859 // We store hotseat items in canonical form which is this orientation invariant position
860 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 }
Adam Cohend4844c32011-02-18 19:25:06 -0800867
Adam Cohend4844c32011-02-18 19:25:06 -0800868 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800869 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
871 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800872 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
874 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800876
Michael Jurka816474f2012-06-25 14:49:02 -0700877 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879
880 /**
881 * Update an item to the database in a specified container.
882 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700883 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700884 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100885 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800887 }
888
Sunny Goyal756a28a2015-04-23 17:07:55 -0700889 private void assertWorkspaceLoaded() {
890 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
891 throw new RuntimeException("Trying to add shortcut while loader is running");
892 }
893 }
894
Adam Cohend4844c32011-02-18 19:25:06 -0800895 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700896 * Returns true if the shortcuts already exists on the workspace. This must be called after
897 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700899 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
900 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700902 if (intent.getComponent() != null) {
903 // If component is not null, an intent with null package will produce
904 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700905 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700906 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700907 intentWithPkg = intent.toUri(0);
908 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700909 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
911 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 }
913 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700914 intentWithPkg = intent.toUri(0);
915 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700916 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917
918 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700919 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 if (item instanceof ShortcutInfo) {
921 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700922 Intent targetIntent = info.promisedIntent == null
923 ? info.intent : info.promisedIntent;
924 if (targetIntent != null && info.user.equals(user)) {
925 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700926 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
927 return true;
928 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700929 }
930 }
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700933 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700934 }
935
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 /**
937 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
938 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700939 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 final ContentResolver cr = context.getContentResolver();
941 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
942 "_id=? and (itemType=? or itemType=?)",
943 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700944 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700945
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 try {
947 if (c.moveToFirst()) {
948 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
949 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
950 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
951 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
952 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
953 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700954 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 FolderInfo folderInfo = null;
957 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700958 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
959 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700961 }
962
Sunny Goyala508e4f2015-05-21 09:33:57 -0700963 // Do not trim the folder label, as is was set by the user.
964 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 folderInfo.id = id;
966 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700968 folderInfo.cellX = c.getInt(cellXIndex);
969 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700970 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971
972 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 } finally {
975 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700976 }
977
978 return null;
979 }
980
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 /**
982 * Add an item to the database in a specified container. Sets the container, screen, cellX and
983 * cellY fields of the item. Also assigns an ID to the item.
984 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700985 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700986 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 item.cellX = cellX;
989 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700990 // We store hotseat items in canonical form which is this orientation invariant position
991 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998
999 final ContentValues values = new ContentValues();
1000 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001001 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002
Michael Jurka414300a2013-08-27 15:42:35 +02001003 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
Jason Monk8e19cf22014-03-20 15:06:57 -04001006 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001009 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001012 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001013 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 sBgItemsIdMap.put(item.id, item);
1015 switch (item.itemType) {
1016 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1017 sBgFolders.put(item.id, (FolderInfo) item);
1018 // Fall through
1019 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1020 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1021 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1022 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1023 sBgWorkspaceItems.add(item);
1024 } else {
1025 if (!sBgFolders.containsKey(item.container)) {
1026 // Adding an item to a folder that doesn't exist.
1027 String msg = "adding item: " + item + " to a folder that " +
1028 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001029 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001031 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 break;
1033 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1034 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1035 break;
1036 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001037 }
1038 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001039 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001041 }
1042
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001044 * Creates a new unique child id, for a given cell span across all layouts.
1045 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001046 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001047 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001048 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001050 }
1051
Sunny Goyal34942622014-08-29 17:20:55 -07001052 private static ArrayList<ItemInfo> getItemsByPackageName(
1053 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001054 ItemInfoFilter filter = new ItemInfoFilter() {
1055 @Override
1056 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1057 return cn.getPackageName().equals(pn) && info.user.equals(user);
1058 }
1059 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001060 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001061 }
1062
1063 /**
1064 * Removes all the items from the database corresponding to the specified package.
1065 */
1066 static void deletePackageFromDatabase(Context context, final String pn,
1067 final UserHandleCompat user) {
1068 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001069 }
1070
1071 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001072 * Removes the specified item from the database
1073 * @param context
1074 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001076 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1078 items.add(item);
1079 deleteItemsFromDatabase(context, items);
1080 }
1081
1082 /**
1083 * Removes the specified items from the database
1084 * @param context
1085 * @param item
1086 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001087 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001089 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001090 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001091 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001092 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001093 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001094
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 // Lock on mBgLock *after* the db operation
1096 synchronized (sBgLock) {
1097 switch (item.itemType) {
1098 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1099 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001100 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 if (info.container == item.id) {
1102 // We are deleting a folder which still contains items that
1103 // think they are contained by that folder.
1104 String msg = "deleting a folder (" + item + ") which still " +
1105 "contains items (" + info + ")";
1106 Log.e(TAG, msg);
1107 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001109 sBgWorkspaceItems.remove(item);
1110 break;
1111 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1112 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1113 sBgWorkspaceItems.remove(item);
1114 break;
1115 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1116 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1117 break;
1118 }
1119 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 }
1122 }
Michael Jurka83df1882011-08-31 20:59:26 -07001123 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 }
1126
1127 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 * Update the order of the workspace screens in the database. The array list contains
1129 * a list of screen ids in the order that they should appear.
1130 */
Winson Chungc9168342013-06-26 14:54:55 -07001131 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001132 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001133 final ContentResolver cr = context.getContentResolver();
1134 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1135
1136 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001137 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 while (iter.hasNext()) {
1139 long id = iter.next();
1140 if (id < 0) {
1141 iter.remove();
1142 }
1143 }
1144
1145 Runnable r = new Runnable() {
1146 @Override
1147 public void run() {
Yura085c8532014-02-11 15:15:29 +00001148 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001150 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001151 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 for (int i = 0; i < count; i++) {
1153 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001154 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001155 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1156 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001157 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001158 }
Yura085c8532014-02-11 15:15:29 +00001159
1160 try {
1161 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1162 } catch (Exception ex) {
1163 throw new RuntimeException(ex);
1164 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001165
Winson Chungba9c37f2013-08-30 14:11:37 -07001166 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001167 sBgWorkspaceScreens.clear();
1168 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001169 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 }
1171 };
1172 runOnWorkerThread(r);
1173 }
1174
1175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 * Remove the contents of the specified folder from the database
1177 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001178 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 final ContentResolver cr = context.getContentResolver();
1180
Michael Jurkac9d95c52011-08-29 14:03:34 -07001181 Runnable r = new Runnable() {
1182 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001183 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001185 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 sBgItemsIdMap.remove(info.id);
1187 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 sBgWorkspaceItems.remove(info);
1189 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001190
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001191 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001192 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001194 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 for (ItemInfo childInfo : info.contents) {
1196 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001198 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001199 }
1200 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203
1204 /**
1205 * Set this as the current Launcher activity object for the loader.
1206 */
1207 public void initialize(Callbacks callbacks) {
1208 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001209 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1210 // workspace to prevent leaking Launcher activities on orientation change.
1211 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 mCallbacks = new WeakReference<Callbacks>(callbacks);
1213 }
1214 }
1215
Kenny Guyed131872014-04-30 03:02:21 +01001216 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001217 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001218 int op = PackageUpdatedTask.OP_UPDATE;
1219 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1220 user));
1221 }
1222
1223 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001224 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001225 int op = PackageUpdatedTask.OP_REMOVE;
1226 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1227 user));
1228 }
1229
1230 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001231 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001232 int op = PackageUpdatedTask.OP_ADD;
1233 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1234 user));
1235 }
1236
1237 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001238 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001239 boolean replacing) {
1240 if (!replacing) {
1241 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1242 user));
1243 if (mAppsCanBeOnRemoveableStorage) {
1244 // Only rebind if we support removable storage. It catches the
1245 // case where
1246 // apps on the external sd card need to be reloaded
1247 startLoaderFromBackground();
1248 }
1249 } else {
1250 // If we are replacing then just update the packages in the list
1251 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1252 packageNames, user));
1253 }
1254 }
1255
1256 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001257 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001258 boolean replacing) {
1259 if (!replacing) {
1260 enqueuePackageUpdated(new PackageUpdatedTask(
1261 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1262 user));
1263 }
Kenny Guyed131872014-04-30 03:02:21 +01001264 }
1265
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001266 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1268 * ACTION_PACKAGE_CHANGED.
1269 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001270 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001271 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001272 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001273
Joe Onorato36115782010-06-17 13:28:48 -04001274 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001275 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 // If we have changed locale we need to clear out the labels in all apps/workspace.
1277 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001278 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1279 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001280 Callbacks callbacks = getCallback();
1281 if (callbacks != null) {
1282 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001283 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001284 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1285 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1286 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001287 }
1288 }
1289
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001290 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001291 resetLoadedState(true, true);
1292
Reena Lee93f824a2011-09-23 17:20:28 -07001293 // Do this here because if the launcher activity is running it will be restarted.
1294 // If it's not running startLoaderFromBackground will merely tell it that it needs
1295 // to reload.
1296 startLoaderFromBackground();
1297 }
1298
Winson Chungf0c6ae02012-03-21 16:10:31 -07001299 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1300 synchronized (mLock) {
1301 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1302 // mWorkspaceLoaded to true later
1303 stopLoaderLocked();
1304 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1305 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1306 }
1307 }
1308
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001309 /**
1310 * When the launcher is in the background, it's possible for it to miss paired
1311 * configuration changes. So whenever we trigger the loader from the background
1312 * tell the launcher that it needs to re-run the loader when it comes back instead
1313 * of doing it now.
1314 */
1315 public void startLoaderFromBackground() {
1316 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001317 Callbacks callbacks = getCallback();
1318 if (callbacks != null) {
1319 // Only actually run the loader if they're not paused.
1320 if (!callbacks.setLoadOnResume()) {
1321 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001322 }
1323 }
1324 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001325 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001326 }
Joe Onorato36115782010-06-17 13:28:48 -04001327 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001328
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001329 /**
1330 * If there is already a loader task running, tell it to stop.
1331 */
1332 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001333 LoaderTask oldTask = mLoaderTask;
1334 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001335 oldTask.stopLocked();
1336 }
Reena Lee93f824a2011-09-23 17:20:28 -07001337 }
1338
Adam Cohen1a85c582014-09-30 09:48:49 -07001339 public boolean isCurrentCallbacks(Callbacks callbacks) {
1340 return (mCallbacks != null && mCallbacks.get() == callbacks);
1341 }
1342
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001343 public void startLoader(int synchronousBindPage) {
1344 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001345 }
1346
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001347 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001348 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1349 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001350 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001351 // Clear any deferred bind-runnables from the synchronized load process
1352 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001353 synchronized (mDeferredBindRunnables) {
1354 mDeferredBindRunnables.clear();
1355 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356
Joe Onorato36115782010-06-17 13:28:48 -04001357 // Don't bother to start the thread if we know it's not going to do anything
1358 if (mCallbacks != null && mCallbacks.get() != null) {
1359 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001360 stopLoaderLocked();
1361 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001362 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001363 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1365 } else {
1366 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1367 sWorker.post(mLoaderTask);
1368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
1371 }
1372
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 void bindRemainingSynchronousPages() {
1374 // Post the remaining side pages to be loaded
1375 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001376 Runnable[] deferredBindRunnables = null;
1377 synchronized (mDeferredBindRunnables) {
1378 deferredBindRunnables = mDeferredBindRunnables.toArray(
1379 new Runnable[mDeferredBindRunnables.size()]);
1380 mDeferredBindRunnables.clear();
1381 }
1382 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001383 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001385 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001386
1387 // Run all the bind complete runnables after workspace is bound.
1388 if (!mBindCompleteRunnables.isEmpty()) {
1389 synchronized (mBindCompleteRunnables) {
1390 for (final Runnable r : mBindCompleteRunnables) {
1391 runOnWorkerThread(r);
1392 }
1393 mBindCompleteRunnables.clear();
1394 }
1395 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001396 }
1397
Joe Onorato36115782010-06-17 13:28:48 -04001398 public void stopLoader() {
1399 synchronized (mLock) {
1400 if (mLoaderTask != null) {
1401 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 }
1403 }
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001406 /**
1407 * Loads the workspace screen ids in an ordered list.
1408 */
Adam Cohen091440a2015-03-18 14:16:05 -07001409 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001410 final ContentResolver contentResolver = context.getContentResolver();
1411 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001412
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001413 // Get screens ordered by rank.
1414 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1415 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1416 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001417 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001418 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001419 while (sc.moveToNext()) {
1420 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001421 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001422 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001423 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1424 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001425 }
1426 }
1427 } finally {
1428 sc.close();
1429 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001431 }
1432
Michael Jurkac57b7a82011-08-09 22:02:20 -07001433 public boolean isAllAppsLoaded() {
1434 return mAllAppsLoaded;
1435 }
1436
Winson Chung36a62fe2012-05-06 18:04:42 -07001437 boolean isLoadingWorkspace() {
1438 synchronized (mLock) {
1439 if (mLoaderTask != null) {
1440 return mLoaderTask.isLoadingWorkspace();
1441 }
1442 }
1443 return false;
1444 }
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 /**
1447 * Runnable for the thread that loads the contents of the launcher:
1448 * - workspace icons
1449 * - widgets
1450 * - all apps icons
1451 */
1452 private class LoaderTask implements Runnable {
1453 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001454 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001455 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001456 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001457 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001459 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001460 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001461 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 }
1463
Winson Chung36a62fe2012-05-06 18:04:42 -07001464 boolean isLoadingWorkspace() {
1465 return mIsLoadingAndBindingWorkspace;
1466 }
1467
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001468 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001469 mIsLoadingAndBindingWorkspace = true;
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001475
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001476 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001477 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001478 synchronized (LoaderTask.this) {
1479 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001480 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001481 }
1482 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 }
1484 }
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001487 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001489
Joe Onorato36115782010-06-17 13:28:48 -04001490 private void waitForIdle() {
1491 // Wait until the either we're stopped or the other threads are done.
1492 // This way we don't start loading all apps until the workspace has settled
1493 // down.
1494 synchronized (LoaderTask.this) {
1495 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001496
Joe Onorato36115782010-06-17 13:28:48 -04001497 mHandler.postIdle(new Runnable() {
1498 public void run() {
1499 synchronized (LoaderTask.this) {
1500 mLoadAndBindStepFinished = true;
1501 if (DEBUG_LOADERS) {
1502 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001505 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001506 }
Joe Onorato36115782010-06-17 13:28:48 -04001507 });
1508
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001509 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001510 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001511 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1512 // wait no longer than 1sec at a time
1513 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001514 } catch (InterruptedException ex) {
1515 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001516 }
1517 }
Joe Onorato36115782010-06-17 13:28:48 -04001518 if (DEBUG_LOADERS) {
1519 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001520 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001521 + "ms for previous step to finish binding");
1522 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001523 }
Joe Onorato36115782010-06-17 13:28:48 -04001524 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001525
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001527 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 // Ensure that we have a valid page index to load synchronously
1529 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1530 "valid page index");
1531 }
1532 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1533 // Ensure that we don't try and bind a specified page when the pages have not been
1534 // loaded already (we should load everything asynchronously in that case)
1535 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1536 }
1537 synchronized (mLock) {
1538 if (mIsLoaderTaskRunning) {
1539 // Ensure that we are never running the background loading at this point since
1540 // we also touch the background collections
1541 throw new RuntimeException("Error! Background loading is already running");
1542 }
1543 }
1544
1545 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1546 // data structures, we can't allow any other thread to touch that data, but because
1547 // this call is synchronous, we can get away with not locking).
1548
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001549 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001550 // operations from the previous activity. We need to ensure that all queued operations
1551 // are executed before any synchronous binding work is done.
1552 mHandler.flush();
1553
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001554 // Divide the set of loaded items into those that we are binding synchronously, and
1555 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001556 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001557 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1558 // arise from that.
1559 onlyBindAllApps();
1560 }
1561
Joe Onorato36115782010-06-17 13:28:48 -04001562 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001563 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001564 if (mStopped) {
1565 return;
1566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 mIsLoaderTaskRunning = true;
1568 }
Joe Onorato36115782010-06-17 13:28:48 -04001569 // Optimize for end-user experience: if the Launcher is up and // running with the
1570 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1571 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001572 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001573 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001574 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001575
Joe Onorato36115782010-06-17 13:28:48 -04001576 if (mStopped) {
1577 break keep_running;
1578 }
1579
Joe Onorato36115782010-06-17 13:28:48 -04001580 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001581
1582 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001583 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1584 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586
Joe Onorato36115782010-06-17 13:28:48 -04001587 // Clear out this reference, otherwise we end up holding it until all of the
1588 // callback runnables are done.
1589 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590
Joe Onorato36115782010-06-17 13:28:48 -04001591 synchronized (mLock) {
1592 // If we are still the last one to be scheduled, remove ourselves.
1593 if (mLoaderTask == this) {
1594 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001596 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001597 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
1600
1601 public void stopLocked() {
1602 synchronized (LoaderTask.this) {
1603 mStopped = true;
1604 this.notify();
1605 }
1606 }
1607
1608 /**
1609 * Gets the callbacks object. If we've been stopped, or if the launcher object
1610 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1611 * object that was around when the deferred message was scheduled, and if there's
1612 * a new Callbacks object around then also return null. This will save us from
1613 * calling onto it with data that will be ignored.
1614 */
1615 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1616 synchronized (mLock) {
1617 if (mStopped) {
1618 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001619 }
Joe Onorato36115782010-06-17 13:28:48 -04001620
1621 if (mCallbacks == null) {
1622 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001623 }
Joe Onorato36115782010-06-17 13:28:48 -04001624
1625 final Callbacks callbacks = mCallbacks.get();
1626 if (callbacks != oldCallbacks) {
1627 return null;
1628 }
1629 if (callbacks == null) {
1630 Log.w(TAG, "no mCallbacks");
1631 return null;
1632 }
1633
1634 return callbacks;
1635 }
1636 }
1637
1638 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001639 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001640 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001641 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1642 final int countX = (int) profile.numColumns;
1643 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001644
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001646 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001647 // Return early if we detect that an item is under the hotseat button
1648 if (mCallbacks == null ||
1649 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001650 Log.e(TAG, "Error loading shortcut into hotseat " + item
1651 + " into position (" + item.screenId + ":" + item.cellX + ","
1652 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001653 return false;
1654 }
1655
Dan Sandler295ae182013-12-10 16:05:47 -05001656 final ItemInfo[][] hotseatItems =
1657 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1658
Adam Cohen2e6da152015-05-06 11:42:25 -07001659 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001660 Log.e(TAG, "Error loading shortcut " + item
1661 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001662 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001663 + ")");
1664 return false;
1665 }
1666
Dan Sandler295ae182013-12-10 16:05:47 -05001667 if (hotseatItems != null) {
1668 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 Log.e(TAG, "Error loading shortcut into hotseat " + item
1670 + " into position (" + item.screenId + ":" + item.cellX + ","
1671 + item.cellY + ") occupied by "
1672 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1673 [(int) item.screenId][0]);
1674 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001675 } else {
1676 hotseatItems[(int) item.screenId][0] = item;
1677 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001679 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001680 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001681 items[(int) item.screenId][0] = item;
1682 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001683 return true;
1684 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001685 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1686 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001687 return true;
1688 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001689
Adam Cohendcd297f2013-06-18 13:13:40 -07001690 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001691 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 occupied.put(item.screenId, items);
1693 }
1694
Dan Sandler295ae182013-12-10 16:05:47 -05001695 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001696 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1697 item.cellX < 0 || item.cellY < 0 ||
1698 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1699 Log.e(TAG, "Error loading shortcut " + item
1700 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1701 + item.cellX + "," + item.cellY
1702 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1703 return false;
1704 }
1705
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001706 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001707 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1708 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001709 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001710 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001712 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001713 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001715 return false;
1716 }
1717 }
1718 }
1719 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1720 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001722 }
1723 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001724
Joe Onorato36115782010-06-17 13:28:48 -04001725 return true;
1726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungba9c37f2013-08-30 14:11:37 -07001728 /** Clears all the sBg data structures */
1729 private void clearSBgDataStructures() {
1730 synchronized (sBgLock) {
1731 sBgWorkspaceItems.clear();
1732 sBgAppWidgets.clear();
1733 sBgFolders.clear();
1734 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001735 sBgWorkspaceScreens.clear();
1736 }
1737 }
1738
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001739 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001740 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741
Joe Onorato36115782010-06-17 13:28:48 -04001742 final Context context = mContext;
1743 final ContentResolver contentResolver = context.getContentResolver();
1744 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001745 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001746 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1747 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001748 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001749
Winson Chung892c74d2013-08-22 16:15:50 -07001750 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001751 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1752 int countX = (int) profile.numColumns;
1753 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001754
Dan Sandlerd5024042014-01-09 15:01:33 -05001755 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1756 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1757 LauncherAppState.getLauncherProvider().deleteDatabase();
1758 }
1759
1760 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1761 // append the user's Launcher2 shortcuts
1762 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1763 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1764 } else {
1765 // Make sure the default workspace is loaded
1766 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001767 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001768 }
Adam Cohene25af792013-06-06 23:08:25 -07001769
Winson Chung2abf94d2012-07-18 18:16:38 -07001770 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001771 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001772 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001773 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001776 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001777 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001778 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001779 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001780
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 // +1 for the hotseat (it can be larger than the workspace)
1782 // Load workspace in reverse order to ensure that latest items are loaded first (and
1783 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001784 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 try {
1787 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1788 final int intentIndex = c.getColumnIndexOrThrow
1789 (LauncherSettings.Favorites.INTENT);
1790 final int titleIndex = c.getColumnIndexOrThrow
1791 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 final int containerIndex = c.getColumnIndexOrThrow(
1793 LauncherSettings.Favorites.CONTAINER);
1794 final int itemTypeIndex = c.getColumnIndexOrThrow(
1795 LauncherSettings.Favorites.ITEM_TYPE);
1796 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1797 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001798 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1799 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 final int screenIndex = c.getColumnIndexOrThrow(
1801 LauncherSettings.Favorites.SCREEN);
1802 final int cellXIndex = c.getColumnIndexOrThrow
1803 (LauncherSettings.Favorites.CELLX);
1804 final int cellYIndex = c.getColumnIndexOrThrow
1805 (LauncherSettings.Favorites.CELLY);
1806 final int spanXIndex = c.getColumnIndexOrThrow
1807 (LauncherSettings.Favorites.SPANX);
1808 final int spanYIndex = c.getColumnIndexOrThrow(
1809 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001810 final int rankIndex = c.getColumnIndexOrThrow(
1811 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001812 final int restoredIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001814 final int profileIdIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001816 final int optionsIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001818 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819
Sunny Goyal7f834d22015-04-21 10:10:23 -07001820 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1821 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1822 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1823 }
1824
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 ShortcutInfo info;
1826 String intentDescription;
1827 LauncherAppWidgetInfo appWidgetInfo;
1828 int container;
1829 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001830 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001832 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 while (!mStopped && c.moveToNext()) {
1835 try {
1836 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001837 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001838 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001839 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 switch (itemType) {
1842 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1843 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001844 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001846 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001847 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001848 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001849 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001850 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001851 if (user == null) {
1852 // User has been deleted remove the item.
1853 itemsToRemove.add(id);
1854 continue;
1855 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 try {
1857 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001858 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001859 if (cn != null && cn.getPackageName() != null) {
1860 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1861 cn.getPackageName(), user);
1862 boolean validComponent = validPkg &&
1863 launcherApps.isActivityEnabledForProfile(cn, user);
1864
1865 if (validComponent) {
1866 if (restored) {
1867 // no special handling necessary for this item
1868 restoredRows.add(id);
1869 restored = false;
1870 }
1871 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001872 intent = null;
1873 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1874 // We allow auto install apps to have their intent
1875 // updated after an install.
1876 intent = manager.getLaunchIntentForPackage(
1877 cn.getPackageName());
1878 if (intent != null) {
1879 ContentValues values = new ContentValues();
1880 values.put(LauncherSettings.Favorites.INTENT,
1881 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001882 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001883 }
1884 }
1885
1886 if (intent == null) {
1887 // The app is installed but the component is no
1888 // longer available.
1889 Launcher.addDumpLog(TAG,
1890 "Invalid component removed: " + cn, true);
1891 itemsToRemove.add(id);
1892 continue;
1893 } else {
1894 // no special handling necessary for this item
1895 restoredRows.add(id);
1896 restored = false;
1897 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001898 } else if (restored) {
1899 // Package is not yet available but might be
1900 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 Launcher.addDumpLog(TAG,
1902 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001903
1904 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1905 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001906 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001907 // App restore has started. Update the flag
1908 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1909 ContentValues values = new ContentValues();
1910 values.put(LauncherSettings.Favorites.RESTORED,
1911 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001912 updateItem(id, values);
1913 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1914 // This is a common app. Try to replace this.
1915 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1916 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1917 if (parser.findDefaultApp()) {
1918 // Default app found. Replace it.
1919 intent = parser.parsedIntent;
1920 cn = intent.getComponent();
1921 ContentValues values = parser.parsedValues;
1922 values.put(LauncherSettings.Favorites.RESTORED, 0);
1923 updateItem(id, values);
1924 restored = false;
1925 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001926
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001927 } else if (REMOVE_UNRESTORED_ICONS) {
1928 Launcher.addDumpLog(TAG,
1929 "Unrestored package removed: " + cn, true);
1930 itemsToRemove.add(id);
1931 continue;
1932 }
Sunny Goyal94485362014-09-18 16:13:58 -07001933 } else if (REMOVE_UNRESTORED_ICONS) {
1934 Launcher.addDumpLog(TAG,
1935 "Unrestored package removed: " + cn, true);
1936 itemsToRemove.add(id);
1937 continue;
1938 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001939 } else if (launcherApps.isAppEnabled(
1940 manager, cn.getPackageName(),
1941 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1942 // Package is present but not available.
1943 allowMissingTarget = true;
1944 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1945 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001946 // SdCard is not ready yet. Package might get available,
1947 // once it is ready.
1948 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1949 + " (check again later)", true);
1950 HashSet<String> pkgs = sPendingPackages.get(user);
1951 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001952 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001953 sPendingPackages.put(user, pkgs);
1954 }
1955 pkgs.add(cn.getPackageName());
1956 allowMissingTarget = true;
1957 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001958
1959 } else {
1960 // Do not wait for external media load anymore.
1961 // Log the invalid package, and remove it
1962 Launcher.addDumpLog(TAG,
1963 "Invalid package removed: " + cn, true);
1964 itemsToRemove.add(id);
1965 continue;
Winson Chungee055712013-07-30 14:46:24 -07001966 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001967 } else if (cn == null) {
1968 // For shortcuts with no component, keep them as they are
1969 restoredRows.add(id);
1970 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001971 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001973 Launcher.addDumpLog(TAG,
1974 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 continue;
1976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977
Sunny Goyal34b65272015-03-11 16:56:52 -07001978 boolean useLowResIcon = container >= 0 &&
1979 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1980
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001981 if (itemReplaced) {
1982 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001983 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001984 cursorIconInfo.iconIndex, titleIndex,
1985 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001986 } else {
1987 // Don't replace items for other profiles.
1988 itemsToRemove.add(id);
1989 continue;
1990 }
1991 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001992 if (user.equals(UserHandleCompat.myUserHandle())) {
1993 Launcher.addDumpLog(TAG,
1994 "constructing info for partially restored package",
1995 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001996 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001997 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001998 intent = getRestoredItemIntent(c, context, intent);
1999 } else {
2000 // Don't restore items for other profiles.
2001 itemsToRemove.add(id);
2002 continue;
2003 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002004 } else if (itemType ==
2005 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002006 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002007 cursorIconInfo.iconIndex, titleIndex,
2008 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 // App shortcuts that used to be automatically added to Launcher
2013 // didn't always have the correct intent flags set, so do that
2014 // here
2015 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002016 intent.getCategories() != null &&
2017 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002018 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 intent.addFlags(
2020 Intent.FLAG_ACTIVITY_NEW_TASK |
2021 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2022 }
Michael Jurka96879562012-03-22 05:54:33 -07002023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002026 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 info.cellX = c.getInt(cellXIndex);
2031 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002032 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002033 info.spanX = 1;
2034 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002035 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002036 if (info.promisedIntent != null) {
2037 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2038 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002039 info.isDisabled = disabledState;
2040 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2041 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2042 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002045 if (!checkItemPlacement(occupied, info)) {
2046 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 break;
2048 }
2049
Sunny Goyal756adbc2015-04-16 15:20:51 -07002050 if (restored) {
2051 ComponentName cn = info.getTargetComponent();
2052 if (cn != null) {
2053 Integer progress = installingPkgs.get(cn.getPackageName());
2054 if (progress != null) {
2055 info.setInstallProgress(progress);
2056 } else {
2057 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2058 }
2059 }
2060 }
2061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 switch (container) {
2063 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2064 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2065 sBgWorkspaceItems.add(info);
2066 break;
2067 default:
2068 // Item is in a user folder
2069 FolderInfo folderInfo =
2070 findOrMakeFolder(sBgFolders, container);
2071 folderInfo.add(info);
2072 break;
2073 }
2074 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002075 } else {
2076 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 }
2078 break;
2079
2080 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2081 id = c.getLong(idIndex);
2082 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2083
Sunny Goyala508e4f2015-05-21 09:33:57 -07002084 // Do not trim the folder label, as is was set by the user.
2085 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 folderInfo.cellX = c.getInt(cellXIndex);
2090 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002091 folderInfo.spanX = 1;
2092 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002093 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094
Daniel Sandler8802e962010-05-26 16:28:16 -04002095 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002096 if (!checkItemPlacement(occupied, folderInfo)) {
2097 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002098 break;
2099 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2103 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2104 sBgWorkspaceItems.add(folderInfo);
2105 break;
Joe Onorato36115782010-06-17 13:28:48 -04002106 }
Joe Onorato17a89222011-02-08 17:26:11 -08002107
Chris Wrenf4d08112014-01-16 18:13:56 -05002108 if (restored) {
2109 // no special handling required for restored folders
2110 restoredRows.add(id);
2111 }
2112
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2114 sBgFolders.put(folderInfo.id, folderInfo);
2115 break;
2116
2117 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002118 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002120 boolean customWidget = itemType ==
2121 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2122
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002124 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002125 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002126 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002127 user = allUsers.get(serialNumber);
2128 if (user == null) {
2129 itemsToRemove.add(id);
2130 continue;
2131 }
2132
Sunny Goyalff572272014-07-23 13:58:07 -07002133 final ComponentName component =
2134 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002135
Sunny Goyal651077b2014-06-30 14:15:31 -07002136 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002137 final boolean isIdValid = (restoreStatus &
2138 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002139 final boolean wasProviderReady = (restoreStatus &
2140 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002141
Adam Cohen59400422014-03-05 18:07:04 -08002142 final LauncherAppWidgetProviderInfo provider =
2143 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002144 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002145 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002146
2147 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002148 if (!isSafeMode && !customWidget &&
2149 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002150 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002151 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002152
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002154 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 itemsToRemove.add(id);
2156 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002157 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002158 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2159 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002160
Sunny Goyalff572272014-07-23 13:58:07 -07002161 int status = restoreStatus;
2162 if (!wasProviderReady) {
2163 // If provider was not previously ready, update the
2164 // status and UI flag.
2165
2166 // Id would be valid only if the widget restore broadcast was received.
2167 if (isIdValid) {
2168 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2169 } else {
2170 status &= ~LauncherAppWidgetInfo
2171 .FLAG_PROVIDER_NOT_READY;
2172 }
2173 }
2174 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002175 } else {
2176 Log.v(TAG, "Widget restore pending id=" + id
2177 + " appWidgetId=" + appWidgetId
2178 + " status =" + restoreStatus);
2179 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002180 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002182 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002183
2184 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2185 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002186 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002187 // App restore has started. Update the flag
2188 appWidgetInfo.restoreStatus |=
2189 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002190 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002191 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002192 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002193 itemsToRemove.add(id);
2194 continue;
2195 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002196
2197 appWidgetInfo.installProgress =
2198 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002199 }
Sunny Goyalff572272014-07-23 13:58:07 -07002200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 appWidgetInfo.cellX = c.getInt(cellXIndex);
2204 appWidgetInfo.cellY = c.getInt(cellYIndex);
2205 appWidgetInfo.spanX = c.getInt(spanXIndex);
2206 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002207 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002208
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2210 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2211 Log.e(TAG, "Widget found where container != " +
2212 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2213 continue;
2214 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002216 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002218 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2219 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 break;
2221 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002222
Adam Cohen59400422014-03-05 18:07:04 -08002223 if (!customWidget) {
2224 String providerName =
2225 appWidgetInfo.providerName.flattenToString();
2226 if (!providerName.equals(savedProvider) ||
2227 (appWidgetInfo.restoreStatus != restoreStatus)) {
2228 ContentValues values = new ContentValues();
2229 values.put(
2230 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2231 providerName);
2232 values.put(LauncherSettings.Favorites.RESTORED,
2233 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002234 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002235 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002236 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2238 sBgAppWidgets.add(appWidgetInfo);
2239 }
Joe Onorato36115782010-06-17 13:28:48 -04002240 break;
2241 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002243 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002244 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 }
2246 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002247 if (c != null) {
2248 c.close();
2249 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 }
2251
Winson Chungba9c37f2013-08-30 14:11:37 -07002252 // Break early if we've stopped loading
2253 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002254 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002255 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002256 }
2257
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002260 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2261 Utilities.createDbSelectionQuery(
2262 LauncherSettings.Favorites._ID, itemsToRemove), null);
2263 if (DEBUG_LOADERS) {
2264 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2265 LauncherSettings.Favorites._ID, itemsToRemove));
2266 }
2267
2268 // Remove any empty folder
2269 for (long folderId : LauncherAppState.getLauncherProvider()
2270 .deleteEmptyFolders()) {
2271 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2272 sBgFolders.remove(folderId);
2273 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002274 }
2275 }
2276
Chris Wrenf4d08112014-01-16 18:13:56 -05002277 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002278 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002279 ContentValues values = new ContentValues();
2280 values.put(LauncherSettings.Favorites.RESTORED, 0);
2281 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2282 Utilities.createDbSelectionQuery(
2283 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002284 }
2285
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002286 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2287 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002288 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002289 null, sWorker);
2290 }
2291
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002292 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002293
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002294 // Remove any empty screens
2295 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002296 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002297 long screenId = item.screenId;
2298 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2299 unusedScreens.contains(screenId)) {
2300 unusedScreens.remove(screenId);
2301 }
2302 }
2303
2304 // If there are any empty screens remove them, and update.
2305 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002306 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 }
2309
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 if (DEBUG_LOADERS) {
2311 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2312 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002313 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002314 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002316
Sunny Goyale2df0622015-04-24 11:27:00 -07002317 for (int i = 0; i < nScreens; i++) {
2318 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002319 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 line += " | ";
2321 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002322 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002323 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002324 if (x < screen.length && y < screen[x].length) {
2325 line += (screen[x][y] != null) ? "#" : ".";
2326 } else {
2327 line += "!";
2328 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 }
Joe Onorato36115782010-06-17 13:28:48 -04002330 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002333 }
Joe Onorato36115782010-06-17 13:28:48 -04002334 }
Adam Cohene25af792013-06-06 23:08:25 -07002335 }
2336
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002337 /**
2338 * Partially updates the item without any notification. Must be called on the worker thread.
2339 */
2340 private void updateItem(long itemId, ContentValues update) {
2341 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002342 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002343 update,
2344 BaseColumns._ID + "= ?",
2345 new String[]{Long.toString(itemId)});
2346 }
2347
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 /** Filters the set of items who are directly or indirectly (via another container) on the
2349 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002350 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 ArrayList<ItemInfo> allWorkspaceItems,
2352 ArrayList<ItemInfo> currentScreenItems,
2353 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002354 // Purge any null ItemInfos
2355 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2356 while (iter.hasNext()) {
2357 ItemInfo i = iter.next();
2358 if (i == null) {
2359 iter.remove();
2360 }
2361 }
2362
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 // Order the set of items by their containers first, this allows use to walk through the
2364 // list sequentially, build up a list of containers that are in the specified screen,
2365 // as well as all items in those containers.
2366 Set<Long> itemsOnScreen = new HashSet<Long>();
2367 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2368 @Override
2369 public int compare(ItemInfo lhs, ItemInfo rhs) {
2370 return (int) (lhs.container - rhs.container);
2371 }
2372 });
2373 for (ItemInfo info : allWorkspaceItems) {
2374 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002375 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 currentScreenItems.add(info);
2377 itemsOnScreen.add(info.id);
2378 } else {
2379 otherScreenItems.add(info);
2380 }
2381 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2382 currentScreenItems.add(info);
2383 itemsOnScreen.add(info.id);
2384 } else {
2385 if (itemsOnScreen.contains(info.container)) {
2386 currentScreenItems.add(info);
2387 itemsOnScreen.add(info.id);
2388 } else {
2389 otherScreenItems.add(info);
2390 }
2391 }
2392 }
2393 }
2394
2395 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002396 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397 ArrayList<LauncherAppWidgetInfo> appWidgets,
2398 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2399 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400
2401 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002402 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002403 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 currentScreenWidgets.add(widget);
2406 } else {
2407 otherScreenWidgets.add(widget);
2408 }
2409 }
2410 }
2411
2412 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002413 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002414 LongArrayMap<ItemInfo> itemsIdMap,
2415 LongArrayMap<FolderInfo> folders,
2416 LongArrayMap<FolderInfo> currentScreenFolders,
2417 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418
Sunny Goyale2df0622015-04-24 11:27:00 -07002419 int total = folders.size();
2420 for (int i = 0; i < total; i++) {
2421 long id = folders.keyAt(i);
2422 FolderInfo folder = folders.valueAt(i);
2423
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002425 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002427 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 currentScreenFolders.put(id, folder);
2429 } else {
2430 otherScreenFolders.put(id, folder);
2431 }
2432 }
2433 }
2434
2435 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2436 * right) */
2437 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002438 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002439 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002440 final int screenCols = profile.numColumns;
2441 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002443 @Override
2444 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002445 if (lhs.container == rhs.container) {
2446 // Within containers, order by their spatial position in that container
2447 switch ((int) lhs.container) {
2448 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2449 long lr = (lhs.screenId * screenCellCount +
2450 lhs.cellY * screenCols + lhs.cellX);
2451 long rr = (rhs.screenId * screenCellCount +
2452 rhs.cellY * screenCols + rhs.cellX);
2453 return (int) (lr - rr);
2454 }
2455 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2456 // We currently use the screen id as the rank
2457 return (int) (lhs.screenId - rhs.screenId);
2458 }
2459 default:
2460 if (LauncherAppState.isDogfoodBuild()) {
2461 throw new RuntimeException("Unexpected container type when " +
2462 "sorting workspace items.");
2463 }
2464 return 0;
2465 }
2466 } else {
2467 // Between containers, order by hotseat, desktop
2468 return (int) (lhs.container - rhs.container);
2469 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002470 }
2471 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002473
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2475 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002476 final Runnable r = new Runnable() {
2477 @Override
2478 public void run() {
2479 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2480 if (callbacks != null) {
2481 callbacks.bindScreens(orderedScreens);
2482 }
2483 }
2484 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002485 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 }
2487
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2489 final ArrayList<ItemInfo> workspaceItems,
2490 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002491 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002493
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 final boolean postOnMainThread = (deferredBindRunnables != null);
2495
2496 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002497 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002499 final int start = i;
2500 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 final Runnable r = new Runnable() {
2502 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002504 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002506 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2507 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 }
2509 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 };
2511 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002512 synchronized (deferredBindRunnables) {
2513 deferredBindRunnables.add(r);
2514 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002516 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 }
Joe Onorato36115782010-06-17 13:28:48 -04002518 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519
2520 // Bind the folders
2521 if (!folders.isEmpty()) {
2522 final Runnable r = new Runnable() {
2523 public void run() {
2524 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2525 if (callbacks != null) {
2526 callbacks.bindFolders(folders);
2527 }
2528 }
2529 };
2530 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002531 synchronized (deferredBindRunnables) {
2532 deferredBindRunnables.add(r);
2533 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002535 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 }
2537 }
2538
2539 // Bind the widgets, one at a time
2540 N = appWidgets.size();
2541 for (int i = 0; i < N; i++) {
2542 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2543 final Runnable r = new Runnable() {
2544 public void run() {
2545 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2546 if (callbacks != null) {
2547 callbacks.bindAppWidget(widget);
2548 }
2549 }
2550 };
2551 if (postOnMainThread) {
2552 deferredBindRunnables.add(r);
2553 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002554 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002555 }
2556 }
2557 }
2558
2559 /**
2560 * Binds all loaded data to actual views on the main thread.
2561 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002562 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 final long t = SystemClock.uptimeMillis();
2564 Runnable r;
2565
2566 // Don't use these two variables in any of the callback runnables.
2567 // Otherwise we hold a reference to them.
2568 final Callbacks oldCallbacks = mCallbacks.get();
2569 if (oldCallbacks == null) {
2570 // This launcher has exited and nobody bothered to tell us. Just bail.
2571 Log.w(TAG, "LoaderTask running with no launcher");
2572 return;
2573 }
2574
Winson Chung9b9fb962013-11-15 15:39:34 -08002575 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2577 ArrayList<LauncherAppWidgetInfo> appWidgets =
2578 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002579 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002580
2581 final LongArrayMap<FolderInfo> folders;
2582 final LongArrayMap<ItemInfo> itemsIdMap;
2583
Winson Chung2abf94d2012-07-18 18:16:38 -07002584 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585 workspaceItems.addAll(sBgWorkspaceItems);
2586 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002587 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002588
2589 folders = sBgFolders.clone();
2590 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591 }
2592
Derek Prothro7aff3992013-12-10 14:00:37 -05002593 final boolean isLoadingSynchronously =
2594 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002595 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002596 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002597 if (currScreen >= orderedScreenIds.size()) {
2598 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002599 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002600 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002601 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002602 final long currentScreenId = currentScreen < 0
2603 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002604
2605 // Load all the items that are on the current page first (and in the process, unbind
2606 // all the existing workspace items before we call startBinding() below.
2607 unbindWorkspaceItemsOnMainThread();
2608
2609 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2611 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2612 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2613 new ArrayList<LauncherAppWidgetInfo>();
2614 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2615 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002616 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2617 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002618
Winson Chung9b9fb962013-11-15 15:39:34 -08002619 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002620 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002621 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002623 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002624 otherFolders);
2625 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2626 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2627
2628 // Tell the workspace that we're about to start binding items
2629 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002630 public void run() {
2631 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2632 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002634 }
2635 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002636 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002637 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002638
Adam Cohendcd297f2013-06-18 13:13:40 -07002639 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2640
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641 // Load items on the current page
2642 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2643 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002644 if (isLoadingSynchronously) {
2645 r = new Runnable() {
2646 public void run() {
2647 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002648 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002649 callbacks.onPageBoundSynchronously(currentScreen);
2650 }
2651 }
2652 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002653 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002654 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002655
Winson Chung4a2afa32012-07-19 14:53:05 -07002656 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2657 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002658 synchronized (mDeferredBindRunnables) {
2659 mDeferredBindRunnables.clear();
2660 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002662 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663
2664 // Tell the workspace that we're done binding items
2665 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002666 public void run() {
2667 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2668 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002669 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002670 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002671
Winson Chung98e030b2012-05-07 16:01:11 -07002672 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002673 if (DEBUG_LOADERS) {
2674 Log.d(TAG, "bound workspace in "
2675 + (SystemClock.uptimeMillis()-t) + "ms");
2676 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002677
2678 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002679 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002681 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002682 synchronized (mDeferredBindRunnables) {
2683 mDeferredBindRunnables.add(r);
2684 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002685 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002686 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002687 }
Joe Onorato36115782010-06-17 13:28:48 -04002688 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002689
Joe Onorato36115782010-06-17 13:28:48 -04002690 private void loadAndBindAllApps() {
2691 if (DEBUG_LOADERS) {
2692 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2693 }
2694 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002695 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002696 synchronized (LoaderTask.this) {
2697 if (mStopped) {
2698 return;
2699 }
2700 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002701 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002702 synchronized (LoaderTask.this) {
2703 if (mStopped) {
2704 return;
2705 }
2706 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002707 }
Joe Onorato36115782010-06-17 13:28:48 -04002708 } else {
2709 onlyBindAllApps();
2710 }
2711 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002712
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002713 private void updateIconCache() {
2714 // Ignore packages which have a promise icon.
2715 HashSet<String> packagesToIgnore = new HashSet<>();
2716 synchronized (sBgLock) {
2717 for (ItemInfo info : sBgItemsIdMap) {
2718 if (info instanceof ShortcutInfo) {
2719 ShortcutInfo si = (ShortcutInfo) info;
2720 if (si.isPromise() && si.getTargetComponent() != null) {
2721 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2722 }
2723 } else if (info instanceof LauncherAppWidgetInfo) {
2724 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2725 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2726 packagesToIgnore.add(lawi.providerName.getPackageName());
2727 }
2728 }
2729 }
2730 }
2731 mIconCache.updateDbIcons(packagesToIgnore);
2732 }
2733
Joe Onorato36115782010-06-17 13:28:48 -04002734 private void onlyBindAllApps() {
2735 final Callbacks oldCallbacks = mCallbacks.get();
2736 if (oldCallbacks == null) {
2737 // This launcher has exited and nobody bothered to tell us. Just bail.
2738 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2739 return;
2740 }
2741
2742 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002743 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002744 final ArrayList<AppInfo> list
2745 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002746 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002747 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002748 public void run() {
2749 final long t = SystemClock.uptimeMillis();
2750 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2751 if (callbacks != null) {
2752 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002753 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002754 }
2755 if (DEBUG_LOADERS) {
2756 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2757 + (SystemClock.uptimeMillis()-t) + "ms");
2758 }
2759 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002760 };
2761 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002762 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002763 r.run();
2764 } else {
2765 mHandler.post(r);
2766 }
Joe Onorato36115782010-06-17 13:28:48 -04002767 }
2768
Winson Chung64359a52013-07-08 17:17:08 -07002769 private void loadAllApps() {
2770 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002771
Joe Onorato36115782010-06-17 13:28:48 -04002772 final Callbacks oldCallbacks = mCallbacks.get();
2773 if (oldCallbacks == null) {
2774 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002775 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002776 return;
2777 }
2778
Kenny Guyed131872014-04-30 03:02:21 +01002779 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2780
Winson Chung64359a52013-07-08 17:17:08 -07002781 // Clear the list of apps
2782 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002783 for (UserHandleCompat user : profiles) {
2784 // Query for the set of apps
2785 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002786 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002787 if (DEBUG_LOADERS) {
2788 Log.d(TAG, "getActivityList took "
2789 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2790 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2791 }
2792 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002793 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002794 if (apps == null || apps.isEmpty()) {
2795 return;
2796 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002797
Kenny Guyed131872014-04-30 03:02:21 +01002798 // Create the ApplicationInfos
2799 for (int i = 0; i < apps.size(); i++) {
2800 LauncherActivityInfoCompat app = apps.get(i);
2801 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002802 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002803 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002804
Sunny Goyal756a28a2015-04-23 17:07:55 -07002805 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2806 if (heuristic != null) {
2807 runAfterBindCompletes(new Runnable() {
2808
2809 @Override
2810 public void run() {
2811 heuristic.processUserApps(apps);
2812 }
2813 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002814 }
Winson Chung64359a52013-07-08 17:17:08 -07002815 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002816 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002817 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2818 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002819
2820 // Post callback on main thread
2821 mHandler.post(new Runnable() {
2822 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002823
Winson Chung64359a52013-07-08 17:17:08 -07002824 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002825 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002826 if (callbacks != null) {
2827 callbacks.bindAllApplications(added);
2828 if (DEBUG_LOADERS) {
2829 Log.d(TAG, "bound " + added.size() + " apps in "
2830 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2831 }
2832 } else {
2833 Log.i(TAG, "not binding apps: no Launcher activity");
2834 }
2835 }
2836 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002837 // Cleanup any data stored for a deleted user.
2838 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002839
Hyunyoung Song219d0482015-05-07 12:51:42 -07002840 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2841 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002842 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002843 Log.d(TAG, "Icons processed in "
2844 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002845 }
2846 }
2847
2848 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002849 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002850 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002851 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2852 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2853 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2854 }
Joe Onorato36115782010-06-17 13:28:48 -04002855 }
2856 }
2857
Sunny Goyal75b0f552015-05-20 21:57:06 -07002858 /**
2859 * Called when the icons for packages have been updated in the icon cache.
2860 */
2861 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2862 final Callbacks callbacks = getCallback();
2863 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2864 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2865
2866 // If any package icon has changed (app was updated while launcher was dead),
2867 // update the corresponding shortcuts.
2868 synchronized (sBgLock) {
2869 for (ItemInfo info : sBgItemsIdMap) {
2870 if (info instanceof ShortcutInfo && user.equals(info.user)
2871 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2872 ShortcutInfo si = (ShortcutInfo) info;
2873 ComponentName cn = si.getTargetComponent();
2874 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2875 si.updateIcon(mIconCache);
2876 updatedShortcuts.add(si);
2877 }
2878 }
2879 }
2880 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2881 }
2882
2883 if (!updatedShortcuts.isEmpty()) {
2884 final UserHandleCompat userFinal = user;
2885 mHandler.post(new Runnable() {
2886
2887 public void run() {
2888 Callbacks cb = getCallback();
2889 if (cb != null && callbacks == cb) {
2890 cb.bindShortcutsChanged(updatedShortcuts,
2891 new ArrayList<ShortcutInfo>(), userFinal);
2892 }
2893 }
2894 });
2895 }
2896
2897 if (!updatedApps.isEmpty()) {
2898 mHandler.post(new Runnable() {
2899
2900 public void run() {
2901 Callbacks cb = getCallback();
2902 if (cb != null && callbacks == cb) {
2903 cb.bindAppsUpdated(updatedApps);
2904 }
2905 }
2906 });
2907 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002908
2909 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2910 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002911 }
2912
Joe Onorato36115782010-06-17 13:28:48 -04002913 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002914 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002915 }
2916
Adam Cohen091440a2015-03-18 14:16:05 -07002917 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002918
2919 @Override
2920 public void onReceive(Context context, Intent intent) {
2921 synchronized (sBgLock) {
2922 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2923 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002924 final PackageManager manager = context.getPackageManager();
2925 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2926 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002927 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2928 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002929 packagesRemoved.clear();
2930 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002931 for (String pkg : entry.getValue()) {
2932 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002933 boolean packageOnSdcard = launcherApps.isAppEnabled(
2934 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2935 if (packageOnSdcard) {
2936 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2937 packagesUnavailable.add(pkg);
2938 } else {
2939 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2940 packagesRemoved.add(pkg);
2941 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002942 }
2943 }
2944 if (!packagesRemoved.isEmpty()) {
2945 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2946 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2947 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002948 if (!packagesUnavailable.isEmpty()) {
2949 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2950 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2951 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002952 }
Sunny Goyal34942622014-08-29 17:20:55 -07002953 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002954 }
2955 }
2956 }
2957
Joe Onorato36115782010-06-17 13:28:48 -04002958 private class PackageUpdatedTask implements Runnable {
2959 int mOp;
2960 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002961 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002962
2963 public static final int OP_NONE = 0;
2964 public static final int OP_ADD = 1;
2965 public static final int OP_UPDATE = 2;
2966 public static final int OP_REMOVE = 3; // uninstlled
2967 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2968
2969
Kenny Guyed131872014-04-30 03:02:21 +01002970 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002971 mOp = op;
2972 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002973 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002974 }
2975
2976 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002977 if (!mHasLoaderCompletedOnce) {
2978 // Loader has not yet run.
2979 return;
2980 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002981 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002982
2983 final String[] packages = mPackages;
2984 final int N = packages.length;
2985 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002986 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002987 for (int i=0; i<N; i++) {
2988 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002989 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002990 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002991 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002992
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002993 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2994 if (heuristic != null) {
2995 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002996 }
Joe Onorato36115782010-06-17 13:28:48 -04002997 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002998 }
Joe Onorato36115782010-06-17 13:28:48 -04002999 case OP_UPDATE:
3000 for (int i=0; i<N; i++) {
3001 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003002 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003003 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003004 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003005 }
3006 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003007 case OP_REMOVE: {
3008 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3009 if (heuristic != null) {
3010 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003011 }
Joe Onorato36115782010-06-17 13:28:48 -04003012 for (int i=0; i<N; i++) {
3013 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003014 mIconCache.removeIconsForPkg(packages[i], mUser);
3015 }
3016 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003017 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003018 case OP_UNAVAILABLE:
3019 for (int i=0; i<N; i++) {
3020 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3021 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003022 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003023 }
3024 break;
3025 }
3026
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003027 ArrayList<AppInfo> added = null;
3028 ArrayList<AppInfo> modified = null;
3029 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003030
Adam Cohen487f7dd2012-06-28 18:12:10 -07003031 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003032 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003033 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003034 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003035 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003036 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003037 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003038 }
Winson Chung5d55f332012-07-16 20:45:03 -07003039 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003040 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003041 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003042 }
3043
Sunny Goyale0f58d72014-11-10 18:05:31 -08003044 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003045 if (callbacks == null) {
3046 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3047 return;
3048 }
3049
Sunny Goyal4390ace2014-10-13 11:33:11 -07003050 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3051 new HashMap<ComponentName, AppInfo>();
3052
Joe Onorato36115782010-06-17 13:28:48 -04003053 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003054 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003055 for (AppInfo ai : added) {
3056 addedOrUpdatedApps.put(ai.componentName, ai);
3057 }
Joe Onorato36115782010-06-17 13:28:48 -04003058 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003059
Joe Onorato36115782010-06-17 13:28:48 -04003060 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003061 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003062 for (AppInfo ai : modified) {
3063 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003064 }
3065
Joe Onorato36115782010-06-17 13:28:48 -04003066 mHandler.post(new Runnable() {
3067 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003068 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003069 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003070 callbacks.bindAppsUpdated(modifiedFinal);
3071 }
3072 }
3073 });
3074 }
Winson Chung83892cc2013-05-01 16:53:33 -07003075
Sunny Goyal4390ace2014-10-13 11:33:11 -07003076 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003077 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003078 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3079 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3080 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3081
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003083 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003084 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003085 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3086 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 boolean infoUpdated = false;
3088 boolean shortcutUpdated = false;
3089
3090 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003091 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003092 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003093 Bitmap icon = Utilities.createIconBitmap(
3094 si.iconResource.packageName,
3095 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003096 if (icon != null) {
3097 si.setIcon(icon);
3098 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003100 }
3101 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102
3103 ComponentName cn = si.getTargetComponent();
3104 if (cn != null && packageSet.contains(cn.getPackageName())) {
3105 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3106
3107 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3109 // Auto install icon
3110 PackageManager pm = context.getPackageManager();
3111 ResolveInfo matched = pm.resolveActivity(
3112 new Intent(Intent.ACTION_MAIN)
3113 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3114 PackageManager.MATCH_DEFAULT_ONLY);
3115 if (matched == null) {
3116 // Try to find the best match activity.
3117 Intent intent = pm.getLaunchIntentForPackage(
3118 cn.getPackageName());
3119 if (intent != null) {
3120 cn = intent.getComponent();
3121 appInfo = addedOrUpdatedApps.get(cn);
3122 }
3123
3124 if ((intent == null) || (appInfo == null)) {
3125 removedShortcuts.add(si);
3126 continue;
3127 }
3128 si.promisedIntent = intent;
3129 }
3130 }
3131
3132 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003133 if (appInfo != null) {
3134 si.flags = appInfo.flags;
3135 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136
Sunny Goyal756adbc2015-04-16 15:20:51 -07003137 si.intent = si.promisedIntent;
3138 si.promisedIntent = null;
3139 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003140 infoUpdated = true;
3141 si.updateIcon(mIconCache);
3142 }
3143
3144 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3145 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3146 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003147 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 si.contentDescription = appInfo.contentDescription;
3149 infoUpdated = true;
3150 }
3151
3152 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3153 // Since package was just updated, the target must be available now.
3154 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3155 shortcutUpdated = true;
3156 }
3157 }
3158
3159 if (infoUpdated || shortcutUpdated) {
3160 updatedShortcuts.add(si);
3161 }
3162 if (infoUpdated) {
3163 updateItemInDatabase(context, si);
3164 }
3165 } else if (info instanceof LauncherAppWidgetInfo) {
3166 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3167 if (mUser.equals(widgetInfo.user)
3168 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3169 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3170 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3171 widgets.add(widgetInfo);
3172 updateItemInDatabase(context, widgetInfo);
3173 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003174 }
3175 }
3176 }
3177
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3179 mHandler.post(new Runnable() {
3180
3181 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003182 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003183 if (callbacks == cb && cb != null) {
3184 callbacks.bindShortcutsChanged(
3185 updatedShortcuts, removedShortcuts, mUser);
3186 }
3187 }
3188 });
3189 if (!removedShortcuts.isEmpty()) {
3190 deleteItemsFromDatabase(context, removedShortcuts);
3191 }
3192 }
3193 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003194 mHandler.post(new Runnable() {
3195 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003196 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003197 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003199 }
3200 }
3201 });
3202 }
3203 }
3204
Winson Chungdf95eb12013-10-16 14:57:07 -07003205 final ArrayList<String> removedPackageNames =
3206 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003207 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003208 // Mark all packages in the broadcast to be removed
3209 removedPackageNames.addAll(Arrays.asList(packages));
3210 } else if (mOp == OP_UPDATE) {
3211 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003212 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003213 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003214 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003215 }
3216 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003217 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003218
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003220 final int removeReason;
3221 if (mOp == OP_UNAVAILABLE) {
3222 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3223 } else {
3224 // Remove all the components associated with this package
3225 for (String pn : removedPackageNames) {
3226 deletePackageFromDatabase(context, pn, mUser);
3227 }
3228 // Remove all the specific components
3229 for (AppInfo a : removedApps) {
3230 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3231 deleteItemsFromDatabase(context, infos);
3232 }
3233 removeReason = 0;
3234 }
3235
Winson Chungdf95eb12013-10-16 14:57:07 -07003236 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003237 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003238 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003239 mHandler.post(new Runnable() {
3240 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003241 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003242 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003243 callbacks.bindComponentsRemoved(
3244 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003245 }
3246 }
3247 });
Joe Onoratobe386092009-11-17 17:32:16 -08003248 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003249
3250 // onProvidersChanged method (API >= 17) already refreshed the widget list
3251 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3252
Adam Cohen4caf2982013-08-20 18:54:31 -07003253 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003254 mHandler.post(new Runnable() {
3255 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003256 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003257 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003258 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003259 }
3260 }
3261 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 }
3263 }
3264
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003265 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3266 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003267 ArrayList<LauncherAppWidgetProviderInfo> results =
3268 new ArrayList<LauncherAppWidgetProviderInfo>();
3269 try {
3270 synchronized (sBgLock) {
3271 if (sBgWidgetProviders == null || refresh) {
3272 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3273 = new HashMap<>();
3274 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3275 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003276
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003277 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3278 for (AppWidgetProviderInfo pInfo : widgets) {
3279 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3280 UserHandleCompat user = wm.getUser(info);
3281 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3282 }
Robin Lee26ace122015-03-16 19:41:43 +00003283
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003284 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3285 for (CustomAppWidget widget : customWidgets) {
3286 info = new LauncherAppWidgetProviderInfo(context, widget);
3287 UserHandleCompat user = wm.getUser(info);
3288 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3289 }
3290 // Replace the global list at the very end, so that if there is an exception,
3291 // previously loaded provider list is used.
3292 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003293 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003294 results.addAll(sBgWidgetProviders.values());
3295 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003296 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003297 } catch (Exception e) {
3298 if (e.getCause() instanceof TransactionTooLargeException) {
3299 // the returned value may be incomplete and will not be refreshed until the next
3300 // time Launcher starts.
3301 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3302 // onResume is called to refresh the widget provider list.
3303 synchronized (sBgLock) {
3304 if (sBgWidgetProviders != null) {
3305 results.addAll(sBgWidgetProviders.values());
3306 }
3307 return results;
3308 }
3309 } else {
3310 throw e;
3311 }
Adam Cohen59400422014-03-05 18:07:04 -08003312 }
3313 }
3314
Robin Lee26ace122015-03-16 19:41:43 +00003315 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3316 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003317 synchronized (sBgLock) {
3318 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003319 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003320 }
Robin Lee26ace122015-03-16 19:41:43 +00003321 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003322 }
3323 }
3324
Hyunyoung Song227239e2015-05-04 18:17:35 -07003325 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3326 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003327
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003328 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003329 @Override
3330 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003331 updateWidgetsModel(context, refresh);
3332 final WidgetsModel model = mBgWidgetsModel.clone();
3333
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003334 mHandler.post(new Runnable() {
3335 @Override
3336 public void run() {
3337 Callbacks cb = getCallback();
3338 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003339 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003340 }
3341 }
3342 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003343 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003344 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3345 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003346 }
3347 });
3348 }
3349
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003350 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003351 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003352 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003353 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003354 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003355 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003356 PackageManager packageManager = context.getPackageManager();
3357 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003358 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003359 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3360 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003361 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003362 }
3363
Adam Cohen091440a2015-03-18 14:16:05 -07003364 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003365 UserHandleCompat user) {
3366 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3367 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003368 }
Adam Cohen556f6132014-01-15 15:18:08 -08003369
Kenny Guyed131872014-04-30 03:02:21 +01003370 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3371 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003372 if (cn == null) {
3373 return false;
3374 }
Kenny Guyed131872014-04-30 03:02:21 +01003375 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3376 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003377 return false;
3378 }
Kenny Guyed131872014-04-30 03:02:21 +01003379 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003380 }
3381
Adam Cohena28b78e2014-05-20 17:03:04 -07003382 public static boolean isValidPackage(Context context, String packageName,
3383 UserHandleCompat user) {
3384 if (packageName == null) {
3385 return false;
3386 }
3387 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3388 return launcherApps.isPackageEnabledForProfile(packageName, user);
3389 }
3390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003392 * Make an ShortcutInfo object for a restored application or shortcut item that points
3393 * to a package that is not yet installed on the system.
3394 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003395 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003396 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003397 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003398 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003399
3400 Bitmap icon = iconInfo.loadIcon(c, info, context);
3401 // the fallback icon
3402 if (icon == null) {
3403 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3404 } else {
3405 info.setIcon(icon);
3406 }
Sunny Goyal34942622014-08-29 17:20:55 -07003407
3408 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003409 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003410 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003411 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003412 }
Sunny Goyal34942622014-08-29 17:20:55 -07003413 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3414 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003415 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003416 }
Sunny Goyal34942622014-08-29 17:20:55 -07003417 } else {
3418 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3419 }
3420
Winson Chung82b016c2015-05-08 17:00:10 -07003421 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003422 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003423 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003424 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003425 return info;
3426 }
3427
3428 /**
3429 * Make an Intent object for a restored application or shortcut item that points
3430 * to the market page for the item.
3431 */
Adam Cohen091440a2015-03-18 14:16:05 -07003432 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003433 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003434 return getMarketIntent(componentName.getPackageName());
3435 }
3436
3437 static Intent getMarketIntent(String packageName) {
3438 return new Intent(Intent.ACTION_VIEW)
3439 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 .scheme("market")
3441 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003442 .appendQueryParameter("id", packageName)
3443 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003444 }
3445
3446 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003447 * Make an ShortcutInfo object for a shortcut that is an application.
3448 *
3449 * If c is not null, then it will be used to fill in missing data like the title and icon.
3450 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003451 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003452 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003453 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003454 if (user == null) {
3455 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003456 return null;
3457 }
3458
Kenny Guyed131872014-04-30 03:02:21 +01003459 ComponentName componentName = intent.getComponent();
3460 if (componentName == null) {
3461 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3462 return null;
3463 }
3464
3465 Intent newIntent = new Intent(intent.getAction(), null);
3466 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3467 newIntent.setComponent(componentName);
3468 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003469 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003470 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3471 return null;
3472 }
3473
3474 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003475 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003476 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3477 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3478 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003479 }
3480
Joe Onorato56d82912010-03-07 14:32:10 -05003481 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003482 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003483 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003484 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003485
Joe Onorato56d82912010-03-07 14:32:10 -05003486 // fall back to the class name of the activity
3487 if (info.title == null) {
3488 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003489 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003492 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003493 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003494 if (lai != null) {
3495 info.flags = AppInfo.initFlags(lai);
3496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003498 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003499
Sunny Goyale2df0622015-04-24 11:27:00 -07003500 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003501 ItemInfoFilter f) {
3502 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3503 for (ItemInfo i : infos) {
3504 if (i instanceof ShortcutInfo) {
3505 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003506 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003507 if (cn != null && f.filterItem(null, info, cn)) {
3508 filtered.add(info);
3509 }
3510 } else if (i instanceof FolderInfo) {
3511 FolderInfo info = (FolderInfo) i;
3512 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003513 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003514 if (cn != null && f.filterItem(info, s, cn)) {
3515 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003516 }
3517 }
Winson Chung64359a52013-07-08 17:17:08 -07003518 } else if (i instanceof LauncherAppWidgetInfo) {
3519 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3520 ComponentName cn = info.providerName;
3521 if (cn != null && f.filterItem(null, info, cn)) {
3522 filtered.add(info);
3523 }
Winson Chung8a435102012-08-30 17:16:53 -07003524 }
3525 }
Winson Chung64359a52013-07-08 17:17:08 -07003526 return new ArrayList<ItemInfo>(filtered);
3527 }
3528
Adam Cohen091440a2015-03-18 14:16:05 -07003529 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003530 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003531 ItemInfoFilter filter = new ItemInfoFilter() {
3532 @Override
3533 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003534 if (info.user == null) {
3535 return cn.equals(cname);
3536 } else {
3537 return cn.equals(cname) && info.user.equals(user);
3538 }
Winson Chung64359a52013-07-08 17:17:08 -07003539 }
3540 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003541 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003542 }
3543
Sunny Goyal1a745e82014-10-02 15:58:31 -07003544 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003545 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003546 */
Adam Cohen091440a2015-03-18 14:16:05 -07003547 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003548 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003549 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003550 // Non-app shortcuts are only supported for current user.
3551 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003553
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003554 // TODO: If there's an explicit component and we can't install that, delete it.
3555
Winson Chung82b016c2015-05-08 17:00:10 -07003556 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003557
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003558 Bitmap icon = iconInfo.loadIcon(c, info, context);
3559 // the fallback icon
3560 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003561 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003562 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003563 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003564 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 return info;
3566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567
Sunny Goyal2350bc92014-10-14 16:42:54 -07003568 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003569 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3570 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3571 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3572
Adam Cohend9198822011-11-22 16:42:47 -08003573 if (intent == null) {
3574 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3575 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3576 return null;
3577 }
3578
Joe Onorato0589f0f2010-02-08 13:44:00 -08003579 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003580 boolean customIcon = false;
3581 ShortcutIconResource iconResource = null;
3582
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003583 if (bitmap instanceof Bitmap) {
3584 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003585 customIcon = true;
3586 } else {
3587 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003588 if (extra instanceof ShortcutIconResource) {
3589 iconResource = (ShortcutIconResource) extra;
3590 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003591 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003592 }
3593 }
3594
Michael Jurkac9d95c52011-08-29 14:03:34 -07003595 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003596
Kenny Guyed131872014-04-30 03:02:21 +01003597 // Only support intents for current user for now. Intents sent from other
3598 // users wouldn't get here without intent forwarding anyway.
3599 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003600 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003601 icon = mIconCache.getDefaultIcon(info.user);
3602 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003603 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003604 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003605
Winson Chung82b016c2015-05-08 17:00:10 -07003606 info.title = Utilities.trim(name);
3607 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003608 info.intent = intent;
3609 info.customIcon = customIcon;
3610 info.iconResource = iconResource;
3611
3612 return info;
3613 }
3614
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003616 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 * or make a new one.
3618 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003619 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 // See if a placeholder was created for us already
3621 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003622 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003624 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 folders.put(id, folderInfo);
3626 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003627 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003628 }
3629
Joe Onoratobe386092009-11-17 17:32:16 -08003630
Sunny Goyal651077b2014-06-30 14:15:31 -07003631 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3632 return (provider != null) && (provider.provider != null)
3633 && (provider.provider.getPackageName() != null);
3634 }
3635
Joe Onoratobe386092009-11-17 17:32:16 -08003636 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003637 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003638 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3639 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3640 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3641 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003642 if (mLoaderTask != null) {
3643 mLoaderTask.dumpState();
3644 } else {
3645 Log.d(TAG, "mLoaderTask=null");
3646 }
Joe Onoratobe386092009-11-17 17:32:16 -08003647 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003648
3649 public Callbacks getCallback() {
3650 return mCallbacks != null ? mCallbacks.get() : null;
3651 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003652
3653 /**
3654 * @return {@link FolderInfo} if its already loaded.
3655 */
3656 public FolderInfo findFolderById(Long folderId) {
3657 synchronized (sBgLock) {
3658 return sBgFolders.get(folderId);
3659 }
3660 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003661
3662 /**
3663 * @return the looper for the worker thread which can be used to start background tasks.
3664 */
3665 public static Looper getWorkerLooper() {
3666 return sWorkerThread.getLooper();
3667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003668}