blob: 65a0232f52a8e4d5b11f59793c584b214fb7d535 [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);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001747 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001748
Winson Chung892c74d2013-08-22 16:15:50 -07001749 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001750 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1751 int countX = (int) profile.numColumns;
1752 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001753
Dan Sandlerd5024042014-01-09 15:01:33 -05001754 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1755 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1756 LauncherAppState.getLauncherProvider().deleteDatabase();
1757 }
1758
1759 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1760 // append the user's Launcher2 shortcuts
1761 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1762 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1763 } else {
1764 // Make sure the default workspace is loaded
1765 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001766 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001767 }
Adam Cohene25af792013-06-06 23:08:25 -07001768
Winson Chung2abf94d2012-07-18 18:16:38 -07001769 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001770 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001771 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001772 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001775 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001776 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001777 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001778 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001779
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 // +1 for the hotseat (it can be larger than the workspace)
1781 // Load workspace in reverse order to ensure that latest items are loaded first (and
1782 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001783 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 try {
1786 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1787 final int intentIndex = c.getColumnIndexOrThrow
1788 (LauncherSettings.Favorites.INTENT);
1789 final int titleIndex = c.getColumnIndexOrThrow
1790 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 final int containerIndex = c.getColumnIndexOrThrow(
1792 LauncherSettings.Favorites.CONTAINER);
1793 final int itemTypeIndex = c.getColumnIndexOrThrow(
1794 LauncherSettings.Favorites.ITEM_TYPE);
1795 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1796 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001797 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 final int screenIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.SCREEN);
1801 final int cellXIndex = c.getColumnIndexOrThrow
1802 (LauncherSettings.Favorites.CELLX);
1803 final int cellYIndex = c.getColumnIndexOrThrow
1804 (LauncherSettings.Favorites.CELLY);
1805 final int spanXIndex = c.getColumnIndexOrThrow
1806 (LauncherSettings.Favorites.SPANX);
1807 final int spanYIndex = c.getColumnIndexOrThrow(
1808 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001809 final int rankIndex = c.getColumnIndexOrThrow(
1810 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001811 final int restoredIndex = c.getColumnIndexOrThrow(
1812 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001813 final int profileIdIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001815 final int optionsIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001817 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818
Sunny Goyal7f834d22015-04-21 10:10:23 -07001819 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1820 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1821 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1822 }
1823
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 ShortcutInfo info;
1825 String intentDescription;
1826 LauncherAppWidgetInfo appWidgetInfo;
1827 int container;
1828 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001829 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001831 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 while (!mStopped && c.moveToNext()) {
1834 try {
1835 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001836 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001837 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001838 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 switch (itemType) {
1841 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1842 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001843 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001845 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001846 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001847 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001848 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001849 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001850 if (user == null) {
1851 // User has been deleted remove the item.
1852 itemsToRemove.add(id);
1853 continue;
1854 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 try {
1856 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001857 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001858 if (cn != null && cn.getPackageName() != null) {
1859 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1860 cn.getPackageName(), user);
1861 boolean validComponent = validPkg &&
1862 launcherApps.isActivityEnabledForProfile(cn, user);
1863
1864 if (validComponent) {
1865 if (restored) {
1866 // no special handling necessary for this item
1867 restoredRows.add(id);
1868 restored = false;
1869 }
1870 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001871 intent = null;
1872 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1873 // We allow auto install apps to have their intent
1874 // updated after an install.
1875 intent = manager.getLaunchIntentForPackage(
1876 cn.getPackageName());
1877 if (intent != null) {
1878 ContentValues values = new ContentValues();
1879 values.put(LauncherSettings.Favorites.INTENT,
1880 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001881 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001882 }
1883 }
1884
1885 if (intent == null) {
1886 // The app is installed but the component is no
1887 // longer available.
1888 Launcher.addDumpLog(TAG,
1889 "Invalid component removed: " + cn, true);
1890 itemsToRemove.add(id);
1891 continue;
1892 } else {
1893 // no special handling necessary for this item
1894 restoredRows.add(id);
1895 restored = false;
1896 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001897 } else if (restored) {
1898 // Package is not yet available but might be
1899 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001900 Launcher.addDumpLog(TAG,
1901 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001902
1903 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1904 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001905 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001906 // App restore has started. Update the flag
1907 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1908 ContentValues values = new ContentValues();
1909 values.put(LauncherSettings.Favorites.RESTORED,
1910 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001911 updateItem(id, values);
1912 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1913 // This is a common app. Try to replace this.
1914 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1915 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1916 if (parser.findDefaultApp()) {
1917 // Default app found. Replace it.
1918 intent = parser.parsedIntent;
1919 cn = intent.getComponent();
1920 ContentValues values = parser.parsedValues;
1921 values.put(LauncherSettings.Favorites.RESTORED, 0);
1922 updateItem(id, values);
1923 restored = false;
1924 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001925
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001926 } else if (REMOVE_UNRESTORED_ICONS) {
1927 Launcher.addDumpLog(TAG,
1928 "Unrestored package removed: " + cn, true);
1929 itemsToRemove.add(id);
1930 continue;
1931 }
Sunny Goyal94485362014-09-18 16:13:58 -07001932 } else if (REMOVE_UNRESTORED_ICONS) {
1933 Launcher.addDumpLog(TAG,
1934 "Unrestored package removed: " + cn, true);
1935 itemsToRemove.add(id);
1936 continue;
1937 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001938 } else if (launcherApps.isAppEnabled(
1939 manager, cn.getPackageName(),
1940 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1941 // Package is present but not available.
1942 allowMissingTarget = true;
1943 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1944 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001945 // SdCard is not ready yet. Package might get available,
1946 // once it is ready.
1947 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1948 + " (check again later)", true);
1949 HashSet<String> pkgs = sPendingPackages.get(user);
1950 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001951 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001952 sPendingPackages.put(user, pkgs);
1953 }
1954 pkgs.add(cn.getPackageName());
1955 allowMissingTarget = true;
1956 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001957
1958 } else {
1959 // Do not wait for external media load anymore.
1960 // Log the invalid package, and remove it
1961 Launcher.addDumpLog(TAG,
1962 "Invalid package removed: " + cn, true);
1963 itemsToRemove.add(id);
1964 continue;
Winson Chungee055712013-07-30 14:46:24 -07001965 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001966 } else if (cn == null) {
1967 // For shortcuts with no component, keep them as they are
1968 restoredRows.add(id);
1969 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001970 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001972 Launcher.addDumpLog(TAG,
1973 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 continue;
1975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976
Sunny Goyal34b65272015-03-11 16:56:52 -07001977 boolean useLowResIcon = container >= 0 &&
1978 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1979
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001980 if (itemReplaced) {
1981 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001982 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001983 cursorIconInfo.iconIndex, titleIndex,
1984 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001985 } else {
1986 // Don't replace items for other profiles.
1987 itemsToRemove.add(id);
1988 continue;
1989 }
1990 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001991 if (user.equals(UserHandleCompat.myUserHandle())) {
1992 Launcher.addDumpLog(TAG,
1993 "constructing info for partially restored package",
1994 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001995 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001996 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001997 intent = getRestoredItemIntent(c, context, intent);
1998 } else {
1999 // Don't restore items for other profiles.
2000 itemsToRemove.add(id);
2001 continue;
2002 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002003 } else if (itemType ==
2004 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002005 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002006 cursorIconInfo.iconIndex, titleIndex,
2007 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002009 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 // App shortcuts that used to be automatically added to Launcher
2012 // didn't always have the correct intent flags set, so do that
2013 // here
2014 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002015 intent.getCategories() != null &&
2016 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002017 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 intent.addFlags(
2019 Intent.FLAG_ACTIVITY_NEW_TASK |
2020 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2021 }
Michael Jurka96879562012-03-22 05:54:33 -07002022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002025 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 info.cellX = c.getInt(cellXIndex);
2030 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002031 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002032 info.spanX = 1;
2033 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002034 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002035 if (info.promisedIntent != null) {
2036 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2037 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002038 info.isDisabled = disabledState;
2039 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2040 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2041 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002042
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002044 if (!checkItemPlacement(occupied, info)) {
2045 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 break;
2047 }
2048
Sunny Goyal756adbc2015-04-16 15:20:51 -07002049 if (restored) {
2050 ComponentName cn = info.getTargetComponent();
2051 if (cn != null) {
2052 Integer progress = installingPkgs.get(cn.getPackageName());
2053 if (progress != null) {
2054 info.setInstallProgress(progress);
2055 } else {
2056 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2057 }
2058 }
2059 }
2060
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 switch (container) {
2062 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2063 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2064 sBgWorkspaceItems.add(info);
2065 break;
2066 default:
2067 // Item is in a user folder
2068 FolderInfo folderInfo =
2069 findOrMakeFolder(sBgFolders, container);
2070 folderInfo.add(info);
2071 break;
2072 }
2073 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002074 } else {
2075 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 }
2077 break;
2078
2079 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2080 id = c.getLong(idIndex);
2081 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2082
Sunny Goyala508e4f2015-05-21 09:33:57 -07002083 // Do not trim the folder label, as is was set by the user.
2084 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 folderInfo.cellX = c.getInt(cellXIndex);
2089 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002090 folderInfo.spanX = 1;
2091 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002092 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093
Daniel Sandler8802e962010-05-26 16:28:16 -04002094 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002095 if (!checkItemPlacement(occupied, folderInfo)) {
2096 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002097 break;
2098 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002099
Joe Onorato9c1289c2009-08-17 11:03:03 -04002100 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2102 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2103 sBgWorkspaceItems.add(folderInfo);
2104 break;
Joe Onorato36115782010-06-17 13:28:48 -04002105 }
Joe Onorato17a89222011-02-08 17:26:11 -08002106
Chris Wrenf4d08112014-01-16 18:13:56 -05002107 if (restored) {
2108 // no special handling required for restored folders
2109 restoredRows.add(id);
2110 }
2111
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2113 sBgFolders.put(folderInfo.id, folderInfo);
2114 break;
2115
2116 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002117 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002119 boolean customWidget = itemType ==
2120 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2121
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002123 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002124 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002125 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002126 user = allUsers.get(serialNumber);
2127 if (user == null) {
2128 itemsToRemove.add(id);
2129 continue;
2130 }
2131
Sunny Goyalff572272014-07-23 13:58:07 -07002132 final ComponentName component =
2133 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002134
Sunny Goyal651077b2014-06-30 14:15:31 -07002135 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002136 final boolean isIdValid = (restoreStatus &
2137 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002138 final boolean wasProviderReady = (restoreStatus &
2139 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002140
Adam Cohen59400422014-03-05 18:07:04 -08002141 final LauncherAppWidgetProviderInfo provider =
2142 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002143 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002144 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002145
2146 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002147 if (!isSafeMode && !customWidget &&
2148 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002149 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002150 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002151
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002153 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 itemsToRemove.add(id);
2155 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002156 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002157 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2158 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002159
Sunny Goyalff572272014-07-23 13:58:07 -07002160 int status = restoreStatus;
2161 if (!wasProviderReady) {
2162 // If provider was not previously ready, update the
2163 // status and UI flag.
2164
2165 // Id would be valid only if the widget restore broadcast was received.
2166 if (isIdValid) {
2167 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2168 } else {
2169 status &= ~LauncherAppWidgetInfo
2170 .FLAG_PROVIDER_NOT_READY;
2171 }
2172 }
2173 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002174 } else {
2175 Log.v(TAG, "Widget restore pending id=" + id
2176 + " appWidgetId=" + appWidgetId
2177 + " status =" + restoreStatus);
2178 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002179 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002180 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002181 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002182
2183 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2184 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002185 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002186 // App restore has started. Update the flag
2187 appWidgetInfo.restoreStatus |=
2188 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002189 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002190 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002191 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002192 itemsToRemove.add(id);
2193 continue;
2194 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002195
2196 appWidgetInfo.installProgress =
2197 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002198 }
Sunny Goyalff572272014-07-23 13:58:07 -07002199
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 appWidgetInfo.cellX = c.getInt(cellXIndex);
2203 appWidgetInfo.cellY = c.getInt(cellYIndex);
2204 appWidgetInfo.spanX = c.getInt(spanXIndex);
2205 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002206 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002207
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2209 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2210 Log.e(TAG, "Widget found where container != " +
2211 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2212 continue;
2213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002215 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002217 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2218 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 break;
2220 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002221
Adam Cohen59400422014-03-05 18:07:04 -08002222 if (!customWidget) {
2223 String providerName =
2224 appWidgetInfo.providerName.flattenToString();
2225 if (!providerName.equals(savedProvider) ||
2226 (appWidgetInfo.restoreStatus != restoreStatus)) {
2227 ContentValues values = new ContentValues();
2228 values.put(
2229 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2230 providerName);
2231 values.put(LauncherSettings.Favorites.RESTORED,
2232 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002233 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002234 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002235 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2237 sBgAppWidgets.add(appWidgetInfo);
2238 }
Joe Onorato36115782010-06-17 13:28:48 -04002239 break;
2240 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002242 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002243 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 }
2245 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002246 if (c != null) {
2247 c.close();
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 }
2250
Winson Chungba9c37f2013-08-30 14:11:37 -07002251 // Break early if we've stopped loading
2252 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002253 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002254 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002255 }
2256
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002259 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2260 Utilities.createDbSelectionQuery(
2261 LauncherSettings.Favorites._ID, itemsToRemove), null);
2262 if (DEBUG_LOADERS) {
2263 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2264 LauncherSettings.Favorites._ID, itemsToRemove));
2265 }
2266
2267 // Remove any empty folder
2268 for (long folderId : LauncherAppState.getLauncherProvider()
2269 .deleteEmptyFolders()) {
2270 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2271 sBgFolders.remove(folderId);
2272 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002273 }
2274 }
2275
Chris Wrenf4d08112014-01-16 18:13:56 -05002276 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002277 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002278 ContentValues values = new ContentValues();
2279 values.put(LauncherSettings.Favorites.RESTORED, 0);
2280 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2281 Utilities.createDbSelectionQuery(
2282 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002283 }
2284
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002285 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2286 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002287 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002288 null, sWorker);
2289 }
2290
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002291 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002292
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002293 // Remove any empty screens
2294 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002295 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002296 long screenId = item.screenId;
2297 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2298 unusedScreens.contains(screenId)) {
2299 unusedScreens.remove(screenId);
2300 }
2301 }
2302
2303 // If there are any empty screens remove them, and update.
2304 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002305 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 }
2308
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 if (DEBUG_LOADERS) {
2310 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2311 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002313 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002315
Sunny Goyale2df0622015-04-24 11:27:00 -07002316 for (int i = 0; i < nScreens; i++) {
2317 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002318 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 line += " | ";
2320 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002321 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002322 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002323 if (x < screen.length && y < screen[x].length) {
2324 line += (screen[x][y] != null) ? "#" : ".";
2325 } else {
2326 line += "!";
2327 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 }
Joe Onorato36115782010-06-17 13:28:48 -04002329 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002331 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 }
Joe Onorato36115782010-06-17 13:28:48 -04002333 }
Adam Cohene25af792013-06-06 23:08:25 -07002334 }
2335
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002336 /**
2337 * Partially updates the item without any notification. Must be called on the worker thread.
2338 */
2339 private void updateItem(long itemId, ContentValues update) {
2340 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002341 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002342 update,
2343 BaseColumns._ID + "= ?",
2344 new String[]{Long.toString(itemId)});
2345 }
2346
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 /** Filters the set of items who are directly or indirectly (via another container) on the
2348 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002349 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 ArrayList<ItemInfo> allWorkspaceItems,
2351 ArrayList<ItemInfo> currentScreenItems,
2352 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002353 // Purge any null ItemInfos
2354 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2355 while (iter.hasNext()) {
2356 ItemInfo i = iter.next();
2357 if (i == null) {
2358 iter.remove();
2359 }
2360 }
2361
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002362 // Order the set of items by their containers first, this allows use to walk through the
2363 // list sequentially, build up a list of containers that are in the specified screen,
2364 // as well as all items in those containers.
2365 Set<Long> itemsOnScreen = new HashSet<Long>();
2366 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2367 @Override
2368 public int compare(ItemInfo lhs, ItemInfo rhs) {
2369 return (int) (lhs.container - rhs.container);
2370 }
2371 });
2372 for (ItemInfo info : allWorkspaceItems) {
2373 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 currentScreenItems.add(info);
2376 itemsOnScreen.add(info.id);
2377 } else {
2378 otherScreenItems.add(info);
2379 }
2380 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2381 currentScreenItems.add(info);
2382 itemsOnScreen.add(info.id);
2383 } else {
2384 if (itemsOnScreen.contains(info.container)) {
2385 currentScreenItems.add(info);
2386 itemsOnScreen.add(info.id);
2387 } else {
2388 otherScreenItems.add(info);
2389 }
2390 }
2391 }
2392 }
2393
2394 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002395 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 ArrayList<LauncherAppWidgetInfo> appWidgets,
2397 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2398 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399
2400 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002401 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002403 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 currentScreenWidgets.add(widget);
2405 } else {
2406 otherScreenWidgets.add(widget);
2407 }
2408 }
2409 }
2410
2411 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002412 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002413 LongArrayMap<ItemInfo> itemsIdMap,
2414 LongArrayMap<FolderInfo> folders,
2415 LongArrayMap<FolderInfo> currentScreenFolders,
2416 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417
Sunny Goyale2df0622015-04-24 11:27:00 -07002418 int total = folders.size();
2419 for (int i = 0; i < total; i++) {
2420 long id = folders.keyAt(i);
2421 FolderInfo folder = folders.valueAt(i);
2422
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002424 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002426 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 currentScreenFolders.put(id, folder);
2428 } else {
2429 otherScreenFolders.put(id, folder);
2430 }
2431 }
2432 }
2433
2434 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2435 * right) */
2436 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002437 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002438 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002439 final int screenCols = profile.numColumns;
2440 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002442 @Override
2443 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002444 if (lhs.container == rhs.container) {
2445 // Within containers, order by their spatial position in that container
2446 switch ((int) lhs.container) {
2447 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2448 long lr = (lhs.screenId * screenCellCount +
2449 lhs.cellY * screenCols + lhs.cellX);
2450 long rr = (rhs.screenId * screenCellCount +
2451 rhs.cellY * screenCols + rhs.cellX);
2452 return (int) (lr - rr);
2453 }
2454 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2455 // We currently use the screen id as the rank
2456 return (int) (lhs.screenId - rhs.screenId);
2457 }
2458 default:
2459 if (LauncherAppState.isDogfoodBuild()) {
2460 throw new RuntimeException("Unexpected container type when " +
2461 "sorting workspace items.");
2462 }
2463 return 0;
2464 }
2465 } else {
2466 // Between containers, order by hotseat, desktop
2467 return (int) (lhs.container - rhs.container);
2468 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002469 }
2470 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002472
Adam Cohendcd297f2013-06-18 13:13:40 -07002473 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2474 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 final Runnable r = new Runnable() {
2476 @Override
2477 public void run() {
2478 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2479 if (callbacks != null) {
2480 callbacks.bindScreens(orderedScreens);
2481 }
2482 }
2483 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002484 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002485 }
2486
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2488 final ArrayList<ItemInfo> workspaceItems,
2489 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002490 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002492
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 final boolean postOnMainThread = (deferredBindRunnables != null);
2494
2495 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002496 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002498 final int start = i;
2499 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 final Runnable r = new Runnable() {
2501 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002503 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002504 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002505 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2506 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 }
2508 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 };
2510 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002511 synchronized (deferredBindRunnables) {
2512 deferredBindRunnables.add(r);
2513 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002515 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 }
Joe Onorato36115782010-06-17 13:28:48 -04002517 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518
2519 // Bind the folders
2520 if (!folders.isEmpty()) {
2521 final Runnable r = new Runnable() {
2522 public void run() {
2523 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2524 if (callbacks != null) {
2525 callbacks.bindFolders(folders);
2526 }
2527 }
2528 };
2529 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002530 synchronized (deferredBindRunnables) {
2531 deferredBindRunnables.add(r);
2532 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002534 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 }
2536 }
2537
2538 // Bind the widgets, one at a time
2539 N = appWidgets.size();
2540 for (int i = 0; i < N; i++) {
2541 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2542 final Runnable r = new Runnable() {
2543 public void run() {
2544 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2545 if (callbacks != null) {
2546 callbacks.bindAppWidget(widget);
2547 }
2548 }
2549 };
2550 if (postOnMainThread) {
2551 deferredBindRunnables.add(r);
2552 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002553 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 }
2555 }
2556 }
2557
2558 /**
2559 * Binds all loaded data to actual views on the main thread.
2560 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002561 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 final long t = SystemClock.uptimeMillis();
2563 Runnable r;
2564
2565 // Don't use these two variables in any of the callback runnables.
2566 // Otherwise we hold a reference to them.
2567 final Callbacks oldCallbacks = mCallbacks.get();
2568 if (oldCallbacks == null) {
2569 // This launcher has exited and nobody bothered to tell us. Just bail.
2570 Log.w(TAG, "LoaderTask running with no launcher");
2571 return;
2572 }
2573
Winson Chung9b9fb962013-11-15 15:39:34 -08002574 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2576 ArrayList<LauncherAppWidgetInfo> appWidgets =
2577 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002578 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002579
2580 final LongArrayMap<FolderInfo> folders;
2581 final LongArrayMap<ItemInfo> itemsIdMap;
2582
Winson Chung2abf94d2012-07-18 18:16:38 -07002583 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002584 workspaceItems.addAll(sBgWorkspaceItems);
2585 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002586 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002587
2588 folders = sBgFolders.clone();
2589 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002590 }
2591
Derek Prothro7aff3992013-12-10 14:00:37 -05002592 final boolean isLoadingSynchronously =
2593 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002594 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002595 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002596 if (currScreen >= orderedScreenIds.size()) {
2597 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002598 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002599 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002600 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002601 final long currentScreenId = currentScreen < 0
2602 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002603
2604 // Load all the items that are on the current page first (and in the process, unbind
2605 // all the existing workspace items before we call startBinding() below.
2606 unbindWorkspaceItemsOnMainThread();
2607
2608 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002609 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2610 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2611 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2612 new ArrayList<LauncherAppWidgetInfo>();
2613 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2614 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002615 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2616 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002617
Winson Chung9b9fb962013-11-15 15:39:34 -08002618 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002619 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002620 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002622 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 otherFolders);
2624 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2625 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2626
2627 // Tell the workspace that we're about to start binding items
2628 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002629 public void run() {
2630 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2631 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002633 }
2634 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002636 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637
Adam Cohendcd297f2013-06-18 13:13:40 -07002638 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2639
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002640 // Load items on the current page
2641 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2642 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002643 if (isLoadingSynchronously) {
2644 r = new Runnable() {
2645 public void run() {
2646 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002647 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002648 callbacks.onPageBoundSynchronously(currentScreen);
2649 }
2650 }
2651 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002652 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002653 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002654
Winson Chung4a2afa32012-07-19 14:53:05 -07002655 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2656 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002657 synchronized (mDeferredBindRunnables) {
2658 mDeferredBindRunnables.clear();
2659 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002660 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002661 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662
2663 // Tell the workspace that we're done binding items
2664 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002665 public void run() {
2666 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2667 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002668 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002669 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670
Winson Chung98e030b2012-05-07 16:01:11 -07002671 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002672 if (DEBUG_LOADERS) {
2673 Log.d(TAG, "bound workspace in "
2674 + (SystemClock.uptimeMillis()-t) + "ms");
2675 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002676
2677 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002679 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002680 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002681 synchronized (mDeferredBindRunnables) {
2682 mDeferredBindRunnables.add(r);
2683 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002684 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002685 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002686 }
Joe Onorato36115782010-06-17 13:28:48 -04002687 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002688
Joe Onorato36115782010-06-17 13:28:48 -04002689 private void loadAndBindAllApps() {
2690 if (DEBUG_LOADERS) {
2691 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2692 }
2693 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002694 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002695 synchronized (LoaderTask.this) {
2696 if (mStopped) {
2697 return;
2698 }
2699 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002700 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002701 synchronized (LoaderTask.this) {
2702 if (mStopped) {
2703 return;
2704 }
2705 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002706 }
Joe Onorato36115782010-06-17 13:28:48 -04002707 } else {
2708 onlyBindAllApps();
2709 }
2710 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002711
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002712 private void updateIconCache() {
2713 // Ignore packages which have a promise icon.
2714 HashSet<String> packagesToIgnore = new HashSet<>();
2715 synchronized (sBgLock) {
2716 for (ItemInfo info : sBgItemsIdMap) {
2717 if (info instanceof ShortcutInfo) {
2718 ShortcutInfo si = (ShortcutInfo) info;
2719 if (si.isPromise() && si.getTargetComponent() != null) {
2720 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2721 }
2722 } else if (info instanceof LauncherAppWidgetInfo) {
2723 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2724 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2725 packagesToIgnore.add(lawi.providerName.getPackageName());
2726 }
2727 }
2728 }
2729 }
2730 mIconCache.updateDbIcons(packagesToIgnore);
2731 }
2732
Joe Onorato36115782010-06-17 13:28:48 -04002733 private void onlyBindAllApps() {
2734 final Callbacks oldCallbacks = mCallbacks.get();
2735 if (oldCallbacks == null) {
2736 // This launcher has exited and nobody bothered to tell us. Just bail.
2737 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2738 return;
2739 }
2740
2741 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002742 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002743 final ArrayList<AppInfo> list
2744 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002745 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002746 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002747 public void run() {
2748 final long t = SystemClock.uptimeMillis();
2749 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2750 if (callbacks != null) {
2751 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002752 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002753 }
2754 if (DEBUG_LOADERS) {
2755 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2756 + (SystemClock.uptimeMillis()-t) + "ms");
2757 }
2758 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002759 };
2760 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002761 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002762 r.run();
2763 } else {
2764 mHandler.post(r);
2765 }
Joe Onorato36115782010-06-17 13:28:48 -04002766 }
2767
Winson Chung64359a52013-07-08 17:17:08 -07002768 private void loadAllApps() {
2769 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002770
Joe Onorato36115782010-06-17 13:28:48 -04002771 final Callbacks oldCallbacks = mCallbacks.get();
2772 if (oldCallbacks == null) {
2773 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002774 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002775 return;
2776 }
2777
Kenny Guyed131872014-04-30 03:02:21 +01002778 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2779
Winson Chung64359a52013-07-08 17:17:08 -07002780 // Clear the list of apps
2781 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002782 for (UserHandleCompat user : profiles) {
2783 // Query for the set of apps
2784 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002785 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002786 if (DEBUG_LOADERS) {
2787 Log.d(TAG, "getActivityList took "
2788 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2789 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2790 }
2791 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002792 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002793 if (apps == null || apps.isEmpty()) {
2794 return;
2795 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002796
Kenny Guyed131872014-04-30 03:02:21 +01002797 // Create the ApplicationInfos
2798 for (int i = 0; i < apps.size(); i++) {
2799 LauncherActivityInfoCompat app = apps.get(i);
2800 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002801 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002802 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002803
Sunny Goyal756a28a2015-04-23 17:07:55 -07002804 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2805 if (heuristic != null) {
2806 runAfterBindCompletes(new Runnable() {
2807
2808 @Override
2809 public void run() {
2810 heuristic.processUserApps(apps);
2811 }
2812 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002813 }
Winson Chung64359a52013-07-08 17:17:08 -07002814 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002815 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002816 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2817 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002818
2819 // Post callback on main thread
2820 mHandler.post(new Runnable() {
2821 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002822
Winson Chung64359a52013-07-08 17:17:08 -07002823 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002824 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002825 if (callbacks != null) {
2826 callbacks.bindAllApplications(added);
2827 if (DEBUG_LOADERS) {
2828 Log.d(TAG, "bound " + added.size() + " apps in "
2829 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2830 }
2831 } else {
2832 Log.i(TAG, "not binding apps: no Launcher activity");
2833 }
2834 }
2835 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002836 // Cleanup any data stored for a deleted user.
2837 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002838
Hyunyoung Song219d0482015-05-07 12:51:42 -07002839 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2840 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002841 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002842 Log.d(TAG, "Icons processed in "
2843 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002844 }
2845 }
2846
2847 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002848 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002849 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002850 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2851 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2852 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2853 }
Joe Onorato36115782010-06-17 13:28:48 -04002854 }
2855 }
2856
Sunny Goyal75b0f552015-05-20 21:57:06 -07002857 /**
2858 * Called when the icons for packages have been updated in the icon cache.
2859 */
2860 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2861 final Callbacks callbacks = getCallback();
2862 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2863 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2864
2865 // If any package icon has changed (app was updated while launcher was dead),
2866 // update the corresponding shortcuts.
2867 synchronized (sBgLock) {
2868 for (ItemInfo info : sBgItemsIdMap) {
2869 if (info instanceof ShortcutInfo && user.equals(info.user)
2870 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2871 ShortcutInfo si = (ShortcutInfo) info;
2872 ComponentName cn = si.getTargetComponent();
2873 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2874 si.updateIcon(mIconCache);
2875 updatedShortcuts.add(si);
2876 }
2877 }
2878 }
2879 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2880 }
2881
2882 if (!updatedShortcuts.isEmpty()) {
2883 final UserHandleCompat userFinal = user;
2884 mHandler.post(new Runnable() {
2885
2886 public void run() {
2887 Callbacks cb = getCallback();
2888 if (cb != null && callbacks == cb) {
2889 cb.bindShortcutsChanged(updatedShortcuts,
2890 new ArrayList<ShortcutInfo>(), userFinal);
2891 }
2892 }
2893 });
2894 }
2895
2896 if (!updatedApps.isEmpty()) {
2897 mHandler.post(new Runnable() {
2898
2899 public void run() {
2900 Callbacks cb = getCallback();
2901 if (cb != null && callbacks == cb) {
2902 cb.bindAppsUpdated(updatedApps);
2903 }
2904 }
2905 });
2906 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002907
2908 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2909 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002910 }
2911
Joe Onorato36115782010-06-17 13:28:48 -04002912 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002913 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002914 }
2915
Adam Cohen091440a2015-03-18 14:16:05 -07002916 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002917
2918 @Override
2919 public void onReceive(Context context, Intent intent) {
2920 synchronized (sBgLock) {
2921 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2922 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002923 final PackageManager manager = context.getPackageManager();
2924 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2925 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002926 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2927 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002928 packagesRemoved.clear();
2929 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002930 for (String pkg : entry.getValue()) {
2931 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002932 boolean packageOnSdcard = launcherApps.isAppEnabled(
2933 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2934 if (packageOnSdcard) {
2935 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2936 packagesUnavailable.add(pkg);
2937 } else {
2938 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2939 packagesRemoved.add(pkg);
2940 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002941 }
2942 }
2943 if (!packagesRemoved.isEmpty()) {
2944 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2945 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2946 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002947 if (!packagesUnavailable.isEmpty()) {
2948 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2949 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2950 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002951 }
Sunny Goyal34942622014-08-29 17:20:55 -07002952 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953 }
2954 }
2955 }
2956
Joe Onorato36115782010-06-17 13:28:48 -04002957 private class PackageUpdatedTask implements Runnable {
2958 int mOp;
2959 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002960 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002961
2962 public static final int OP_NONE = 0;
2963 public static final int OP_ADD = 1;
2964 public static final int OP_UPDATE = 2;
2965 public static final int OP_REMOVE = 3; // uninstlled
2966 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2967
2968
Kenny Guyed131872014-04-30 03:02:21 +01002969 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002970 mOp = op;
2971 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002972 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
2974
2975 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002976 if (!mHasLoaderCompletedOnce) {
2977 // Loader has not yet run.
2978 return;
2979 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002980 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002981
2982 final String[] packages = mPackages;
2983 final int N = packages.length;
2984 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002985 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002986 for (int i=0; i<N; i++) {
2987 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002988 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002989 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002990 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002991
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002992 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2993 if (heuristic != null) {
2994 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002995 }
Joe Onorato36115782010-06-17 13:28:48 -04002996 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 }
Joe Onorato36115782010-06-17 13:28:48 -04002998 case OP_UPDATE:
2999 for (int i=0; i<N; i++) {
3000 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003001 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003002 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003003 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003004 }
3005 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003006 case OP_REMOVE: {
3007 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3008 if (heuristic != null) {
3009 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003010 }
Joe Onorato36115782010-06-17 13:28:48 -04003011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003013 mIconCache.removeIconsForPkg(packages[i], mUser);
3014 }
3015 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003016 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003017 case OP_UNAVAILABLE:
3018 for (int i=0; i<N; i++) {
3019 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3020 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003021 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003022 }
3023 break;
3024 }
3025
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003026 ArrayList<AppInfo> added = null;
3027 ArrayList<AppInfo> modified = null;
3028 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003029
Adam Cohen487f7dd2012-06-28 18:12:10 -07003030 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003031 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003032 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003033 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003034 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003035 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003036 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
Winson Chung5d55f332012-07-16 20:45:03 -07003038 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003039 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003040 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003041 }
3042
Sunny Goyale0f58d72014-11-10 18:05:31 -08003043 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003044 if (callbacks == null) {
3045 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3046 return;
3047 }
3048
Sunny Goyal4390ace2014-10-13 11:33:11 -07003049 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3050 new HashMap<ComponentName, AppInfo>();
3051
Joe Onorato36115782010-06-17 13:28:48 -04003052 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003053 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003054 for (AppInfo ai : added) {
3055 addedOrUpdatedApps.put(ai.componentName, ai);
3056 }
Joe Onorato36115782010-06-17 13:28:48 -04003057 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003058
Joe Onorato36115782010-06-17 13:28:48 -04003059 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003060 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 for (AppInfo ai : modified) {
3062 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003063 }
3064
Joe Onorato36115782010-06-17 13:28:48 -04003065 mHandler.post(new Runnable() {
3066 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003067 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003068 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003069 callbacks.bindAppsUpdated(modifiedFinal);
3070 }
3071 }
3072 });
3073 }
Winson Chung83892cc2013-05-01 16:53:33 -07003074
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003076 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3078 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3079 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3080
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003081 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003083 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3085 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 boolean infoUpdated = false;
3087 boolean shortcutUpdated = false;
3088
3089 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003090 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003092 Bitmap icon = Utilities.createIconBitmap(
3093 si.iconResource.packageName,
3094 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003095 if (icon != null) {
3096 si.setIcon(icon);
3097 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003098 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 }
3100 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003101
3102 ComponentName cn = si.getTargetComponent();
3103 if (cn != null && packageSet.contains(cn.getPackageName())) {
3104 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3105
3106 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3108 // Auto install icon
3109 PackageManager pm = context.getPackageManager();
3110 ResolveInfo matched = pm.resolveActivity(
3111 new Intent(Intent.ACTION_MAIN)
3112 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3113 PackageManager.MATCH_DEFAULT_ONLY);
3114 if (matched == null) {
3115 // Try to find the best match activity.
3116 Intent intent = pm.getLaunchIntentForPackage(
3117 cn.getPackageName());
3118 if (intent != null) {
3119 cn = intent.getComponent();
3120 appInfo = addedOrUpdatedApps.get(cn);
3121 }
3122
3123 if ((intent == null) || (appInfo == null)) {
3124 removedShortcuts.add(si);
3125 continue;
3126 }
3127 si.promisedIntent = intent;
3128 }
3129 }
3130
3131 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003132 if (appInfo != null) {
3133 si.flags = appInfo.flags;
3134 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135
Sunny Goyal756adbc2015-04-16 15:20:51 -07003136 si.intent = si.promisedIntent;
3137 si.promisedIntent = null;
3138 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 infoUpdated = true;
3140 si.updateIcon(mIconCache);
3141 }
3142
3143 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3144 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3145 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003146 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 si.contentDescription = appInfo.contentDescription;
3148 infoUpdated = true;
3149 }
3150
3151 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3152 // Since package was just updated, the target must be available now.
3153 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3154 shortcutUpdated = true;
3155 }
3156 }
3157
3158 if (infoUpdated || shortcutUpdated) {
3159 updatedShortcuts.add(si);
3160 }
3161 if (infoUpdated) {
3162 updateItemInDatabase(context, si);
3163 }
3164 } else if (info instanceof LauncherAppWidgetInfo) {
3165 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3166 if (mUser.equals(widgetInfo.user)
3167 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3168 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3169 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3170 widgets.add(widgetInfo);
3171 updateItemInDatabase(context, widgetInfo);
3172 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003173 }
3174 }
3175 }
3176
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3178 mHandler.post(new Runnable() {
3179
3180 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003181 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 if (callbacks == cb && cb != null) {
3183 callbacks.bindShortcutsChanged(
3184 updatedShortcuts, removedShortcuts, mUser);
3185 }
3186 }
3187 });
3188 if (!removedShortcuts.isEmpty()) {
3189 deleteItemsFromDatabase(context, removedShortcuts);
3190 }
3191 }
3192 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003193 mHandler.post(new Runnable() {
3194 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003195 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003196 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003197 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003198 }
3199 }
3200 });
3201 }
3202 }
3203
Winson Chungdf95eb12013-10-16 14:57:07 -07003204 final ArrayList<String> removedPackageNames =
3205 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003206 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003207 // Mark all packages in the broadcast to be removed
3208 removedPackageNames.addAll(Arrays.asList(packages));
3209 } else if (mOp == OP_UPDATE) {
3210 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003211 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003212 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003213 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003214 }
3215 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003216 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003217
Winson Chungdf95eb12013-10-16 14:57:07 -07003218 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003219 final int removeReason;
3220 if (mOp == OP_UNAVAILABLE) {
3221 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3222 } else {
3223 // Remove all the components associated with this package
3224 for (String pn : removedPackageNames) {
3225 deletePackageFromDatabase(context, pn, mUser);
3226 }
3227 // Remove all the specific components
3228 for (AppInfo a : removedApps) {
3229 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3230 deleteItemsFromDatabase(context, infos);
3231 }
3232 removeReason = 0;
3233 }
3234
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003236 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003237 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003238 mHandler.post(new Runnable() {
3239 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003240 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003241 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003242 callbacks.bindComponentsRemoved(
3243 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003244 }
3245 }
3246 });
Joe Onoratobe386092009-11-17 17:32:16 -08003247 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003248
3249 // onProvidersChanged method (API >= 17) already refreshed the widget list
3250 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3251
Adam Cohen4caf2982013-08-20 18:54:31 -07003252 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003253 mHandler.post(new Runnable() {
3254 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003255 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003256 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003257 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003258 }
3259 }
3260 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 }
3262 }
3263
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003264 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3265 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003266 ArrayList<LauncherAppWidgetProviderInfo> results =
3267 new ArrayList<LauncherAppWidgetProviderInfo>();
3268 try {
3269 synchronized (sBgLock) {
3270 if (sBgWidgetProviders == null || refresh) {
3271 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3272 = new HashMap<>();
3273 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3274 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003275
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003276 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3277 for (AppWidgetProviderInfo pInfo : widgets) {
3278 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3279 UserHandleCompat user = wm.getUser(info);
3280 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3281 }
Robin Lee26ace122015-03-16 19:41:43 +00003282
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003283 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3284 for (CustomAppWidget widget : customWidgets) {
3285 info = new LauncherAppWidgetProviderInfo(context, widget);
3286 UserHandleCompat user = wm.getUser(info);
3287 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3288 }
3289 // Replace the global list at the very end, so that if there is an exception,
3290 // previously loaded provider list is used.
3291 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003292 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003293 results.addAll(sBgWidgetProviders.values());
3294 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003295 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003296 } catch (Exception e) {
3297 if (e.getCause() instanceof TransactionTooLargeException) {
3298 // the returned value may be incomplete and will not be refreshed until the next
3299 // time Launcher starts.
3300 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3301 // onResume is called to refresh the widget provider list.
3302 synchronized (sBgLock) {
3303 if (sBgWidgetProviders != null) {
3304 results.addAll(sBgWidgetProviders.values());
3305 }
3306 return results;
3307 }
3308 } else {
3309 throw e;
3310 }
Adam Cohen59400422014-03-05 18:07:04 -08003311 }
3312 }
3313
Robin Lee26ace122015-03-16 19:41:43 +00003314 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3315 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003316 synchronized (sBgLock) {
3317 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003318 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003319 }
Robin Lee26ace122015-03-16 19:41:43 +00003320 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003321 }
3322 }
3323
Hyunyoung Song227239e2015-05-04 18:17:35 -07003324 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3325 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003326
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003327 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003328 @Override
3329 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003330 updateWidgetsModel(context, refresh);
3331 final WidgetsModel model = mBgWidgetsModel.clone();
3332
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003333 mHandler.post(new Runnable() {
3334 @Override
3335 public void run() {
3336 Callbacks cb = getCallback();
3337 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003338 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003339 }
3340 }
3341 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003342 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003343 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3344 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003345 }
3346 });
3347 }
3348
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003349 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003350 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003351 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003352 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003353 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003354 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003355 PackageManager packageManager = context.getPackageManager();
3356 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003357 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003358 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3359 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003360 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003361 }
3362
Adam Cohen091440a2015-03-18 14:16:05 -07003363 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003364 UserHandleCompat user) {
3365 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3366 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003367 }
Adam Cohen556f6132014-01-15 15:18:08 -08003368
Kenny Guyed131872014-04-30 03:02:21 +01003369 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3370 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003371 if (cn == null) {
3372 return false;
3373 }
Kenny Guyed131872014-04-30 03:02:21 +01003374 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3375 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003376 return false;
3377 }
Kenny Guyed131872014-04-30 03:02:21 +01003378 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003379 }
3380
Adam Cohena28b78e2014-05-20 17:03:04 -07003381 public static boolean isValidPackage(Context context, String packageName,
3382 UserHandleCompat user) {
3383 if (packageName == null) {
3384 return false;
3385 }
3386 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3387 return launcherApps.isPackageEnabledForProfile(packageName, user);
3388 }
3389
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003391 * Make an ShortcutInfo object for a restored application or shortcut item that points
3392 * to a package that is not yet installed on the system.
3393 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003394 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003395 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003396 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003397 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003398
3399 Bitmap icon = iconInfo.loadIcon(c, info, context);
3400 // the fallback icon
3401 if (icon == null) {
3402 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3403 } else {
3404 info.setIcon(icon);
3405 }
Sunny Goyal34942622014-08-29 17:20:55 -07003406
3407 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003408 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003409 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003410 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003411 }
Sunny Goyal34942622014-08-29 17:20:55 -07003412 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3413 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003414 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003415 }
Sunny Goyal34942622014-08-29 17:20:55 -07003416 } else {
3417 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3418 }
3419
Winson Chung82b016c2015-05-08 17:00:10 -07003420 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003421 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003422 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003423 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 return info;
3425 }
3426
3427 /**
3428 * Make an Intent object for a restored application or shortcut item that points
3429 * to the market page for the item.
3430 */
Adam Cohen091440a2015-03-18 14:16:05 -07003431 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003432 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003433 return getMarketIntent(componentName.getPackageName());
3434 }
3435
3436 static Intent getMarketIntent(String packageName) {
3437 return new Intent(Intent.ACTION_VIEW)
3438 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003439 .scheme("market")
3440 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003441 .appendQueryParameter("id", packageName)
3442 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003443 }
3444
3445 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003446 * Make an ShortcutInfo object for a shortcut that is an application.
3447 *
3448 * If c is not null, then it will be used to fill in missing data like the title and icon.
3449 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003450 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003451 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003452 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003453 if (user == null) {
3454 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003455 return null;
3456 }
3457
Kenny Guyed131872014-04-30 03:02:21 +01003458 ComponentName componentName = intent.getComponent();
3459 if (componentName == null) {
3460 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3461 return null;
3462 }
3463
3464 Intent newIntent = new Intent(intent.getAction(), null);
3465 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3466 newIntent.setComponent(componentName);
3467 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003468 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003469 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3470 return null;
3471 }
3472
3473 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003474 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003475 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3476 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3477 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003478 }
3479
Joe Onorato56d82912010-03-07 14:32:10 -05003480 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003481 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003482 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003483 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003484
Joe Onorato56d82912010-03-07 14:32:10 -05003485 // fall back to the class name of the activity
3486 if (info.title == null) {
3487 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003488 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003491 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003492 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003493 if (lai != null) {
3494 info.flags = AppInfo.initFlags(lai);
3495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003497 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003498
Sunny Goyale2df0622015-04-24 11:27:00 -07003499 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003500 ItemInfoFilter f) {
3501 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3502 for (ItemInfo i : infos) {
3503 if (i instanceof ShortcutInfo) {
3504 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003505 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003506 if (cn != null && f.filterItem(null, info, cn)) {
3507 filtered.add(info);
3508 }
3509 } else if (i instanceof FolderInfo) {
3510 FolderInfo info = (FolderInfo) i;
3511 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003512 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003513 if (cn != null && f.filterItem(info, s, cn)) {
3514 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003515 }
3516 }
Winson Chung64359a52013-07-08 17:17:08 -07003517 } else if (i instanceof LauncherAppWidgetInfo) {
3518 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3519 ComponentName cn = info.providerName;
3520 if (cn != null && f.filterItem(null, info, cn)) {
3521 filtered.add(info);
3522 }
Winson Chung8a435102012-08-30 17:16:53 -07003523 }
3524 }
Winson Chung64359a52013-07-08 17:17:08 -07003525 return new ArrayList<ItemInfo>(filtered);
3526 }
3527
Adam Cohen091440a2015-03-18 14:16:05 -07003528 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003529 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003530 ItemInfoFilter filter = new ItemInfoFilter() {
3531 @Override
3532 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003533 if (info.user == null) {
3534 return cn.equals(cname);
3535 } else {
3536 return cn.equals(cname) && info.user.equals(user);
3537 }
Winson Chung64359a52013-07-08 17:17:08 -07003538 }
3539 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003540 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003541 }
3542
Sunny Goyal1a745e82014-10-02 15:58:31 -07003543 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003544 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003545 */
Adam Cohen091440a2015-03-18 14:16:05 -07003546 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003547 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003548 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003549 // Non-app shortcuts are only supported for current user.
3550 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003553 // TODO: If there's an explicit component and we can't install that, delete it.
3554
Winson Chung82b016c2015-05-08 17:00:10 -07003555 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003556
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003557 Bitmap icon = iconInfo.loadIcon(c, info, context);
3558 // the fallback icon
3559 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003560 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003561 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003562 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003563 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 return info;
3565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003566
Sunny Goyal2350bc92014-10-14 16:42:54 -07003567 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003568 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3569 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3570 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3571
Adam Cohend9198822011-11-22 16:42:47 -08003572 if (intent == null) {
3573 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3574 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3575 return null;
3576 }
3577
Joe Onorato0589f0f2010-02-08 13:44:00 -08003578 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003579 boolean customIcon = false;
3580 ShortcutIconResource iconResource = null;
3581
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003582 if (bitmap instanceof Bitmap) {
3583 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003584 customIcon = true;
3585 } else {
3586 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003587 if (extra instanceof ShortcutIconResource) {
3588 iconResource = (ShortcutIconResource) extra;
3589 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003590 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003591 }
3592 }
3593
Michael Jurkac9d95c52011-08-29 14:03:34 -07003594 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003595
Kenny Guyed131872014-04-30 03:02:21 +01003596 // Only support intents for current user for now. Intents sent from other
3597 // users wouldn't get here without intent forwarding anyway.
3598 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003599 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003600 icon = mIconCache.getDefaultIcon(info.user);
3601 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003602 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003603 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003604
Winson Chung82b016c2015-05-08 17:00:10 -07003605 info.title = Utilities.trim(name);
3606 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 info.intent = intent;
3608 info.customIcon = customIcon;
3609 info.iconResource = iconResource;
3610
3611 return info;
3612 }
3613
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003615 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 * or make a new one.
3617 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003618 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 // See if a placeholder was created for us already
3620 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003621 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003623 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 folders.put(id, folderInfo);
3625 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003626 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003627 }
3628
Joe Onoratobe386092009-11-17 17:32:16 -08003629
Sunny Goyal651077b2014-06-30 14:15:31 -07003630 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3631 return (provider != null) && (provider.provider != null)
3632 && (provider.provider.getPackageName() != null);
3633 }
3634
Joe Onoratobe386092009-11-17 17:32:16 -08003635 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003636 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003637 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3638 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3639 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3640 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003641 if (mLoaderTask != null) {
3642 mLoaderTask.dumpState();
3643 } else {
3644 Log.d(TAG, "mLoaderTask=null");
3645 }
Joe Onoratobe386092009-11-17 17:32:16 -08003646 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003647
3648 public Callbacks getCallback() {
3649 return mCallbacks != null ? mCallbacks.get() : null;
3650 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003651
3652 /**
3653 * @return {@link FolderInfo} if its already loaded.
3654 */
3655 public FolderInfo findFolderById(Long folderId) {
3656 synchronized (sBgLock) {
3657 return sBgFolders.get(folderId);
3658 }
3659 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003660
3661 /**
3662 * @return the looper for the worker thread which can be used to start background tasks.
3663 */
3664 public static Looper getWorkerLooper() {
3665 return sWorkerThread.getLooper();
3666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667}