blob: bc1bc17df4b667ee218e5d5a43f2309d83b78112 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070036import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070058import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000059import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070060import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070061import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070062import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070063import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080064
Michael Jurkac2f801e2011-07-12 14:19:46 -070065import java.lang.ref.WeakReference;
66import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070067import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070069import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070070import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.Collections;
72import java.util.Comparator;
73import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070074import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070075import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070077import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080/**
81 * Maintains in-memory state of the Launcher. It is expected that there should be only one
82 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070083 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 */
Kenny Guyed131872014-04-30 03:02:21 +010085public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010086 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040088 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070089 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070092
Dan Sandlerd5024042014-01-09 15:01:33 -050093 public static final int LOADER_FLAG_NONE = 0;
94 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
95 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
96
Joe Onorato36115782010-06-17 13:28:48 -040097 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050098 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080099
Adam Cohen091440a2015-03-18 14:16:05 -0700100 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800101 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final LauncherAppState mApp;
104 @Thunk final Object mLock = new Object();
105 @Thunk DeferredHandler mHandler = new DeferredHandler();
106 @Thunk LoaderTask mLoaderTask;
107 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700108 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Jason Monkbbe1e242014-05-16 17:37:34 -0400110 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700113 static {
114 sWorkerThread.start();
115 }
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 // We start off with everything not loaded. After that, we assume that
119 // our monitoring of the package manager provides all updates and we never
120 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk boolean mWorkspaceLoaded;
122 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // When we are loading pages synchronously, we can't just post the binding of items on the side
125 // pages as this delays the rotation process. Instead, we wait for a callback from the first
126 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
127 // a normal load, we also clear this set of Runnables.
128 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
129
Sunny Goyal756a28a2015-04-23 17:07:55 -0700130 /**
131 * Set of runnables to be called on the background thread after the workspace binding
132 * is complete.
133 */
134 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
135
Adam Cohen091440a2015-03-18 14:16:05 -0700136 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700139 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700140 // Entire list of widgets.
141 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700151 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700163 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 // sBgWorkspaceScreens is the ordered set of workspace screens.
166 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
167
Adam Cohen59400422014-03-05 18:07:04 -0800168 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000169 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk final LauncherAppsCompat mLauncherApps;
180 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
185 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
187 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700188 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700190 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700199 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
200 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
201 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700202 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700203 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700204 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700205 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100206 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700207 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700209 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Winson Chungee055712013-07-30 14:46:24 -0700219 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700220 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400221 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
222 // resource string.
223 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
224 ProviderInfo providerInfo =
225 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
226 ProviderInfo redirectProvider =
227 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700228
229 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400230 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 if (mOldContentProviderExists) {
233 Log.d(TAG, "Old launcher provider exists.");
234 } else {
235 Log.d(TAG, "Old launcher provider does not exist.");
236 }
237
Daniel Sandlere4f98912013-06-25 15:13:26 -0400238 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100239 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700240 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Kenny Guyed131872014-04-30 03:02:21 +0100243 mLauncherApps = LauncherAppsCompat.getInstance(context);
244 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
248 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 // If we are on the worker thread, post onto the main handler
252 mHandler.post(r);
253 } else {
254 r.run();
255 }
256 }
257
258 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
259 * posted on the worker thread handler. */
260 private static void runOnWorkerThread(Runnable r) {
261 if (sWorkerThread.getThreadId() == Process.myTid()) {
262 r.run();
263 } else {
264 // If we are not on the worker thread, then post to the worker handler
265 sWorker.post(r);
266 }
267 }
268
Sunny Goyal756a28a2015-04-23 17:07:55 -0700269 /**
270 * Runs the specified runnable after the loader is complete
271 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700272 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700273 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
274 synchronized (mBindCompleteRunnables) {
275 mBindCompleteRunnables.add(r);
276 }
277 } else {
278 runOnWorkerThread(r);
279 }
280 }
281
Winson Chunge43a1e72014-01-15 10:33:02 -0800282 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
283 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800284 }
285
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 public void setPackageState(final PackageInstallInfo installInfo) {
287 Runnable updateRunnable = new Runnable() {
288
289 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 synchronized (sBgLock) {
292 final HashSet<ItemInfo> updates = new HashSet<>();
293
294 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
295 // Ignore install success events as they are handled by Package add events.
296 return;
297 }
298
Sunny Goyale2df0622015-04-24 11:27:00 -0700299 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700300 if (info instanceof ShortcutInfo) {
301 ShortcutInfo si = (ShortcutInfo) info;
302 ComponentName cn = si.getTargetComponent();
303 if (si.isPromise() && (cn != null)
304 && installInfo.packageName.equals(cn.getPackageName())) {
305 si.setInstallProgress(installInfo.progress);
306
307 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
308 // Mark this info as broken.
309 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
310 }
311 updates.add(si);
312 }
313 }
314 }
315
316 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
317 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
318 widget.installProgress = installInfo.progress;
319 updates.add(widget);
320 }
321 }
322
323 if (!updates.isEmpty()) {
324 // Push changes to the callback.
325 Runnable r = new Runnable() {
326 public void run() {
327 Callbacks callbacks = getCallback();
328 if (callbacks != null) {
329 callbacks.bindRestoreItemsChange(updates);
330 }
331 }
332 };
333 mHandler.post(r);
334 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500335 }
336 }
337 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500339 }
340
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 /**
342 * Updates the icons and label of all pending icons for the provided package name.
343 */
344 public void updateSessionDisplayInfo(final String packageName) {
345 Runnable updateRunnable = new Runnable() {
346
347 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700348 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700349 synchronized (sBgLock) {
350 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
351 final UserHandleCompat user = UserHandleCompat.myUserHandle();
352
Sunny Goyale2df0622015-04-24 11:27:00 -0700353 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700354 if (info instanceof ShortcutInfo) {
355 ShortcutInfo si = (ShortcutInfo) info;
356 ComponentName cn = si.getTargetComponent();
357 if (si.isPromise() && (cn != null)
358 && packageName.equals(cn.getPackageName())) {
359 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
360 // For auto install apps update the icon as well as label.
361 mIconCache.getTitleAndIcon(si,
362 si.promisedIntent, user,
363 si.shouldUseLowResIcon());
364 } else {
365 // Only update the icon for restored apps.
366 si.updateIcon(mIconCache);
367 }
368 updates.add(si);
369 }
370 }
371 }
372
373 if (!updates.isEmpty()) {
374 // Push changes to the callback.
375 Runnable r = new Runnable() {
376 public void run() {
377 Callbacks callbacks = getCallback();
378 if (callbacks != null) {
379 callbacks.bindShortcutsChanged(updates,
380 new ArrayList<ShortcutInfo>(), user);
381 }
382 }
383 };
384 mHandler.post(r);
385 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700386 }
387 }
388 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700389 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700390 }
391
Adam Cohen76a47a12014-02-05 11:47:43 -0800392 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800393 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800394
395 if (allAppsApps == null) {
396 throw new RuntimeException("allAppsApps must not be null");
397 }
398 if (allAppsApps.isEmpty()) {
399 return;
400 }
401
402 // Process the newly added applications and add them to the database first
403 Runnable r = new Runnable() {
404 public void run() {
405 runOnMainThread(new Runnable() {
406 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800407 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800408 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500409 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 }
411 }
412 });
413 }
414 };
415 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700416 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800417
Sunny Goyala9116722015-04-29 13:55:58 -0700418 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 int[] xy, int spanX, int spanY) {
420 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700421 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
422 final int xCount = (int) profile.numColumns;
423 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 boolean[][] occupied = new boolean[xCount][yCount];
425 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700426 for (ItemInfo r : occupiedPos) {
427 int right = r.cellX + r.spanX;
428 int bottom = r.cellY + r.spanY;
429 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
430 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 occupied[x][y] = true;
432 }
433 }
434 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800435 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700436 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 }
438
439 /**
440 * Find a position on the screen for the given size or adds a new screen.
441 * @return screenId and the coordinates for the item.
442 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700443 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 ArrayList<Long> workspaceScreens,
446 ArrayList<Long> addedWorkspaceScreensFinal,
447 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700448 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449
Sunny Goyala9116722015-04-29 13:55:58 -0700450 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700451 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700452 synchronized (sBgLock) {
453 for (ItemInfo info : sBgItemsIdMap) {
454 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
455 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
456 if (items == null) {
457 items = new ArrayList<>();
458 screenItems.put(info.screenId, items);
459 }
460 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
464
465 // Find appropriate space for the item.
466 long screenId = 0;
467 int[] cordinates = new int[2];
468 boolean found = false;
469
470 int screenCount = workspaceScreens.size();
471 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700472 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800473 if (preferredScreenIndex < screenCount) {
474 screenId = workspaceScreens.get(preferredScreenIndex);
475 found = findNextAvailableIconSpaceInScreen(
476 screenItems.get(screenId), cordinates, spanX, spanY);
477 }
478
479 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700480 // Search on any of the screens starting from the first screen.
481 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 screenId = workspaceScreens.get(screen);
483 if (findNextAvailableIconSpaceInScreen(
484 screenItems.get(screenId), cordinates, spanX, spanY)) {
485 // We found a space for it
486 found = true;
487 break;
488 }
489 }
490 }
491
492 if (!found) {
493 // Still no position found. Add a new screen to the end.
494 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
495
496 // Save the screen id for binding in the workspace
497 workspaceScreens.add(screenId);
498 addedWorkspaceScreensFinal.add(screenId);
499
500 // If we still can't find an empty space, then God help us all!!!
501 if (!findNextAvailableIconSpaceInScreen(
502 screenItems.get(screenId), cordinates, spanX, spanY)) {
503 throw new RuntimeException("Can't find space to add the item");
504 }
505 }
506 return Pair.create(screenId, cordinates);
507 }
508
509 /**
510 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700513 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800515 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700516 return;
Winson Chung997a9232013-07-24 15:33:46 -0700517 }
Winson Chung64359a52013-07-08 17:17:08 -0700518 // Process the newly added applications and add them to the database first
519 Runnable r = new Runnable() {
520 public void run() {
521 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
522 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
523
Winson Chung76828c82013-08-19 15:43:29 -0700524 // Get the list of workspace screens. We need to append to this list and
525 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
526 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800527 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700528 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700530 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700532 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700534 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 }
Winson Chung76828c82013-08-19 15:43:29 -0700536
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700538 Pair<Long, int[]> coords = findSpaceForItem(context,
539 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 1, 1);
541 long screenId = coords.first;
542 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700543
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 ItemInfo itemInfo;
545 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
546 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800547 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700549 } else {
550 throw new RuntimeException("Unexpected info type");
551 }
Winson Chung94d67682013-09-25 16:29:40 -0700552
Winson Chung64359a52013-07-08 17:17:08 -0700553 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700554 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700555 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700556 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700557 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700558 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700559 }
560 }
561
Winson Chung76828c82013-08-19 15:43:29 -0700562 // Update the workspace screens
563 updateWorkspaceScreenOrder(context, workspaceScreens);
564
Adam Cohen76a47a12014-02-05 11:47:43 -0800565 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700566 runOnMainThread(new Runnable() {
567 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800568 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700569 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700570 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
571 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700572 if (!addedShortcutsFinal.isEmpty()) {
573 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
574 long lastScreenId = info.screenId;
575 for (ItemInfo i : addedShortcutsFinal) {
576 if (i.screenId == lastScreenId) {
577 addAnimated.add(i);
578 } else {
579 addNotAnimated.add(i);
580 }
Winson Chung997a9232013-07-24 15:33:46 -0700581 }
582 }
Winson Chungd64d1762013-08-20 14:37:16 -0700583 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800584 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700585 }
Winson Chung64359a52013-07-08 17:17:08 -0700586 }
Winson Chung997a9232013-07-24 15:33:46 -0700587 });
588 }
Winson Chung64359a52013-07-08 17:17:08 -0700589 }
590 };
591 runOnWorkerThread(r);
592 }
593
Sunny Goyald33860f2015-04-23 16:02:20 -0700594 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700595 if (sWorkerThread.getThreadId() == Process.myTid()) {
596 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
597 "main thread");
598 }
599
600 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400601 synchronized (mDeferredBindRunnables) {
602 mDeferredBindRunnables.clear();
603 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700604
605 // Remove any queued UI runnables
606 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700607 // Unbind all the workspace items
608 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700609 }
610
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700612 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700613 // Ensure that we don't use the same workspace items data structure on the main thread
614 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700615 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700616 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700617 tmpItems.addAll(sBgWorkspaceItems);
618 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700619 }
620 Runnable r = new Runnable() {
621 @Override
622 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700623 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 item.unbind();
625 }
626 }
627 };
628 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700629 }
630
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 /**
632 * Adds an item to the DB if it was not created previously, or move it to a new
633 * <container, screen, cellX, cellY>
634 */
635 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 if (item.container == ItemInfo.NO_ID) {
638 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700639 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 } else {
641 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644 }
645
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 static void checkItemInfoLocked(
647 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
648 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
649 if (modelItem != null && item != modelItem) {
650 // check all the data is consistent
651 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
652 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
653 ShortcutInfo shortcut = (ShortcutInfo) item;
654 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
655 modelShortcut.intent.filterEquals(shortcut.intent) &&
656 modelShortcut.id == shortcut.id &&
657 modelShortcut.itemType == shortcut.itemType &&
658 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700659 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700660 modelShortcut.cellX == shortcut.cellX &&
661 modelShortcut.cellY == shortcut.cellY &&
662 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700663 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 // For all intents and purposes, this is the same object
665 return;
666 }
667 }
668
669 // the modelItem needs to match up perfectly with item if our model is
670 // to be consistent with the database-- for now, just require
671 // modelItem == item or the equality check above
672 String msg = "item: " + ((item != null) ? item.toString() : "null") +
673 "modelItem: " +
674 ((modelItem != null) ? modelItem.toString() : "null") +
675 "Error: ItemInfo passed to checkItemInfo doesn't match original";
676 RuntimeException e = new RuntimeException(msg);
677 if (stackTrace != null) {
678 e.setStackTrace(stackTrace);
679 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800680 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 }
682 }
683
Michael Jurka816474f2012-06-25 14:49:02 -0700684 static void checkItemInfo(final ItemInfo item) {
685 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
686 final long itemId = item.id;
687 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700688 public void run() {
689 synchronized (sBgLock) {
690 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700691 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700692 }
693 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700694 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700695 }
696
Michael Jurkac9d95c52011-08-29 14:03:34 -0700697 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
698 final ItemInfo item, final String callingFunction) {
699 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700700 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701 final ContentResolver cr = context.getContentResolver();
702
Adam Cohen487f7dd2012-06-28 18:12:10 -0700703 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704 Runnable r = new Runnable() {
705 public void run() {
706 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 updateItemArrays(item, itemId, stackTrace);
708 }
709 };
710 runOnWorkerThread(r);
711 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
714 final ArrayList<ItemInfo> items, final String callingFunction) {
715 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700716
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700717 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
718 Runnable r = new Runnable() {
719 public void run() {
720 ArrayList<ContentProviderOperation> ops =
721 new ArrayList<ContentProviderOperation>();
722 int count = items.size();
723 for (int i = 0; i < count; i++) {
724 ItemInfo item = items.get(i);
725 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700726 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700727 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700728
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
730 updateItemArrays(item, itemId, stackTrace);
731
732 }
733 try {
734 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
735 } catch (Exception e) {
736 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700737 }
738 }
739 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700740 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700741 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700742
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
744 // Lock on mBgLock *after* the db operation
745 synchronized (sBgLock) {
746 checkItemInfoLocked(itemId, item, stackTrace);
747
748 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
749 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
750 // Item is in a folder, make sure this folder exists
751 if (!sBgFolders.containsKey(item.container)) {
752 // An items container is being set to a that of an item which is not in
753 // the list of Folders.
754 String msg = "item: " + item + " container being set to: " +
755 item.container + ", not in the list of folders";
756 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757 }
758 }
759
760 // Items are added/removed from the corresponding FolderInfo elsewhere, such
761 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
762 // that are on the desktop, as appropriate
763 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800764 if (modelItem != null &&
765 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
766 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 switch (modelItem.itemType) {
768 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
769 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
770 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
771 if (!sBgWorkspaceItems.contains(modelItem)) {
772 sBgWorkspaceItems.add(modelItem);
773 }
774 break;
775 default:
776 break;
777 }
778 } else {
779 sBgWorkspaceItems.remove(modelItem);
780 }
781 }
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700786 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700787 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 item.cellX = cellX;
791 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 // We store hotseat items in canonical form which is this orientation invariant position
794 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801
802 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
805 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800806 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Michael Jurkac9d95c52011-08-29 14:03:34 -0700809 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700810 }
811
812 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
814 * cellX, cellY have already been updated on the ItemInfos.
815 */
816 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
817 final long container, final int screen) {
818
819 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
820 int count = items.size();
821
822 for (int i = 0; i < count; i++) {
823 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 item.container = container;
825
826 // We store hotseat items in canonical form which is this orientation invariant position
827 // in the hotseat
828 if (context instanceof Launcher && screen < 0 &&
829 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 item.cellY);
832 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700833 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700834 }
835
836 final ContentValues values = new ContentValues();
837 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
838 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
839 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800840 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842
843 contentValues.add(values);
844 }
845 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
846 }
847
848 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800850 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700853 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800854 item.cellX = cellX;
855 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 item.spanX = spanX;
857 item.spanY = spanY;
858
859 // We store hotseat items in canonical form which is this orientation invariant position
860 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 }
Adam Cohend4844c32011-02-18 19:25:06 -0800867
Adam Cohend4844c32011-02-18 19:25:06 -0800868 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800869 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
871 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800872 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
874 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800876
Michael Jurka816474f2012-06-25 14:49:02 -0700877 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879
880 /**
881 * Update an item to the database in a specified container.
882 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700883 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700884 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100885 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800887 }
888
Sunny Goyal756a28a2015-04-23 17:07:55 -0700889 private void assertWorkspaceLoaded() {
890 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
891 throw new RuntimeException("Trying to add shortcut while loader is running");
892 }
893 }
894
Adam Cohend4844c32011-02-18 19:25:06 -0800895 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700896 * Returns true if the shortcuts already exists on the workspace. This must be called after
897 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700899 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
900 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700902 if (intent.getComponent() != null) {
903 // If component is not null, an intent with null package will produce
904 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700905 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700906 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700907 intentWithPkg = intent.toUri(0);
908 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700909 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
911 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 }
913 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700914 intentWithPkg = intent.toUri(0);
915 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700916 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917
918 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700919 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 if (item instanceof ShortcutInfo) {
921 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700922 Intent targetIntent = info.promisedIntent == null
923 ? info.intent : info.promisedIntent;
924 if (targetIntent != null && info.user.equals(user)) {
925 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700926 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
927 return true;
928 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700929 }
930 }
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700933 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700934 }
935
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 /**
937 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
938 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700939 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 final ContentResolver cr = context.getContentResolver();
941 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
942 "_id=? and (itemType=? or itemType=?)",
943 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700944 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700945
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 try {
947 if (c.moveToFirst()) {
948 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
949 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
950 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
951 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
952 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
953 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700954 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 FolderInfo folderInfo = null;
957 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700958 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
959 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700961 }
962
Sunny Goyala508e4f2015-05-21 09:33:57 -0700963 // Do not trim the folder label, as is was set by the user.
964 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 folderInfo.id = id;
966 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700968 folderInfo.cellX = c.getInt(cellXIndex);
969 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700970 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971
972 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 } finally {
975 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700976 }
977
978 return null;
979 }
980
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 /**
982 * Add an item to the database in a specified container. Sets the container, screen, cellX and
983 * cellY fields of the item. Also assigns an ID to the item.
984 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700985 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700986 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 item.cellX = cellX;
989 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700990 // We store hotseat items in canonical form which is this orientation invariant position
991 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998
999 final ContentValues values = new ContentValues();
1000 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001001 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002
Michael Jurka414300a2013-08-27 15:42:35 +02001003 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
Jason Monk8e19cf22014-03-20 15:06:57 -04001006 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001009 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001012 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001013 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 sBgItemsIdMap.put(item.id, item);
1015 switch (item.itemType) {
1016 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1017 sBgFolders.put(item.id, (FolderInfo) item);
1018 // Fall through
1019 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1020 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1021 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1022 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1023 sBgWorkspaceItems.add(item);
1024 } else {
1025 if (!sBgFolders.containsKey(item.container)) {
1026 // Adding an item to a folder that doesn't exist.
1027 String msg = "adding item: " + item + " to a folder that " +
1028 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001029 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001031 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 break;
1033 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1034 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1035 break;
1036 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001037 }
1038 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001039 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001041 }
1042
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001044 * Creates a new unique child id, for a given cell span across all layouts.
1045 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001046 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001047 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001048 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001050 }
1051
Sunny Goyal34942622014-08-29 17:20:55 -07001052 private static ArrayList<ItemInfo> getItemsByPackageName(
1053 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001054 ItemInfoFilter filter = new ItemInfoFilter() {
1055 @Override
1056 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1057 return cn.getPackageName().equals(pn) && info.user.equals(user);
1058 }
1059 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001060 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001061 }
1062
1063 /**
1064 * Removes all the items from the database corresponding to the specified package.
1065 */
1066 static void deletePackageFromDatabase(Context context, final String pn,
1067 final UserHandleCompat user) {
1068 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001069 }
1070
1071 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001072 * Removes the specified item from the database
1073 * @param context
1074 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001076 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1078 items.add(item);
1079 deleteItemsFromDatabase(context, items);
1080 }
1081
1082 /**
1083 * Removes the specified items from the database
1084 * @param context
1085 * @param item
1086 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001087 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001089 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001090 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001091 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001092 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001093 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001094
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 // Lock on mBgLock *after* the db operation
1096 synchronized (sBgLock) {
1097 switch (item.itemType) {
1098 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1099 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001100 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 if (info.container == item.id) {
1102 // We are deleting a folder which still contains items that
1103 // think they are contained by that folder.
1104 String msg = "deleting a folder (" + item + ") which still " +
1105 "contains items (" + info + ")";
1106 Log.e(TAG, msg);
1107 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001109 sBgWorkspaceItems.remove(item);
1110 break;
1111 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1112 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1113 sBgWorkspaceItems.remove(item);
1114 break;
1115 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1116 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1117 break;
1118 }
1119 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 }
1122 }
Michael Jurka83df1882011-08-31 20:59:26 -07001123 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 }
1126
1127 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 * Update the order of the workspace screens in the database. The array list contains
1129 * a list of screen ids in the order that they should appear.
1130 */
Winson Chungc9168342013-06-26 14:54:55 -07001131 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001132 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001133 final ContentResolver cr = context.getContentResolver();
1134 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1135
1136 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001137 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 while (iter.hasNext()) {
1139 long id = iter.next();
1140 if (id < 0) {
1141 iter.remove();
1142 }
1143 }
1144
1145 Runnable r = new Runnable() {
1146 @Override
1147 public void run() {
Yura085c8532014-02-11 15:15:29 +00001148 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001150 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001151 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 for (int i = 0; i < count; i++) {
1153 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001154 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001155 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1156 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001157 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001158 }
Yura085c8532014-02-11 15:15:29 +00001159
1160 try {
1161 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1162 } catch (Exception ex) {
1163 throw new RuntimeException(ex);
1164 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001165
Winson Chungba9c37f2013-08-30 14:11:37 -07001166 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001167 sBgWorkspaceScreens.clear();
1168 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001169 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 }
1171 };
1172 runOnWorkerThread(r);
1173 }
1174
1175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 * Remove the contents of the specified folder from the database
1177 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001178 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 final ContentResolver cr = context.getContentResolver();
1180
Michael Jurkac9d95c52011-08-29 14:03:34 -07001181 Runnable r = new Runnable() {
1182 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001183 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001185 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 sBgItemsIdMap.remove(info.id);
1187 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 sBgWorkspaceItems.remove(info);
1189 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001190
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001191 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001192 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001194 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 for (ItemInfo childInfo : info.contents) {
1196 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001198 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001199 }
1200 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203
1204 /**
1205 * Set this as the current Launcher activity object for the loader.
1206 */
1207 public void initialize(Callbacks callbacks) {
1208 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001209 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1210 // workspace to prevent leaking Launcher activities on orientation change.
1211 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 mCallbacks = new WeakReference<Callbacks>(callbacks);
1213 }
1214 }
1215
Kenny Guyed131872014-04-30 03:02:21 +01001216 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001217 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001218 int op = PackageUpdatedTask.OP_UPDATE;
1219 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1220 user));
1221 }
1222
1223 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001224 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001225 int op = PackageUpdatedTask.OP_REMOVE;
1226 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1227 user));
1228 }
1229
1230 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001231 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001232 int op = PackageUpdatedTask.OP_ADD;
1233 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1234 user));
1235 }
1236
1237 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001238 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001239 boolean replacing) {
1240 if (!replacing) {
1241 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1242 user));
1243 if (mAppsCanBeOnRemoveableStorage) {
1244 // Only rebind if we support removable storage. It catches the
1245 // case where
1246 // apps on the external sd card need to be reloaded
1247 startLoaderFromBackground();
1248 }
1249 } else {
1250 // If we are replacing then just update the packages in the list
1251 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1252 packageNames, user));
1253 }
1254 }
1255
1256 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001257 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001258 boolean replacing) {
1259 if (!replacing) {
1260 enqueuePackageUpdated(new PackageUpdatedTask(
1261 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1262 user));
1263 }
Kenny Guyed131872014-04-30 03:02:21 +01001264 }
1265
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001266 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1268 * ACTION_PACKAGE_CHANGED.
1269 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001270 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001271 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001272 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001273
Joe Onorato36115782010-06-17 13:28:48 -04001274 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001275 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 // If we have changed locale we need to clear out the labels in all apps/workspace.
1277 forceReload();
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001278 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1279 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001280 Callbacks callbacks = getCallback();
1281 if (callbacks != null) {
1282 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001283 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001284 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1285 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1286 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001287 }
1288 }
1289
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001290 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001291 resetLoadedState(true, true);
1292
Reena Lee93f824a2011-09-23 17:20:28 -07001293 // Do this here because if the launcher activity is running it will be restarted.
1294 // If it's not running startLoaderFromBackground will merely tell it that it needs
1295 // to reload.
1296 startLoaderFromBackground();
1297 }
1298
Winson Chungf0c6ae02012-03-21 16:10:31 -07001299 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1300 synchronized (mLock) {
1301 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1302 // mWorkspaceLoaded to true later
1303 stopLoaderLocked();
1304 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1305 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1306 }
1307 }
1308
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001309 /**
1310 * When the launcher is in the background, it's possible for it to miss paired
1311 * configuration changes. So whenever we trigger the loader from the background
1312 * tell the launcher that it needs to re-run the loader when it comes back instead
1313 * of doing it now.
1314 */
1315 public void startLoaderFromBackground() {
1316 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001317 Callbacks callbacks = getCallback();
1318 if (callbacks != null) {
1319 // Only actually run the loader if they're not paused.
1320 if (!callbacks.setLoadOnResume()) {
1321 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001322 }
1323 }
1324 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001325 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001326 }
Joe Onorato36115782010-06-17 13:28:48 -04001327 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001328
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001329 /**
1330 * If there is already a loader task running, tell it to stop.
1331 */
1332 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001333 LoaderTask oldTask = mLoaderTask;
1334 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001335 oldTask.stopLocked();
1336 }
Reena Lee93f824a2011-09-23 17:20:28 -07001337 }
1338
Adam Cohen1a85c582014-09-30 09:48:49 -07001339 public boolean isCurrentCallbacks(Callbacks callbacks) {
1340 return (mCallbacks != null && mCallbacks.get() == callbacks);
1341 }
1342
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001343 public void startLoader(int synchronousBindPage) {
1344 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001345 }
1346
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001347 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001348 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1349 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001350 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001351 // Clear any deferred bind-runnables from the synchronized load process
1352 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001353 synchronized (mDeferredBindRunnables) {
1354 mDeferredBindRunnables.clear();
1355 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356
Joe Onorato36115782010-06-17 13:28:48 -04001357 // Don't bother to start the thread if we know it's not going to do anything
1358 if (mCallbacks != null && mCallbacks.get() != null) {
1359 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001360 stopLoaderLocked();
1361 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001362 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001363 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1365 } else {
1366 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1367 sWorker.post(mLoaderTask);
1368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
1371 }
1372
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 void bindRemainingSynchronousPages() {
1374 // Post the remaining side pages to be loaded
1375 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001376 Runnable[] deferredBindRunnables = null;
1377 synchronized (mDeferredBindRunnables) {
1378 deferredBindRunnables = mDeferredBindRunnables.toArray(
1379 new Runnable[mDeferredBindRunnables.size()]);
1380 mDeferredBindRunnables.clear();
1381 }
1382 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001383 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001385 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001386
1387 // Run all the bind complete runnables after workspace is bound.
1388 if (!mBindCompleteRunnables.isEmpty()) {
1389 synchronized (mBindCompleteRunnables) {
1390 for (final Runnable r : mBindCompleteRunnables) {
1391 runOnWorkerThread(r);
1392 }
1393 mBindCompleteRunnables.clear();
1394 }
1395 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001396 }
1397
Joe Onorato36115782010-06-17 13:28:48 -04001398 public void stopLoader() {
1399 synchronized (mLock) {
1400 if (mLoaderTask != null) {
1401 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 }
1403 }
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001406 /**
1407 * Loads the workspace screen ids in an ordered list.
1408 */
Adam Cohen091440a2015-03-18 14:16:05 -07001409 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001410 final ContentResolver contentResolver = context.getContentResolver();
1411 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001412
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001413 // Get screens ordered by rank.
1414 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1415 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1416 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001417 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001418 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001419 while (sc.moveToNext()) {
1420 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001421 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001422 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001423 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1424 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001425 }
1426 }
1427 } finally {
1428 sc.close();
1429 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001431 }
1432
Michael Jurkac57b7a82011-08-09 22:02:20 -07001433 public boolean isAllAppsLoaded() {
1434 return mAllAppsLoaded;
1435 }
1436
Winson Chung36a62fe2012-05-06 18:04:42 -07001437 boolean isLoadingWorkspace() {
1438 synchronized (mLock) {
1439 if (mLoaderTask != null) {
1440 return mLoaderTask.isLoadingWorkspace();
1441 }
1442 }
1443 return false;
1444 }
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 /**
1447 * Runnable for the thread that loads the contents of the launcher:
1448 * - workspace icons
1449 * - widgets
1450 * - all apps icons
1451 */
1452 private class LoaderTask implements Runnable {
1453 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001454 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001455 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001456 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001457 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001459 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001460 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001461 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 }
1463
Winson Chung36a62fe2012-05-06 18:04:42 -07001464 boolean isLoadingWorkspace() {
1465 return mIsLoadingAndBindingWorkspace;
1466 }
1467
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001468 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001469 mIsLoadingAndBindingWorkspace = true;
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001475
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001476 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001477 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001478 synchronized (LoaderTask.this) {
1479 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001480 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001481 }
1482 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 }
1484 }
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001487 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001489
Joe Onorato36115782010-06-17 13:28:48 -04001490 private void waitForIdle() {
1491 // Wait until the either we're stopped or the other threads are done.
1492 // This way we don't start loading all apps until the workspace has settled
1493 // down.
1494 synchronized (LoaderTask.this) {
1495 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001496
Joe Onorato36115782010-06-17 13:28:48 -04001497 mHandler.postIdle(new Runnable() {
1498 public void run() {
1499 synchronized (LoaderTask.this) {
1500 mLoadAndBindStepFinished = true;
1501 if (DEBUG_LOADERS) {
1502 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001505 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001506 }
Joe Onorato36115782010-06-17 13:28:48 -04001507 });
1508
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001509 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001510 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001511 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1512 // wait no longer than 1sec at a time
1513 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001514 } catch (InterruptedException ex) {
1515 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001516 }
1517 }
Joe Onorato36115782010-06-17 13:28:48 -04001518 if (DEBUG_LOADERS) {
1519 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001520 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001521 + "ms for previous step to finish binding");
1522 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001523 }
Joe Onorato36115782010-06-17 13:28:48 -04001524 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001525
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001527 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 // Ensure that we have a valid page index to load synchronously
1529 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1530 "valid page index");
1531 }
1532 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1533 // Ensure that we don't try and bind a specified page when the pages have not been
1534 // loaded already (we should load everything asynchronously in that case)
1535 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1536 }
1537 synchronized (mLock) {
1538 if (mIsLoaderTaskRunning) {
1539 // Ensure that we are never running the background loading at this point since
1540 // we also touch the background collections
1541 throw new RuntimeException("Error! Background loading is already running");
1542 }
1543 }
1544
1545 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1546 // data structures, we can't allow any other thread to touch that data, but because
1547 // this call is synchronous, we can get away with not locking).
1548
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001549 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001550 // operations from the previous activity. We need to ensure that all queued operations
1551 // are executed before any synchronous binding work is done.
1552 mHandler.flush();
1553
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001554 // Divide the set of loaded items into those that we are binding synchronously, and
1555 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001556 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001557 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1558 // arise from that.
1559 onlyBindAllApps();
1560 }
1561
Joe Onorato36115782010-06-17 13:28:48 -04001562 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001563 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001564 if (mStopped) {
1565 return;
1566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 mIsLoaderTaskRunning = true;
1568 }
Joe Onorato36115782010-06-17 13:28:48 -04001569 // Optimize for end-user experience: if the Launcher is up and // running with the
1570 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1571 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001572 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001573 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001574 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001575
Joe Onorato36115782010-06-17 13:28:48 -04001576 if (mStopped) {
1577 break keep_running;
1578 }
1579
Joe Onorato36115782010-06-17 13:28:48 -04001580 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001581
1582 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001583 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1584 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586
Joe Onorato36115782010-06-17 13:28:48 -04001587 // Clear out this reference, otherwise we end up holding it until all of the
1588 // callback runnables are done.
1589 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590
Joe Onorato36115782010-06-17 13:28:48 -04001591 synchronized (mLock) {
1592 // If we are still the last one to be scheduled, remove ourselves.
1593 if (mLoaderTask == this) {
1594 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001596 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001597 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
1600
1601 public void stopLocked() {
1602 synchronized (LoaderTask.this) {
1603 mStopped = true;
1604 this.notify();
1605 }
1606 }
1607
1608 /**
1609 * Gets the callbacks object. If we've been stopped, or if the launcher object
1610 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1611 * object that was around when the deferred message was scheduled, and if there's
1612 * a new Callbacks object around then also return null. This will save us from
1613 * calling onto it with data that will be ignored.
1614 */
1615 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1616 synchronized (mLock) {
1617 if (mStopped) {
1618 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001619 }
Joe Onorato36115782010-06-17 13:28:48 -04001620
1621 if (mCallbacks == null) {
1622 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001623 }
Joe Onorato36115782010-06-17 13:28:48 -04001624
1625 final Callbacks callbacks = mCallbacks.get();
1626 if (callbacks != oldCallbacks) {
1627 return null;
1628 }
1629 if (callbacks == null) {
1630 Log.w(TAG, "no mCallbacks");
1631 return null;
1632 }
1633
1634 return callbacks;
1635 }
1636 }
1637
1638 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001639 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001640 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001641 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1642 final int countX = (int) profile.numColumns;
1643 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001644
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001646 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001647 // Return early if we detect that an item is under the hotseat button
1648 if (mCallbacks == null ||
1649 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001650 Log.e(TAG, "Error loading shortcut into hotseat " + item
1651 + " into position (" + item.screenId + ":" + item.cellX + ","
1652 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001653 return false;
1654 }
1655
Dan Sandler295ae182013-12-10 16:05:47 -05001656 final ItemInfo[][] hotseatItems =
1657 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1658
Adam Cohen2e6da152015-05-06 11:42:25 -07001659 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001660 Log.e(TAG, "Error loading shortcut " + item
1661 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001662 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001663 + ")");
1664 return false;
1665 }
1666
Dan Sandler295ae182013-12-10 16:05:47 -05001667 if (hotseatItems != null) {
1668 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 Log.e(TAG, "Error loading shortcut into hotseat " + item
1670 + " into position (" + item.screenId + ":" + item.cellX + ","
1671 + item.cellY + ") occupied by "
1672 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1673 [(int) item.screenId][0]);
1674 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001675 } else {
1676 hotseatItems[(int) item.screenId][0] = item;
1677 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001679 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001680 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001681 items[(int) item.screenId][0] = item;
1682 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001683 return true;
1684 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001685 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1686 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001687 return true;
1688 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001689
Adam Cohendcd297f2013-06-18 13:13:40 -07001690 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001691 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 occupied.put(item.screenId, items);
1693 }
1694
Dan Sandler295ae182013-12-10 16:05:47 -05001695 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001696 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1697 item.cellX < 0 || item.cellY < 0 ||
1698 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1699 Log.e(TAG, "Error loading shortcut " + item
1700 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1701 + item.cellX + "," + item.cellY
1702 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1703 return false;
1704 }
1705
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001706 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001707 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1708 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001709 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001710 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001712 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001713 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001715 return false;
1716 }
1717 }
1718 }
1719 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1720 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001722 }
1723 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001724
Joe Onorato36115782010-06-17 13:28:48 -04001725 return true;
1726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungba9c37f2013-08-30 14:11:37 -07001728 /** Clears all the sBg data structures */
1729 private void clearSBgDataStructures() {
1730 synchronized (sBgLock) {
1731 sBgWorkspaceItems.clear();
1732 sBgAppWidgets.clear();
1733 sBgFolders.clear();
1734 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001735 sBgWorkspaceScreens.clear();
1736 }
1737 }
1738
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001739 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001740 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741
Joe Onorato36115782010-06-17 13:28:48 -04001742 final Context context = mContext;
1743 final ContentResolver contentResolver = context.getContentResolver();
1744 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001745 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001746 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1747 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001748 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001749
Winson Chung892c74d2013-08-22 16:15:50 -07001750 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001751 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1752 int countX = (int) profile.numColumns;
1753 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001754
Dan Sandlerd5024042014-01-09 15:01:33 -05001755 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1756 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1757 LauncherAppState.getLauncherProvider().deleteDatabase();
1758 }
1759
1760 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1761 // append the user's Launcher2 shortcuts
1762 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1763 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1764 } else {
1765 // Make sure the default workspace is loaded
1766 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001767 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001768 }
Adam Cohene25af792013-06-06 23:08:25 -07001769
Winson Chung2abf94d2012-07-18 18:16:38 -07001770 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001771 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001772 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001773 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001776 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001777 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001778 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001779 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001780
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 // +1 for the hotseat (it can be larger than the workspace)
1782 // Load workspace in reverse order to ensure that latest items are loaded first (and
1783 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001784 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 try {
1787 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1788 final int intentIndex = c.getColumnIndexOrThrow
1789 (LauncherSettings.Favorites.INTENT);
1790 final int titleIndex = c.getColumnIndexOrThrow
1791 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 final int containerIndex = c.getColumnIndexOrThrow(
1793 LauncherSettings.Favorites.CONTAINER);
1794 final int itemTypeIndex = c.getColumnIndexOrThrow(
1795 LauncherSettings.Favorites.ITEM_TYPE);
1796 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1797 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001798 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1799 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 final int screenIndex = c.getColumnIndexOrThrow(
1801 LauncherSettings.Favorites.SCREEN);
1802 final int cellXIndex = c.getColumnIndexOrThrow
1803 (LauncherSettings.Favorites.CELLX);
1804 final int cellYIndex = c.getColumnIndexOrThrow
1805 (LauncherSettings.Favorites.CELLY);
1806 final int spanXIndex = c.getColumnIndexOrThrow
1807 (LauncherSettings.Favorites.SPANX);
1808 final int spanYIndex = c.getColumnIndexOrThrow(
1809 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001810 final int rankIndex = c.getColumnIndexOrThrow(
1811 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001812 final int restoredIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001814 final int profileIdIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001816 final int optionsIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001818 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819
Sunny Goyal7f834d22015-04-21 10:10:23 -07001820 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1821 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1822 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1823 }
1824
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 ShortcutInfo info;
1826 String intentDescription;
1827 LauncherAppWidgetInfo appWidgetInfo;
1828 int container;
1829 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001830 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001832 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 while (!mStopped && c.moveToNext()) {
1835 try {
1836 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001837 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001838 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001839 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 switch (itemType) {
1842 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1843 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001844 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001846 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001847 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001848 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001849 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001850 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001851 if (user == null) {
1852 // User has been deleted remove the item.
1853 itemsToRemove.add(id);
1854 continue;
1855 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 try {
1857 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001858 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001859 if (cn != null && cn.getPackageName() != null) {
1860 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1861 cn.getPackageName(), user);
1862 boolean validComponent = validPkg &&
1863 launcherApps.isActivityEnabledForProfile(cn, user);
1864
1865 if (validComponent) {
1866 if (restored) {
1867 // no special handling necessary for this item
1868 restoredRows.add(id);
1869 restored = false;
1870 }
1871 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001872 intent = null;
1873 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1874 // We allow auto install apps to have their intent
1875 // updated after an install.
1876 intent = manager.getLaunchIntentForPackage(
1877 cn.getPackageName());
1878 if (intent != null) {
1879 ContentValues values = new ContentValues();
1880 values.put(LauncherSettings.Favorites.INTENT,
1881 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001882 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001883 }
1884 }
1885
1886 if (intent == null) {
1887 // The app is installed but the component is no
1888 // longer available.
1889 Launcher.addDumpLog(TAG,
1890 "Invalid component removed: " + cn, true);
1891 itemsToRemove.add(id);
1892 continue;
1893 } else {
1894 // no special handling necessary for this item
1895 restoredRows.add(id);
1896 restored = false;
1897 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001898 } else if (restored) {
1899 // Package is not yet available but might be
1900 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 Launcher.addDumpLog(TAG,
1902 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001903
1904 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1905 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001906 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001907 // App restore has started. Update the flag
1908 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1909 ContentValues values = new ContentValues();
1910 values.put(LauncherSettings.Favorites.RESTORED,
1911 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001912 updateItem(id, values);
1913 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1914 // This is a common app. Try to replace this.
1915 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1916 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1917 if (parser.findDefaultApp()) {
1918 // Default app found. Replace it.
1919 intent = parser.parsedIntent;
1920 cn = intent.getComponent();
1921 ContentValues values = parser.parsedValues;
1922 values.put(LauncherSettings.Favorites.RESTORED, 0);
1923 updateItem(id, values);
1924 restored = false;
1925 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001926
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001927 } else if (REMOVE_UNRESTORED_ICONS) {
1928 Launcher.addDumpLog(TAG,
1929 "Unrestored package removed: " + cn, true);
1930 itemsToRemove.add(id);
1931 continue;
1932 }
Sunny Goyal94485362014-09-18 16:13:58 -07001933 } else if (REMOVE_UNRESTORED_ICONS) {
1934 Launcher.addDumpLog(TAG,
1935 "Unrestored package removed: " + cn, true);
1936 itemsToRemove.add(id);
1937 continue;
1938 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001939 } else if (launcherApps.isAppEnabled(
1940 manager, cn.getPackageName(),
1941 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1942 // Package is present but not available.
1943 allowMissingTarget = true;
1944 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1945 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001946 // SdCard is not ready yet. Package might get available,
1947 // once it is ready.
1948 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1949 + " (check again later)", true);
1950 HashSet<String> pkgs = sPendingPackages.get(user);
1951 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001952 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001953 sPendingPackages.put(user, pkgs);
1954 }
1955 pkgs.add(cn.getPackageName());
1956 allowMissingTarget = true;
1957 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001958
1959 } else {
1960 // Do not wait for external media load anymore.
1961 // Log the invalid package, and remove it
1962 Launcher.addDumpLog(TAG,
1963 "Invalid package removed: " + cn, true);
1964 itemsToRemove.add(id);
1965 continue;
Winson Chungee055712013-07-30 14:46:24 -07001966 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001967 } else if (cn == null) {
1968 // For shortcuts with no component, keep them as they are
1969 restoredRows.add(id);
1970 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001971 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001973 Launcher.addDumpLog(TAG,
1974 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 continue;
1976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977
Sunny Goyal34b65272015-03-11 16:56:52 -07001978 boolean useLowResIcon = container >= 0 &&
1979 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1980
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001981 if (itemReplaced) {
1982 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001983 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001984 cursorIconInfo.iconIndex, titleIndex,
1985 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001986 } else {
1987 // Don't replace items for other profiles.
1988 itemsToRemove.add(id);
1989 continue;
1990 }
1991 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001992 if (user.equals(UserHandleCompat.myUserHandle())) {
1993 Launcher.addDumpLog(TAG,
1994 "constructing info for partially restored package",
1995 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001996 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001997 promiseType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001998 intent = getRestoredItemIntent(c, context, intent);
1999 } else {
2000 // Don't restore items for other profiles.
2001 itemsToRemove.add(id);
2002 continue;
2003 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002004 } else if (itemType ==
2005 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002006 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002007 cursorIconInfo.iconIndex, titleIndex,
2008 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 // App shortcuts that used to be automatically added to Launcher
2013 // didn't always have the correct intent flags set, so do that
2014 // here
2015 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002016 intent.getCategories() != null &&
2017 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002018 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 intent.addFlags(
2020 Intent.FLAG_ACTIVITY_NEW_TASK |
2021 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2022 }
Michael Jurka96879562012-03-22 05:54:33 -07002023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002026 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 info.cellX = c.getInt(cellXIndex);
2031 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002032 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002033 info.spanX = 1;
2034 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002035 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002036 if (info.promisedIntent != null) {
2037 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2038 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002039 info.isDisabled = disabledState;
2040 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2041 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2042 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002045 if (!checkItemPlacement(occupied, info)) {
2046 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 break;
2048 }
2049
Sunny Goyal756adbc2015-04-16 15:20:51 -07002050 if (restored) {
2051 ComponentName cn = info.getTargetComponent();
2052 if (cn != null) {
2053 Integer progress = installingPkgs.get(cn.getPackageName());
2054 if (progress != null) {
2055 info.setInstallProgress(progress);
2056 } else {
2057 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2058 }
2059 }
2060 }
2061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 switch (container) {
2063 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2064 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2065 sBgWorkspaceItems.add(info);
2066 break;
2067 default:
2068 // Item is in a user folder
2069 FolderInfo folderInfo =
2070 findOrMakeFolder(sBgFolders, container);
2071 folderInfo.add(info);
2072 break;
2073 }
2074 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002075 } else {
2076 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 }
2078 break;
2079
2080 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2081 id = c.getLong(idIndex);
2082 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2083
Sunny Goyala508e4f2015-05-21 09:33:57 -07002084 // Do not trim the folder label, as is was set by the user.
2085 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 folderInfo.cellX = c.getInt(cellXIndex);
2090 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002091 folderInfo.spanX = 1;
2092 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002093 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094
Daniel Sandler8802e962010-05-26 16:28:16 -04002095 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002096 if (!checkItemPlacement(occupied, folderInfo)) {
2097 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002098 break;
2099 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2103 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2104 sBgWorkspaceItems.add(folderInfo);
2105 break;
Joe Onorato36115782010-06-17 13:28:48 -04002106 }
Joe Onorato17a89222011-02-08 17:26:11 -08002107
Chris Wrenf4d08112014-01-16 18:13:56 -05002108 if (restored) {
2109 // no special handling required for restored folders
2110 restoredRows.add(id);
2111 }
2112
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2114 sBgFolders.put(folderInfo.id, folderInfo);
2115 break;
2116
2117 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002118 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002120 boolean customWidget = itemType ==
2121 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2122
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002124 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002125 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002126 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002127 user = allUsers.get(serialNumber);
2128 if (user == null) {
2129 itemsToRemove.add(id);
2130 continue;
2131 }
2132
Sunny Goyalff572272014-07-23 13:58:07 -07002133 final ComponentName component =
2134 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002135
Sunny Goyal651077b2014-06-30 14:15:31 -07002136 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002137 final boolean isIdValid = (restoreStatus &
2138 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002139 final boolean wasProviderReady = (restoreStatus &
2140 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002141
Adam Cohen59400422014-03-05 18:07:04 -08002142 final LauncherAppWidgetProviderInfo provider =
2143 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002144 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002145 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002146
2147 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002148 if (!isSafeMode && !customWidget &&
2149 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002150 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002151 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002152
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002154 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 itemsToRemove.add(id);
2156 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002157 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002158 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2159 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002160
Sunny Goyalff572272014-07-23 13:58:07 -07002161 int status = restoreStatus;
2162 if (!wasProviderReady) {
2163 // If provider was not previously ready, update the
2164 // status and UI flag.
2165
2166 // Id would be valid only if the widget restore broadcast was received.
2167 if (isIdValid) {
2168 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2169 } else {
2170 status &= ~LauncherAppWidgetInfo
2171 .FLAG_PROVIDER_NOT_READY;
2172 }
2173 }
2174 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002175 } else {
2176 Log.v(TAG, "Widget restore pending id=" + id
2177 + " appWidgetId=" + appWidgetId
2178 + " status =" + restoreStatus);
2179 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002180 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002182 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002183
2184 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2185 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002186 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002187 // App restore has started. Update the flag
2188 appWidgetInfo.restoreStatus |=
2189 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002190 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002191 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002192 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002193 itemsToRemove.add(id);
2194 continue;
2195 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002196
2197 appWidgetInfo.installProgress =
2198 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002199 }
Sunny Goyalff572272014-07-23 13:58:07 -07002200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 appWidgetInfo.cellX = c.getInt(cellXIndex);
2204 appWidgetInfo.cellY = c.getInt(cellYIndex);
2205 appWidgetInfo.spanX = c.getInt(spanXIndex);
2206 appWidgetInfo.spanY = c.getInt(spanYIndex);
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 Goyal05e318d2014-07-29 11:49:35 -07002287 new IntentFilter(StartupReceiver.SYSTEM_READY),
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 Chungb8b2a5a2012-07-12 17:55:31 -07002439 // XXX: review this
2440 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002441 @Override
2442 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002443 int cellCountX = (int) profile.numColumns;
2444 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002445 int screenOffset = cellCountX * cellCountY;
2446 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002447 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002448 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002449 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002450 rhs.cellY * cellCountX + rhs.cellX);
2451 return (int) (lr - rr);
2452 }
2453 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002455
Adam Cohendcd297f2013-06-18 13:13:40 -07002456 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2457 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002458 final Runnable r = new Runnable() {
2459 @Override
2460 public void run() {
2461 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2462 if (callbacks != null) {
2463 callbacks.bindScreens(orderedScreens);
2464 }
2465 }
2466 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002467 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 }
2469
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2471 final ArrayList<ItemInfo> workspaceItems,
2472 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002473 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002475
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 final boolean postOnMainThread = (deferredBindRunnables != null);
2477
2478 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002479 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002481 final int start = i;
2482 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 final Runnable r = new Runnable() {
2484 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002487 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002488 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2489 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 }
2491 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 };
2493 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002494 synchronized (deferredBindRunnables) {
2495 deferredBindRunnables.add(r);
2496 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002498 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 }
Joe Onorato36115782010-06-17 13:28:48 -04002500 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501
2502 // Bind the folders
2503 if (!folders.isEmpty()) {
2504 final Runnable r = new Runnable() {
2505 public void run() {
2506 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2507 if (callbacks != null) {
2508 callbacks.bindFolders(folders);
2509 }
2510 }
2511 };
2512 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002513 synchronized (deferredBindRunnables) {
2514 deferredBindRunnables.add(r);
2515 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002517 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 }
2519 }
2520
2521 // Bind the widgets, one at a time
2522 N = appWidgets.size();
2523 for (int i = 0; i < N; i++) {
2524 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2525 final Runnable r = new Runnable() {
2526 public void run() {
2527 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2528 if (callbacks != null) {
2529 callbacks.bindAppWidget(widget);
2530 }
2531 }
2532 };
2533 if (postOnMainThread) {
2534 deferredBindRunnables.add(r);
2535 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002536 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 }
2538 }
2539 }
2540
2541 /**
2542 * Binds all loaded data to actual views on the main thread.
2543 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002544 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 final long t = SystemClock.uptimeMillis();
2546 Runnable r;
2547
2548 // Don't use these two variables in any of the callback runnables.
2549 // Otherwise we hold a reference to them.
2550 final Callbacks oldCallbacks = mCallbacks.get();
2551 if (oldCallbacks == null) {
2552 // This launcher has exited and nobody bothered to tell us. Just bail.
2553 Log.w(TAG, "LoaderTask running with no launcher");
2554 return;
2555 }
2556
Winson Chung9b9fb962013-11-15 15:39:34 -08002557 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2559 ArrayList<LauncherAppWidgetInfo> appWidgets =
2560 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002561 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002562
2563 final LongArrayMap<FolderInfo> folders;
2564 final LongArrayMap<ItemInfo> itemsIdMap;
2565
Winson Chung2abf94d2012-07-18 18:16:38 -07002566 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 workspaceItems.addAll(sBgWorkspaceItems);
2568 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002569 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002570
2571 folders = sBgFolders.clone();
2572 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 }
2574
Derek Prothro7aff3992013-12-10 14:00:37 -05002575 final boolean isLoadingSynchronously =
2576 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002577 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002578 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002579 if (currScreen >= orderedScreenIds.size()) {
2580 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002581 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002582 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002583 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002584 final long currentScreenId = currentScreen < 0
2585 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002586
2587 // Load all the items that are on the current page first (and in the process, unbind
2588 // all the existing workspace items before we call startBinding() below.
2589 unbindWorkspaceItemsOnMainThread();
2590
2591 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2593 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2594 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2595 new ArrayList<LauncherAppWidgetInfo>();
2596 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2597 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002598 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2599 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002600
Winson Chung9b9fb962013-11-15 15:39:34 -08002601 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002602 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002603 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002604 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002605 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002606 otherFolders);
2607 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2608 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2609
2610 // Tell the workspace that we're about to start binding items
2611 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002612 public void run() {
2613 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2614 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002615 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002616 }
2617 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002618 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002619 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002620
Adam Cohendcd297f2013-06-18 13:13:40 -07002621 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2622
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 // Load items on the current page
2624 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2625 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002626 if (isLoadingSynchronously) {
2627 r = new Runnable() {
2628 public void run() {
2629 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002630 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002631 callbacks.onPageBoundSynchronously(currentScreen);
2632 }
2633 }
2634 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002635 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002636 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637
Winson Chung4a2afa32012-07-19 14:53:05 -07002638 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2639 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002640 synchronized (mDeferredBindRunnables) {
2641 mDeferredBindRunnables.clear();
2642 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002644 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645
2646 // Tell the workspace that we're done binding items
2647 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002648 public void run() {
2649 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2650 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002651 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002652 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002653
Winson Chung98e030b2012-05-07 16:01:11 -07002654 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002655 if (DEBUG_LOADERS) {
2656 Log.d(TAG, "bound workspace in "
2657 + (SystemClock.uptimeMillis()-t) + "ms");
2658 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002659
2660 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002663 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002664 synchronized (mDeferredBindRunnables) {
2665 mDeferredBindRunnables.add(r);
2666 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002667 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002668 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002669 }
Joe Onorato36115782010-06-17 13:28:48 -04002670 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002671
Joe Onorato36115782010-06-17 13:28:48 -04002672 private void loadAndBindAllApps() {
2673 if (DEBUG_LOADERS) {
2674 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2675 }
2676 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002677 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002678 synchronized (LoaderTask.this) {
2679 if (mStopped) {
2680 return;
2681 }
2682 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002683 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002684 synchronized (LoaderTask.this) {
2685 if (mStopped) {
2686 return;
2687 }
2688 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002689 }
Joe Onorato36115782010-06-17 13:28:48 -04002690 } else {
2691 onlyBindAllApps();
2692 }
2693 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002694
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002695 private void updateIconCache() {
2696 // Ignore packages which have a promise icon.
2697 HashSet<String> packagesToIgnore = new HashSet<>();
2698 synchronized (sBgLock) {
2699 for (ItemInfo info : sBgItemsIdMap) {
2700 if (info instanceof ShortcutInfo) {
2701 ShortcutInfo si = (ShortcutInfo) info;
2702 if (si.isPromise() && si.getTargetComponent() != null) {
2703 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2704 }
2705 } else if (info instanceof LauncherAppWidgetInfo) {
2706 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2707 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2708 packagesToIgnore.add(lawi.providerName.getPackageName());
2709 }
2710 }
2711 }
2712 }
2713 mIconCache.updateDbIcons(packagesToIgnore);
2714 }
2715
Joe Onorato36115782010-06-17 13:28:48 -04002716 private void onlyBindAllApps() {
2717 final Callbacks oldCallbacks = mCallbacks.get();
2718 if (oldCallbacks == null) {
2719 // This launcher has exited and nobody bothered to tell us. Just bail.
2720 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2721 return;
2722 }
2723
2724 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002725 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002726 final ArrayList<AppInfo> list
2727 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002728 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002729 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002730 public void run() {
2731 final long t = SystemClock.uptimeMillis();
2732 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2733 if (callbacks != null) {
2734 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002735 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002736 }
2737 if (DEBUG_LOADERS) {
2738 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2739 + (SystemClock.uptimeMillis()-t) + "ms");
2740 }
2741 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002742 };
2743 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002744 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002745 r.run();
2746 } else {
2747 mHandler.post(r);
2748 }
Joe Onorato36115782010-06-17 13:28:48 -04002749 }
2750
Winson Chung64359a52013-07-08 17:17:08 -07002751 private void loadAllApps() {
2752 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002753
Joe Onorato36115782010-06-17 13:28:48 -04002754 final Callbacks oldCallbacks = mCallbacks.get();
2755 if (oldCallbacks == null) {
2756 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002757 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002758 return;
2759 }
2760
Kenny Guyed131872014-04-30 03:02:21 +01002761 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2762
Winson Chung64359a52013-07-08 17:17:08 -07002763 // Clear the list of apps
2764 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002765 for (UserHandleCompat user : profiles) {
2766 // Query for the set of apps
2767 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002768 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002769 if (DEBUG_LOADERS) {
2770 Log.d(TAG, "getActivityList took "
2771 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2772 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2773 }
2774 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002775 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002776 if (apps == null || apps.isEmpty()) {
2777 return;
2778 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002779
Kenny Guyed131872014-04-30 03:02:21 +01002780 // Create the ApplicationInfos
2781 for (int i = 0; i < apps.size(); i++) {
2782 LauncherActivityInfoCompat app = apps.get(i);
2783 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002784 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002785 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002786
Sunny Goyal756a28a2015-04-23 17:07:55 -07002787 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2788 if (heuristic != null) {
2789 runAfterBindCompletes(new Runnable() {
2790
2791 @Override
2792 public void run() {
2793 heuristic.processUserApps(apps);
2794 }
2795 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002796 }
Winson Chung64359a52013-07-08 17:17:08 -07002797 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002798 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002799 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2800 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002801
2802 // Post callback on main thread
2803 mHandler.post(new Runnable() {
2804 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002805
Winson Chung64359a52013-07-08 17:17:08 -07002806 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002807 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002808 if (callbacks != null) {
2809 callbacks.bindAllApplications(added);
2810 if (DEBUG_LOADERS) {
2811 Log.d(TAG, "bound " + added.size() + " apps in "
2812 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2813 }
2814 } else {
2815 Log.i(TAG, "not binding apps: no Launcher activity");
2816 }
2817 }
2818 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002819 // Cleanup any data stored for a deleted user.
2820 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002821
Hyunyoung Song219d0482015-05-07 12:51:42 -07002822 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2823 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002824 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002825 Log.d(TAG, "Icons processed in "
2826 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002827 }
2828 }
2829
2830 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002831 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002832 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002833 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2834 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2835 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2836 }
Joe Onorato36115782010-06-17 13:28:48 -04002837 }
2838 }
2839
Sunny Goyal75b0f552015-05-20 21:57:06 -07002840 /**
2841 * Called when the icons for packages have been updated in the icon cache.
2842 */
2843 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2844 final Callbacks callbacks = getCallback();
2845 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2846 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2847
2848 // If any package icon has changed (app was updated while launcher was dead),
2849 // update the corresponding shortcuts.
2850 synchronized (sBgLock) {
2851 for (ItemInfo info : sBgItemsIdMap) {
2852 if (info instanceof ShortcutInfo && user.equals(info.user)
2853 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2854 ShortcutInfo si = (ShortcutInfo) info;
2855 ComponentName cn = si.getTargetComponent();
2856 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2857 si.updateIcon(mIconCache);
2858 updatedShortcuts.add(si);
2859 }
2860 }
2861 }
2862 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2863 }
2864
2865 if (!updatedShortcuts.isEmpty()) {
2866 final UserHandleCompat userFinal = user;
2867 mHandler.post(new Runnable() {
2868
2869 public void run() {
2870 Callbacks cb = getCallback();
2871 if (cb != null && callbacks == cb) {
2872 cb.bindShortcutsChanged(updatedShortcuts,
2873 new ArrayList<ShortcutInfo>(), userFinal);
2874 }
2875 }
2876 });
2877 }
2878
2879 if (!updatedApps.isEmpty()) {
2880 mHandler.post(new Runnable() {
2881
2882 public void run() {
2883 Callbacks cb = getCallback();
2884 if (cb != null && callbacks == cb) {
2885 cb.bindAppsUpdated(updatedApps);
2886 }
2887 }
2888 });
2889 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002890
2891 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2892 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002893 }
2894
Joe Onorato36115782010-06-17 13:28:48 -04002895 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002896 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002897 }
2898
Adam Cohen091440a2015-03-18 14:16:05 -07002899 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002900
2901 @Override
2902 public void onReceive(Context context, Intent intent) {
2903 synchronized (sBgLock) {
2904 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2905 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002906 final PackageManager manager = context.getPackageManager();
2907 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2908 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002909 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2910 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002911 packagesRemoved.clear();
2912 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002913 for (String pkg : entry.getValue()) {
2914 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002915 boolean packageOnSdcard = launcherApps.isAppEnabled(
2916 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2917 if (packageOnSdcard) {
2918 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2919 packagesUnavailable.add(pkg);
2920 } else {
2921 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2922 packagesRemoved.add(pkg);
2923 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002924 }
2925 }
2926 if (!packagesRemoved.isEmpty()) {
2927 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2928 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2929 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002930 if (!packagesUnavailable.isEmpty()) {
2931 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2932 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2933 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002934 }
Sunny Goyal34942622014-08-29 17:20:55 -07002935 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002936 }
2937 }
2938 }
2939
Joe Onorato36115782010-06-17 13:28:48 -04002940 private class PackageUpdatedTask implements Runnable {
2941 int mOp;
2942 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002943 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002944
2945 public static final int OP_NONE = 0;
2946 public static final int OP_ADD = 1;
2947 public static final int OP_UPDATE = 2;
2948 public static final int OP_REMOVE = 3; // uninstlled
2949 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2950
2951
Kenny Guyed131872014-04-30 03:02:21 +01002952 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002953 mOp = op;
2954 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002955 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002956 }
2957
2958 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002959 if (!mHasLoaderCompletedOnce) {
2960 // Loader has not yet run.
2961 return;
2962 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002963 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002964
2965 final String[] packages = mPackages;
2966 final int N = packages.length;
2967 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002968 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002969 for (int i=0; i<N; i++) {
2970 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002971 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002972 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002974
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002975 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2976 if (heuristic != null) {
2977 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002978 }
Joe Onorato36115782010-06-17 13:28:48 -04002979 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002980 }
Joe Onorato36115782010-06-17 13:28:48 -04002981 case OP_UPDATE:
2982 for (int i=0; i<N; i++) {
2983 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002984 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002985 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002986 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002987 }
2988 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002989 case OP_REMOVE: {
2990 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2991 if (heuristic != null) {
2992 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002993 }
Joe Onorato36115782010-06-17 13:28:48 -04002994 for (int i=0; i<N; i++) {
2995 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002996 mIconCache.removeIconsForPkg(packages[i], mUser);
2997 }
2998 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002999 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003000 case OP_UNAVAILABLE:
3001 for (int i=0; i<N; i++) {
3002 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3003 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003004 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003005 }
3006 break;
3007 }
3008
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003009 ArrayList<AppInfo> added = null;
3010 ArrayList<AppInfo> modified = null;
3011 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003012
Adam Cohen487f7dd2012-06-28 18:12:10 -07003013 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003014 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003015 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003016 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003017 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003018 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003019 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003020 }
Winson Chung5d55f332012-07-16 20:45:03 -07003021 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003022 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003023 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003024 }
3025
Sunny Goyale0f58d72014-11-10 18:05:31 -08003026 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003027 if (callbacks == null) {
3028 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3029 return;
3030 }
3031
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3033 new HashMap<ComponentName, AppInfo>();
3034
Joe Onorato36115782010-06-17 13:28:48 -04003035 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003036 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003037 for (AppInfo ai : added) {
3038 addedOrUpdatedApps.put(ai.componentName, ai);
3039 }
Joe Onorato36115782010-06-17 13:28:48 -04003040 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003041
Joe Onorato36115782010-06-17 13:28:48 -04003042 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003043 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003044 for (AppInfo ai : modified) {
3045 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003046 }
3047
Joe Onorato36115782010-06-17 13:28:48 -04003048 mHandler.post(new Runnable() {
3049 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003050 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003051 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003052 callbacks.bindAppsUpdated(modifiedFinal);
3053 }
3054 }
3055 });
3056 }
Winson Chung83892cc2013-05-01 16:53:33 -07003057
Sunny Goyal4390ace2014-10-13 11:33:11 -07003058 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003059 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003060 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3061 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3062 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3063
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003064 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003065 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003066 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003067 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3068 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003069 boolean infoUpdated = false;
3070 boolean shortcutUpdated = false;
3071
3072 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003073 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003074 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003075 Bitmap icon = Utilities.createIconBitmap(
3076 si.iconResource.packageName,
3077 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003078 if (icon != null) {
3079 si.setIcon(icon);
3080 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003081 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 }
3083 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084
3085 ComponentName cn = si.getTargetComponent();
3086 if (cn != null && packageSet.contains(cn.getPackageName())) {
3087 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3088
3089 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3091 // Auto install icon
3092 PackageManager pm = context.getPackageManager();
3093 ResolveInfo matched = pm.resolveActivity(
3094 new Intent(Intent.ACTION_MAIN)
3095 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3096 PackageManager.MATCH_DEFAULT_ONLY);
3097 if (matched == null) {
3098 // Try to find the best match activity.
3099 Intent intent = pm.getLaunchIntentForPackage(
3100 cn.getPackageName());
3101 if (intent != null) {
3102 cn = intent.getComponent();
3103 appInfo = addedOrUpdatedApps.get(cn);
3104 }
3105
3106 if ((intent == null) || (appInfo == null)) {
3107 removedShortcuts.add(si);
3108 continue;
3109 }
3110 si.promisedIntent = intent;
3111 }
3112 }
3113
3114 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003115 if (appInfo != null) {
3116 si.flags = appInfo.flags;
3117 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118
Sunny Goyal756adbc2015-04-16 15:20:51 -07003119 si.intent = si.promisedIntent;
3120 si.promisedIntent = null;
3121 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 infoUpdated = true;
3123 si.updateIcon(mIconCache);
3124 }
3125
3126 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3127 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3128 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003129 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 si.contentDescription = appInfo.contentDescription;
3131 infoUpdated = true;
3132 }
3133
3134 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3135 // Since package was just updated, the target must be available now.
3136 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3137 shortcutUpdated = true;
3138 }
3139 }
3140
3141 if (infoUpdated || shortcutUpdated) {
3142 updatedShortcuts.add(si);
3143 }
3144 if (infoUpdated) {
3145 updateItemInDatabase(context, si);
3146 }
3147 } else if (info instanceof LauncherAppWidgetInfo) {
3148 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3149 if (mUser.equals(widgetInfo.user)
3150 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3151 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3152 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3153 widgets.add(widgetInfo);
3154 updateItemInDatabase(context, widgetInfo);
3155 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003156 }
3157 }
3158 }
3159
Sunny Goyal4390ace2014-10-13 11:33:11 -07003160 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3161 mHandler.post(new Runnable() {
3162
3163 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003164 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165 if (callbacks == cb && cb != null) {
3166 callbacks.bindShortcutsChanged(
3167 updatedShortcuts, removedShortcuts, mUser);
3168 }
3169 }
3170 });
3171 if (!removedShortcuts.isEmpty()) {
3172 deleteItemsFromDatabase(context, removedShortcuts);
3173 }
3174 }
3175 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003176 mHandler.post(new Runnable() {
3177 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003178 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003179 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003181 }
3182 }
3183 });
3184 }
3185 }
3186
Winson Chungdf95eb12013-10-16 14:57:07 -07003187 final ArrayList<String> removedPackageNames =
3188 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003189 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003190 // Mark all packages in the broadcast to be removed
3191 removedPackageNames.addAll(Arrays.asList(packages));
3192 } else if (mOp == OP_UPDATE) {
3193 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003194 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003195 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003196 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003197 }
3198 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003199 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003200
Winson Chungdf95eb12013-10-16 14:57:07 -07003201 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003202 final int removeReason;
3203 if (mOp == OP_UNAVAILABLE) {
3204 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3205 } else {
3206 // Remove all the components associated with this package
3207 for (String pn : removedPackageNames) {
3208 deletePackageFromDatabase(context, pn, mUser);
3209 }
3210 // Remove all the specific components
3211 for (AppInfo a : removedApps) {
3212 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3213 deleteItemsFromDatabase(context, infos);
3214 }
3215 removeReason = 0;
3216 }
3217
Winson Chungdf95eb12013-10-16 14:57:07 -07003218 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003219 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003221 mHandler.post(new Runnable() {
3222 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003224 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003225 callbacks.bindComponentsRemoved(
3226 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003227 }
3228 }
3229 });
Joe Onoratobe386092009-11-17 17:32:16 -08003230 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003231
3232 // onProvidersChanged method (API >= 17) already refreshed the widget list
3233 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3234
Adam Cohen4caf2982013-08-20 18:54:31 -07003235 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003236 mHandler.post(new Runnable() {
3237 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003238 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003239 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003240 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003241 }
3242 }
3243 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 }
3245 }
3246
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003247 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3248 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003249 ArrayList<LauncherAppWidgetProviderInfo> results =
3250 new ArrayList<LauncherAppWidgetProviderInfo>();
3251 try {
3252 synchronized (sBgLock) {
3253 if (sBgWidgetProviders == null || refresh) {
3254 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3255 = new HashMap<>();
3256 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3257 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003258
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003259 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3260 for (AppWidgetProviderInfo pInfo : widgets) {
3261 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3262 UserHandleCompat user = wm.getUser(info);
3263 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3264 }
Robin Lee26ace122015-03-16 19:41:43 +00003265
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003266 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3267 for (CustomAppWidget widget : customWidgets) {
3268 info = new LauncherAppWidgetProviderInfo(context, widget);
3269 UserHandleCompat user = wm.getUser(info);
3270 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3271 }
3272 // Replace the global list at the very end, so that if there is an exception,
3273 // previously loaded provider list is used.
3274 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003275 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003276 results.addAll(sBgWidgetProviders.values());
3277 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003278 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003279 } catch (Exception e) {
3280 if (e.getCause() instanceof TransactionTooLargeException) {
3281 // the returned value may be incomplete and will not be refreshed until the next
3282 // time Launcher starts.
3283 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3284 // onResume is called to refresh the widget provider list.
3285 synchronized (sBgLock) {
3286 if (sBgWidgetProviders != null) {
3287 results.addAll(sBgWidgetProviders.values());
3288 }
3289 return results;
3290 }
3291 } else {
3292 throw e;
3293 }
Adam Cohen59400422014-03-05 18:07:04 -08003294 }
3295 }
3296
Robin Lee26ace122015-03-16 19:41:43 +00003297 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3298 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003299 synchronized (sBgLock) {
3300 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003301 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003302 }
Robin Lee26ace122015-03-16 19:41:43 +00003303 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003304 }
3305 }
3306
Hyunyoung Song227239e2015-05-04 18:17:35 -07003307 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3308 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003309
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003310 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003311 @Override
3312 public void run() {
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003313 updateWidgetsModel(context, refresh);
3314 final WidgetsModel model = mBgWidgetsModel.clone();
3315
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003316 mHandler.post(new Runnable() {
3317 @Override
3318 public void run() {
3319 Callbacks cb = getCallback();
3320 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003321 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003322 }
3323 }
3324 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003325 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003326 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3327 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003328 }
3329 });
3330 }
3331
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003332 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003333 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003334 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003335 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003336 */
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003337 @Thunk void updateWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003338 PackageManager packageManager = context.getPackageManager();
3339 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003340 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003341 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3342 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003343 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003344 }
3345
Adam Cohen091440a2015-03-18 14:16:05 -07003346 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003347 UserHandleCompat user) {
3348 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3349 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003350 }
Adam Cohen556f6132014-01-15 15:18:08 -08003351
Kenny Guyed131872014-04-30 03:02:21 +01003352 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3353 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003354 if (cn == null) {
3355 return false;
3356 }
Kenny Guyed131872014-04-30 03:02:21 +01003357 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3358 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003359 return false;
3360 }
Kenny Guyed131872014-04-30 03:02:21 +01003361 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003362 }
3363
Adam Cohena28b78e2014-05-20 17:03:04 -07003364 public static boolean isValidPackage(Context context, String packageName,
3365 UserHandleCompat user) {
3366 if (packageName == null) {
3367 return false;
3368 }
3369 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3370 return launcherApps.isPackageEnabledForProfile(packageName, user);
3371 }
3372
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003374 * Make an ShortcutInfo object for a restored application or shortcut item that points
3375 * to a package that is not yet installed on the system.
3376 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003377 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
3378 int promiseType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003379 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003380 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003381
3382 Bitmap icon = iconInfo.loadIcon(c, info, context);
3383 // the fallback icon
3384 if (icon == null) {
3385 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3386 } else {
3387 info.setIcon(icon);
3388 }
Sunny Goyal34942622014-08-29 17:20:55 -07003389
3390 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003391 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003392 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003393 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003394 }
Sunny Goyal34942622014-08-29 17:20:55 -07003395 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3396 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003397 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003398 }
Sunny Goyal34942622014-08-29 17:20:55 -07003399 } else {
3400 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3401 }
3402
Winson Chung82b016c2015-05-08 17:00:10 -07003403 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003404 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003405 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003406 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003407 return info;
3408 }
3409
3410 /**
3411 * Make an Intent object for a restored application or shortcut item that points
3412 * to the market page for the item.
3413 */
Adam Cohen091440a2015-03-18 14:16:05 -07003414 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003415 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003416 return getMarketIntent(componentName.getPackageName());
3417 }
3418
3419 static Intent getMarketIntent(String packageName) {
3420 return new Intent(Intent.ACTION_VIEW)
3421 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003422 .scheme("market")
3423 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003424 .appendQueryParameter("id", packageName)
3425 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003426 }
3427
3428 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003429 * Make an ShortcutInfo object for a shortcut that is an application.
3430 *
3431 * If c is not null, then it will be used to fill in missing data like the title and icon.
3432 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003433 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003434 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003435 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003436 if (user == null) {
3437 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003438 return null;
3439 }
3440
Kenny Guyed131872014-04-30 03:02:21 +01003441 ComponentName componentName = intent.getComponent();
3442 if (componentName == null) {
3443 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3444 return null;
3445 }
3446
3447 Intent newIntent = new Intent(intent.getAction(), null);
3448 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3449 newIntent.setComponent(componentName);
3450 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003451 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003452 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3453 return null;
3454 }
3455
3456 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003457 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003458 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3459 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3460 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003461 }
3462
Joe Onorato56d82912010-03-07 14:32:10 -05003463 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003464 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003465 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003466 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003467
Joe Onorato56d82912010-03-07 14:32:10 -05003468 // fall back to the class name of the activity
3469 if (info.title == null) {
3470 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003471 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003472
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003474 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003475 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003476 if (lai != null) {
3477 info.flags = AppInfo.initFlags(lai);
3478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003480 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003481
Sunny Goyale2df0622015-04-24 11:27:00 -07003482 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003483 ItemInfoFilter f) {
3484 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3485 for (ItemInfo i : infos) {
3486 if (i instanceof ShortcutInfo) {
3487 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003488 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003489 if (cn != null && f.filterItem(null, info, cn)) {
3490 filtered.add(info);
3491 }
3492 } else if (i instanceof FolderInfo) {
3493 FolderInfo info = (FolderInfo) i;
3494 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003495 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003496 if (cn != null && f.filterItem(info, s, cn)) {
3497 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003498 }
3499 }
Winson Chung64359a52013-07-08 17:17:08 -07003500 } else if (i instanceof LauncherAppWidgetInfo) {
3501 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3502 ComponentName cn = info.providerName;
3503 if (cn != null && f.filterItem(null, info, cn)) {
3504 filtered.add(info);
3505 }
Winson Chung8a435102012-08-30 17:16:53 -07003506 }
3507 }
Winson Chung64359a52013-07-08 17:17:08 -07003508 return new ArrayList<ItemInfo>(filtered);
3509 }
3510
Adam Cohen091440a2015-03-18 14:16:05 -07003511 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003512 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003513 ItemInfoFilter filter = new ItemInfoFilter() {
3514 @Override
3515 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003516 if (info.user == null) {
3517 return cn.equals(cname);
3518 } else {
3519 return cn.equals(cname) && info.user.equals(user);
3520 }
Winson Chung64359a52013-07-08 17:17:08 -07003521 }
3522 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003523 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003524 }
3525
Sunny Goyal1a745e82014-10-02 15:58:31 -07003526 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003527 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 */
Adam Cohen091440a2015-03-18 14:16:05 -07003529 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003530 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003531 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003532 // Non-app shortcuts are only supported for current user.
3533 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003535
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003536 // TODO: If there's an explicit component and we can't install that, delete it.
3537
Winson Chung82b016c2015-05-08 17:00:10 -07003538 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003539
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003540 Bitmap icon = iconInfo.loadIcon(c, info, context);
3541 // the fallback icon
3542 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003543 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003544 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003545 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003546 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 return info;
3548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003549
Sunny Goyal2350bc92014-10-14 16:42:54 -07003550 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003551 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3552 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3553 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3554
Adam Cohend9198822011-11-22 16:42:47 -08003555 if (intent == null) {
3556 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3557 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3558 return null;
3559 }
3560
Joe Onorato0589f0f2010-02-08 13:44:00 -08003561 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003562 boolean customIcon = false;
3563 ShortcutIconResource iconResource = null;
3564
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003565 if (bitmap instanceof Bitmap) {
3566 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003567 customIcon = true;
3568 } else {
3569 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003570 if (extra instanceof ShortcutIconResource) {
3571 iconResource = (ShortcutIconResource) extra;
3572 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003573 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003574 }
3575 }
3576
Michael Jurkac9d95c52011-08-29 14:03:34 -07003577 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003578
Kenny Guyed131872014-04-30 03:02:21 +01003579 // Only support intents for current user for now. Intents sent from other
3580 // users wouldn't get here without intent forwarding anyway.
3581 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003582 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003583 icon = mIconCache.getDefaultIcon(info.user);
3584 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003585 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003587
Winson Chung82b016c2015-05-08 17:00:10 -07003588 info.title = Utilities.trim(name);
3589 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003590 info.intent = intent;
3591 info.customIcon = customIcon;
3592 info.iconResource = iconResource;
3593
3594 return info;
3595 }
3596
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003598 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 * or make a new one.
3600 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003601 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 // See if a placeholder was created for us already
3603 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003604 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003606 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 folders.put(id, folderInfo);
3608 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003609 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003610 }
3611
Joe Onoratobe386092009-11-17 17:32:16 -08003612
Sunny Goyal651077b2014-06-30 14:15:31 -07003613 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3614 return (provider != null) && (provider.provider != null)
3615 && (provider.provider.getPackageName() != null);
3616 }
3617
Joe Onoratobe386092009-11-17 17:32:16 -08003618 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003619 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003620 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3621 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3622 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3623 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003624 if (mLoaderTask != null) {
3625 mLoaderTask.dumpState();
3626 } else {
3627 Log.d(TAG, "mLoaderTask=null");
3628 }
Joe Onoratobe386092009-11-17 17:32:16 -08003629 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003630
3631 public Callbacks getCallback() {
3632 return mCallbacks != null ? mCallbacks.get() : null;
3633 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003634
3635 /**
3636 * @return {@link FolderInfo} if its already loaded.
3637 */
3638 public FolderInfo findFolderById(Long folderId) {
3639 synchronized (sBgLock) {
3640 return sBgFolders.get(folderId);
3641 }
3642 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003643
3644 /**
3645 * @return the looper for the worker thread which can be used to start background tasks.
3646 */
3647 public static Looper getWorkerLooper() {
3648 return sWorkerThread.getLooper();
3649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650}