blob: 34edf7759fced964d8dbd204e2a6ce6ef865af87 [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;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Sunny Goyal3dc7bee2015-09-15 11:32:58 -070037import android.os.DeadObjectException;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale5bb7052015-07-27 14:36:07 -070059import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
143 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700145 // The lock that must be acquired before referencing any static bg data structures. Unlike
146 // other locks, this one can generally be held long-term because we never expect any of these
147 // static data structures to be referenced outside of the worker thread except on the first
148 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700149 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700153 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
156 // created by LauncherModel that are directly on the home screen (however, no widgets or
157 // shortcuts within folders).
158 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
161 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 new ArrayList<LauncherAppWidgetInfo>();
163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700165 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700166
Adam Cohendcd297f2013-06-18 13:13:40 -0700167 // sBgWorkspaceScreens is the ordered set of workspace screens.
168 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
169
Adam Cohen59400422014-03-05 18:07:04 -0800170 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000171 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
187 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700188 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
189 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700190 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700192 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800193 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700196 public void bindAppsAdded(ArrayList<Long> newScreens,
197 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700198 ArrayList<ItemInfo> addAnimated,
199 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
202 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
203 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700204 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700205 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700206 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700207 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700208 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700209 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700210 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700211 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chung64359a52013-07-08 17:17:08 -0700214 public interface ItemInfoFilter {
215 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
216 }
217
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800219 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400220
Winson Chungee055712013-07-30 14:46:24 -0700221 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700222 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400223 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
224 // resource string.
225 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
226 ProviderInfo providerInfo =
227 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
228 ProviderInfo redirectProvider =
229 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700230
231 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400232 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 if (mOldContentProviderExists) {
235 Log.d(TAG, "Old launcher provider exists.");
236 } else {
237 Log.d(TAG, "Old launcher provider does not exist.");
238 }
239
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100241 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700242 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 mIconCache = iconCache;
244
Kenny Guyed131872014-04-30 03:02:21 +0100245 mLauncherApps = LauncherAppsCompat.getInstance(context);
246 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 }
248
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700249 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
250 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 if (sWorkerThread.getThreadId() == Process.myTid()) {
253 // If we are on the worker thread, post onto the main handler
254 mHandler.post(r);
255 } else {
256 r.run();
257 }
258 }
259
260 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
261 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700262 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700263 if (sWorkerThread.getThreadId() == Process.myTid()) {
264 r.run();
265 } else {
266 // If we are not on the worker thread, then post to the worker handler
267 sWorker.post(r);
268 }
269 }
270
Winson Chunge43a1e72014-01-15 10:33:02 -0800271 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
272 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800273 }
274
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 public void setPackageState(final PackageInstallInfo installInfo) {
276 Runnable updateRunnable = new Runnable() {
277
278 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500279 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700280 synchronized (sBgLock) {
281 final HashSet<ItemInfo> updates = new HashSet<>();
282
283 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
284 // Ignore install success events as they are handled by Package add events.
285 return;
286 }
287
Sunny Goyale2df0622015-04-24 11:27:00 -0700288 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700289 if (info instanceof ShortcutInfo) {
290 ShortcutInfo si = (ShortcutInfo) info;
291 ComponentName cn = si.getTargetComponent();
292 if (si.isPromise() && (cn != null)
293 && installInfo.packageName.equals(cn.getPackageName())) {
294 si.setInstallProgress(installInfo.progress);
295
296 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
297 // Mark this info as broken.
298 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
299 }
300 updates.add(si);
301 }
302 }
303 }
304
305 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
306 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
307 widget.installProgress = installInfo.progress;
308 updates.add(widget);
309 }
310 }
311
312 if (!updates.isEmpty()) {
313 // Push changes to the callback.
314 Runnable r = new Runnable() {
315 public void run() {
316 Callbacks callbacks = getCallback();
317 if (callbacks != null) {
318 callbacks.bindRestoreItemsChange(updates);
319 }
320 }
321 };
322 mHandler.post(r);
323 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500324 }
325 }
326 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500328 }
329
Sunny Goyal756adbc2015-04-16 15:20:51 -0700330 /**
331 * Updates the icons and label of all pending icons for the provided package name.
332 */
333 public void updateSessionDisplayInfo(final String packageName) {
334 Runnable updateRunnable = new Runnable() {
335
336 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700337 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 synchronized (sBgLock) {
339 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
340 final UserHandleCompat user = UserHandleCompat.myUserHandle();
341
Sunny Goyale2df0622015-04-24 11:27:00 -0700342 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 if (info instanceof ShortcutInfo) {
344 ShortcutInfo si = (ShortcutInfo) info;
345 ComponentName cn = si.getTargetComponent();
346 if (si.isPromise() && (cn != null)
347 && packageName.equals(cn.getPackageName())) {
348 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
349 // For auto install apps update the icon as well as label.
350 mIconCache.getTitleAndIcon(si,
351 si.promisedIntent, user,
352 si.shouldUseLowResIcon());
353 } else {
354 // Only update the icon for restored apps.
355 si.updateIcon(mIconCache);
356 }
357 updates.add(si);
358 }
359 }
360 }
361
362 if (!updates.isEmpty()) {
363 // Push changes to the callback.
364 Runnable r = new Runnable() {
365 public void run() {
366 Callbacks callbacks = getCallback();
367 if (callbacks != null) {
368 callbacks.bindShortcutsChanged(updates,
369 new ArrayList<ShortcutInfo>(), user);
370 }
371 }
372 };
373 mHandler.post(r);
374 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700375 }
376 }
377 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700378 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700379 }
380
Adam Cohen76a47a12014-02-05 11:47:43 -0800381 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800382 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800383
384 if (allAppsApps == null) {
385 throw new RuntimeException("allAppsApps must not be null");
386 }
387 if (allAppsApps.isEmpty()) {
388 return;
389 }
390
391 // Process the newly added applications and add them to the database first
392 Runnable r = new Runnable() {
393 public void run() {
394 runOnMainThread(new Runnable() {
395 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800396 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800397 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500398 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800399 }
400 }
401 });
402 }
403 };
404 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700405 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800406
Sunny Goyala9116722015-04-29 13:55:58 -0700407 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800408 int[] xy, int spanX, int spanY) {
409 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700410 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
411 final int xCount = (int) profile.numColumns;
412 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800413 boolean[][] occupied = new boolean[xCount][yCount];
414 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700415 for (ItemInfo r : occupiedPos) {
416 int right = r.cellX + r.spanX;
417 int bottom = r.cellY + r.spanY;
418 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
419 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 occupied[x][y] = true;
421 }
422 }
423 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800424 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700425 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 }
427
428 /**
429 * Find a position on the screen for the given size or adds a new screen.
430 * @return screenId and the coordinates for the item.
431 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700432 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 ArrayList<Long> workspaceScreens,
435 ArrayList<Long> addedWorkspaceScreensFinal,
436 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700437 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438
Sunny Goyala9116722015-04-29 13:55:58 -0700439 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700440 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700441 synchronized (sBgLock) {
442 for (ItemInfo info : sBgItemsIdMap) {
443 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
444 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
445 if (items == null) {
446 items = new ArrayList<>();
447 screenItems.put(info.screenId, items);
448 }
449 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
453
454 // Find appropriate space for the item.
455 long screenId = 0;
456 int[] cordinates = new int[2];
457 boolean found = false;
458
459 int screenCount = workspaceScreens.size();
460 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700461 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 if (preferredScreenIndex < screenCount) {
463 screenId = workspaceScreens.get(preferredScreenIndex);
464 found = findNextAvailableIconSpaceInScreen(
465 screenItems.get(screenId), cordinates, spanX, spanY);
466 }
467
468 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700469 // Search on any of the screens starting from the first screen.
470 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800471 screenId = workspaceScreens.get(screen);
472 if (findNextAvailableIconSpaceInScreen(
473 screenItems.get(screenId), cordinates, spanX, spanY)) {
474 // We found a space for it
475 found = true;
476 break;
477 }
478 }
479 }
480
481 if (!found) {
482 // Still no position found. Add a new screen to the end.
483 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
484
485 // Save the screen id for binding in the workspace
486 workspaceScreens.add(screenId);
487 addedWorkspaceScreensFinal.add(screenId);
488
489 // If we still can't find an empty space, then God help us all!!!
490 if (!findNextAvailableIconSpaceInScreen(
491 screenItems.get(screenId), cordinates, spanX, spanY)) {
492 throw new RuntimeException("Can't find space to add the item");
493 }
494 }
495 return Pair.create(screenId, cordinates);
496 }
497
498 /**
499 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700501 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700502 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800504 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700505 return;
Winson Chung997a9232013-07-24 15:33:46 -0700506 }
Winson Chung64359a52013-07-08 17:17:08 -0700507 // Process the newly added applications and add them to the database first
508 Runnable r = new Runnable() {
509 public void run() {
510 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
511 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
512
Winson Chung76828c82013-08-19 15:43:29 -0700513 // Get the list of workspace screens. We need to append to this list and
514 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
515 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800516 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700517 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700519 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700521 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700523 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 }
Winson Chung76828c82013-08-19 15:43:29 -0700525
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700527 Pair<Long, int[]> coords = findSpaceForItem(context,
528 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 1, 1);
530 long screenId = coords.first;
531 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700532
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700533 ItemInfo itemInfo;
534 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
535 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700537 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700538 } else {
539 throw new RuntimeException("Unexpected info type");
540 }
Winson Chung94d67682013-09-25 16:29:40 -0700541
Winson Chung64359a52013-07-08 17:17:08 -0700542 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700543 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700544 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700545 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700546 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700547 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700548 }
549 }
550
Winson Chung76828c82013-08-19 15:43:29 -0700551 // Update the workspace screens
552 updateWorkspaceScreenOrder(context, workspaceScreens);
553
Adam Cohen76a47a12014-02-05 11:47:43 -0800554 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700555 runOnMainThread(new Runnable() {
556 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800557 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700558 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700559 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
560 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700561 if (!addedShortcutsFinal.isEmpty()) {
562 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
563 long lastScreenId = info.screenId;
564 for (ItemInfo i : addedShortcutsFinal) {
565 if (i.screenId == lastScreenId) {
566 addAnimated.add(i);
567 } else {
568 addNotAnimated.add(i);
569 }
Winson Chung997a9232013-07-24 15:33:46 -0700570 }
571 }
Winson Chungd64d1762013-08-20 14:37:16 -0700572 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800573 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700574 }
Winson Chung64359a52013-07-08 17:17:08 -0700575 }
Winson Chung997a9232013-07-24 15:33:46 -0700576 });
577 }
Winson Chung64359a52013-07-08 17:17:08 -0700578 }
579 };
580 runOnWorkerThread(r);
581 }
582
Sunny Goyald33860f2015-04-23 16:02:20 -0700583 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700584 if (sWorkerThread.getThreadId() == Process.myTid()) {
585 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
586 "main thread");
587 }
588
589 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400590 synchronized (mDeferredBindRunnables) {
591 mDeferredBindRunnables.clear();
592 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700593
594 // Remove any queued UI runnables
595 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700596 // Unbind all the workspace items
597 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700598 }
599
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700601 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700602 // Ensure that we don't use the same workspace items data structure on the main thread
603 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700604 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700605 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700606 tmpItems.addAll(sBgWorkspaceItems);
607 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700608 }
609 Runnable r = new Runnable() {
610 @Override
611 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700612 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700613 item.unbind();
614 }
615 }
616 };
617 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700618 }
619
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620 /**
621 * Adds an item to the DB if it was not created previously, or move it to a new
622 * <container, screen, cellX, cellY>
623 */
624 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 if (item.container == ItemInfo.NO_ID) {
627 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700628 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 } else {
630 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
633 }
634
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700635 static void checkItemInfoLocked(
636 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
637 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
638 if (modelItem != null && item != modelItem) {
639 // check all the data is consistent
640 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
641 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
642 ShortcutInfo shortcut = (ShortcutInfo) item;
643 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
644 modelShortcut.intent.filterEquals(shortcut.intent) &&
645 modelShortcut.id == shortcut.id &&
646 modelShortcut.itemType == shortcut.itemType &&
647 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700649 modelShortcut.cellX == shortcut.cellX &&
650 modelShortcut.cellY == shortcut.cellY &&
651 modelShortcut.spanX == shortcut.spanX &&
652 modelShortcut.spanY == shortcut.spanY &&
653 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
654 (modelShortcut.dropPos != null &&
655 shortcut.dropPos != null &&
656 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
657 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
658 // For all intents and purposes, this is the same object
659 return;
660 }
661 }
662
663 // the modelItem needs to match up perfectly with item if our model is
664 // to be consistent with the database-- for now, just require
665 // modelItem == item or the equality check above
666 String msg = "item: " + ((item != null) ? item.toString() : "null") +
667 "modelItem: " +
668 ((modelItem != null) ? modelItem.toString() : "null") +
669 "Error: ItemInfo passed to checkItemInfo doesn't match original";
670 RuntimeException e = new RuntimeException(msg);
671 if (stackTrace != null) {
672 e.setStackTrace(stackTrace);
673 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800674 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700675 }
676 }
677
Michael Jurka816474f2012-06-25 14:49:02 -0700678 static void checkItemInfo(final ItemInfo item) {
679 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
680 final long itemId = item.id;
681 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700682 public void run() {
683 synchronized (sBgLock) {
684 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700685 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700686 }
687 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700688 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700689 }
690
Michael Jurkac9d95c52011-08-29 14:03:34 -0700691 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
692 final ItemInfo item, final String callingFunction) {
693 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700694 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700695 final ContentResolver cr = context.getContentResolver();
696
Adam Cohen487f7dd2012-06-28 18:12:10 -0700697 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698 Runnable r = new Runnable() {
699 public void run() {
700 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 updateItemArrays(item, itemId, stackTrace);
702 }
703 };
704 runOnWorkerThread(r);
705 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
708 final ArrayList<ItemInfo> items, final String callingFunction) {
709 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700710
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700711 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
712 Runnable r = new Runnable() {
713 public void run() {
714 ArrayList<ContentProviderOperation> ops =
715 new ArrayList<ContentProviderOperation>();
716 int count = items.size();
717 for (int i = 0; i < count; i++) {
718 ItemInfo item = items.get(i);
719 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700720 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700721 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700722
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
724 updateItemArrays(item, itemId, stackTrace);
725
726 }
727 try {
728 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
729 } catch (Exception e) {
730 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700731 }
732 }
733 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700734 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700735 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700736
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
738 // Lock on mBgLock *after* the db operation
739 synchronized (sBgLock) {
740 checkItemInfoLocked(itemId, item, stackTrace);
741
742 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
743 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
744 // Item is in a folder, make sure this folder exists
745 if (!sBgFolders.containsKey(item.container)) {
746 // An items container is being set to a that of an item which is not in
747 // the list of Folders.
748 String msg = "item: " + item + " container being set to: " +
749 item.container + ", not in the list of folders";
750 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 }
752 }
753
754 // Items are added/removed from the corresponding FolderInfo elsewhere, such
755 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
756 // that are on the desktop, as appropriate
757 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800758 if (modelItem != null &&
759 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
760 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700761 switch (modelItem.itemType) {
762 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
763 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
764 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
765 if (!sBgWorkspaceItems.contains(modelItem)) {
766 sBgWorkspaceItems.add(modelItem);
767 }
768 break;
769 default:
770 break;
771 }
772 } else {
773 sBgWorkspaceItems.remove(modelItem);
774 }
775 }
776 }
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700780 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700781 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 item.cellX = cellX;
785 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700786
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 // We store hotseat items in canonical form which is this orientation invariant position
788 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700791 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700792 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
796 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
799 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800800 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802
Michael Jurkac9d95c52011-08-29 14:03:34 -0700803 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700804 }
805
806 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700807 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
808 * cellX, cellY have already been updated on the ItemInfos.
809 */
810 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
811 final long container, final int screen) {
812
813 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
814 int count = items.size();
815
816 for (int i = 0; i < count; i++) {
817 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 item.container = container;
819
820 // We store hotseat items in canonical form which is this orientation invariant position
821 // in the hotseat
822 if (context instanceof Launcher && screen < 0 &&
823 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 item.cellY);
826 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 }
829
830 final ContentValues values = new ContentValues();
831 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
832 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
833 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800834 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836
837 contentValues.add(values);
838 }
839 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
840 }
841
842 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700843 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800844 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700847 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800848 item.cellX = cellX;
849 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700850 item.spanX = spanX;
851 item.spanY = spanY;
852
853 // We store hotseat items in canonical form which is this orientation invariant position
854 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700859 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 }
Adam Cohend4844c32011-02-18 19:25:06 -0800861
Adam Cohend4844c32011-02-18 19:25:06 -0800862 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800863 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
865 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800866 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
868 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800870
Michael Jurka816474f2012-06-25 14:49:02 -0700871 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873
874 /**
875 * Update an item to the database in a specified container.
876 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700877 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700878 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100879 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700880 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800881 }
882
Sunny Goyal756a28a2015-04-23 17:07:55 -0700883 private void assertWorkspaceLoaded() {
Sunny Goyal639e9062015-08-19 19:17:06 -0700884 if (LauncherAppState.isDogfoodBuild()) {
885 synchronized (mLock) {
886 if (!mHasLoaderCompletedOnce ||
887 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
888 throw new RuntimeException("Trying to add shortcut while loader is running");
889 }
890 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700891 }
892 }
893
Adam Cohend4844c32011-02-18 19:25:06 -0800894 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700895 * Returns true if the shortcuts already exists on the workspace. This must be called after
896 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700898 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
899 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700900 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700901 if (intent.getComponent() != null) {
902 // If component is not null, an intent with null package will produce
903 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700904 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700905 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700906 intentWithPkg = intent.toUri(0);
907 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
910 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 }
912 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700913 intentWithPkg = intent.toUri(0);
914 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700915 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916
917 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700918 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700919 if (item instanceof ShortcutInfo) {
920 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700921 Intent targetIntent = info.promisedIntent == null
922 ? info.intent : info.promisedIntent;
923 if (targetIntent != null && info.user.equals(user)) {
924 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700925 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
926 return true;
927 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700928 }
929 }
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700932 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700933 }
934
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935 /**
936 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
937 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700938 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 final ContentResolver cr = context.getContentResolver();
940 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
941 "_id=? and (itemType=? or itemType=?)",
942 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700943 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700944
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 try {
946 if (c.moveToFirst()) {
947 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
948 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
949 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
950 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
951 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
952 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700953 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 FolderInfo folderInfo = null;
956 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700957 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
958 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700960 }
961
Sunny Goyala508e4f2015-05-21 09:33:57 -0700962 // Do not trim the folder label, as is was set by the user.
963 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 folderInfo.id = id;
965 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700966 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700967 folderInfo.cellX = c.getInt(cellXIndex);
968 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700969 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970
971 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 } finally {
974 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700975 }
976
977 return null;
978 }
979
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 /**
981 * Add an item to the database in a specified container. Sets the container, screen, cellX and
982 * cellY fields of the item. Also assigns an ID to the item.
983 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700984 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700985 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 item.cellX = cellX;
988 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 // We store hotseat items in canonical form which is this orientation invariant position
990 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700995 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
998 final ContentValues values = new ContentValues();
999 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001000 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
Michael Jurka414300a2013-08-27 15:42:35 +02001002 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001003 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001004
Jason Monk8e19cf22014-03-20 15:06:57 -04001005 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001006 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001007 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001008 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001011 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001012 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 sBgItemsIdMap.put(item.id, item);
1014 switch (item.itemType) {
1015 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1016 sBgFolders.put(item.id, (FolderInfo) item);
1017 // Fall through
1018 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1019 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1020 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1021 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1022 sBgWorkspaceItems.add(item);
1023 } else {
1024 if (!sBgFolders.containsKey(item.container)) {
1025 // Adding an item to a folder that doesn't exist.
1026 String msg = "adding item: " + item + " to a folder that " +
1027 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001028 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001029 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001030 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001031 break;
1032 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1033 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1034 break;
1035 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001036 }
1037 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001038 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001040 }
1041
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001043 * Creates a new unique child id, for a given cell span across all layouts.
1044 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001045 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001046 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001047 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001048 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001049 }
1050
Sunny Goyal34942622014-08-29 17:20:55 -07001051 private static ArrayList<ItemInfo> getItemsByPackageName(
1052 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001053 ItemInfoFilter filter = new ItemInfoFilter() {
1054 @Override
1055 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1056 return cn.getPackageName().equals(pn) && info.user.equals(user);
1057 }
1058 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001059 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001060 }
1061
1062 /**
1063 * Removes all the items from the database corresponding to the specified package.
1064 */
1065 static void deletePackageFromDatabase(Context context, final String pn,
1066 final UserHandleCompat user) {
1067 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001068 }
1069
1070 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001071 * Removes the specified item from the database
1072 * @param context
1073 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001075 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001076 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1077 items.add(item);
1078 deleteItemsFromDatabase(context, items);
1079 }
1080
1081 /**
1082 * Removes the specified items from the database
1083 * @param context
1084 * @param item
1085 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001086 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001088 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001089 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001090 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001091 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001092 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001093
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001094 // Lock on mBgLock *after* the db operation
1095 synchronized (sBgLock) {
1096 switch (item.itemType) {
1097 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1098 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001099 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 if (info.container == item.id) {
1101 // We are deleting a folder which still contains items that
1102 // think they are contained by that folder.
1103 String msg = "deleting a folder (" + item + ") which still " +
1104 "contains items (" + info + ")";
1105 Log.e(TAG, msg);
1106 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001108 sBgWorkspaceItems.remove(item);
1109 break;
1110 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1111 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1112 sBgWorkspaceItems.remove(item);
1113 break;
1114 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1115 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1116 break;
1117 }
1118 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001120 }
1121 }
Michael Jurka83df1882011-08-31 20:59:26 -07001122 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 }
1125
1126 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 * Update the order of the workspace screens in the database. The array list contains
1128 * a list of screen ids in the order that they should appear.
1129 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001130 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001131 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 final ContentResolver cr = context.getContentResolver();
1133 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1134
1135 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001136 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001137 while (iter.hasNext()) {
1138 long id = iter.next();
1139 if (id < 0) {
1140 iter.remove();
1141 }
1142 }
1143
1144 Runnable r = new Runnable() {
1145 @Override
1146 public void run() {
Yura085c8532014-02-11 15:15:29 +00001147 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001149 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001150 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 for (int i = 0; i < count; i++) {
1152 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001153 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1155 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001156 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 }
Yura085c8532014-02-11 15:15:29 +00001158
1159 try {
1160 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1161 } catch (Exception ex) {
1162 throw new RuntimeException(ex);
1163 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001164
Winson Chungba9c37f2013-08-30 14:11:37 -07001165 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001166 sBgWorkspaceScreens.clear();
1167 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001168 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001169 }
1170 };
1171 runOnWorkerThread(r);
1172 }
1173
1174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 * Remove the contents of the specified folder from the database
1176 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001177 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 final ContentResolver cr = context.getContentResolver();
1179
Michael Jurkac9d95c52011-08-29 14:03:34 -07001180 Runnable r = new Runnable() {
1181 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001182 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001183 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001184 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 sBgItemsIdMap.remove(info.id);
1186 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 sBgWorkspaceItems.remove(info);
1188 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001189
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001190 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001191 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001193 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 for (ItemInfo childInfo : info.contents) {
1195 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001197 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001198 }
1199 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202
1203 /**
1204 * Set this as the current Launcher activity object for the loader.
1205 */
1206 public void initialize(Callbacks callbacks) {
1207 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001208 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1209 // workspace to prevent leaking Launcher activities on orientation change.
1210 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 mCallbacks = new WeakReference<Callbacks>(callbacks);
1212 }
1213 }
1214
Kenny Guyed131872014-04-30 03:02:21 +01001215 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001216 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001217 int op = PackageUpdatedTask.OP_UPDATE;
1218 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1219 user));
1220 }
1221
1222 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001223 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001224 int op = PackageUpdatedTask.OP_REMOVE;
1225 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1226 user));
1227 }
1228
1229 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001230 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001231 int op = PackageUpdatedTask.OP_ADD;
1232 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1233 user));
1234 }
1235
1236 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001237 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001238 boolean replacing) {
1239 if (!replacing) {
1240 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1241 user));
1242 if (mAppsCanBeOnRemoveableStorage) {
1243 // Only rebind if we support removable storage. It catches the
1244 // case where
1245 // apps on the external sd card need to be reloaded
1246 startLoaderFromBackground();
1247 }
1248 } else {
1249 // If we are replacing then just update the packages in the list
1250 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1251 packageNames, user));
1252 }
1253 }
1254
1255 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001256 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001257 boolean replacing) {
1258 if (!replacing) {
1259 enqueuePackageUpdated(new PackageUpdatedTask(
1260 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1261 user));
1262 }
Kenny Guyed131872014-04-30 03:02:21 +01001263 }
1264
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001265 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1267 * ACTION_PACKAGE_CHANGED.
1268 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001269 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001270 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001271 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001272
Joe Onorato36115782010-06-17 13:28:48 -04001273 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001274 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001275 // If we have changed locale we need to clear out the labels in all apps/workspace.
1276 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001277 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001278 Callbacks callbacks = getCallback();
1279 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001280 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001281 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001282 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1283 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001284 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001285 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001286 }
1287 }
1288
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001289 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001290 resetLoadedState(true, true);
1291
Reena Lee93f824a2011-09-23 17:20:28 -07001292 // Do this here because if the launcher activity is running it will be restarted.
1293 // If it's not running startLoaderFromBackground will merely tell it that it needs
1294 // to reload.
1295 startLoaderFromBackground();
1296 }
1297
Winson Chungf0c6ae02012-03-21 16:10:31 -07001298 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1299 synchronized (mLock) {
1300 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1301 // mWorkspaceLoaded to true later
1302 stopLoaderLocked();
1303 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1304 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1305 }
1306 }
1307
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001308 /**
1309 * When the launcher is in the background, it's possible for it to miss paired
1310 * configuration changes. So whenever we trigger the loader from the background
1311 * tell the launcher that it needs to re-run the loader when it comes back instead
1312 * of doing it now.
1313 */
1314 public void startLoaderFromBackground() {
1315 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001316 Callbacks callbacks = getCallback();
1317 if (callbacks != null) {
1318 // Only actually run the loader if they're not paused.
1319 if (!callbacks.setLoadOnResume()) {
1320 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001321 }
1322 }
1323 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001324 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001325 }
Joe Onorato36115782010-06-17 13:28:48 -04001326 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001327
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001328 /**
1329 * If there is already a loader task running, tell it to stop.
1330 */
1331 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001332 LoaderTask oldTask = mLoaderTask;
1333 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001334 oldTask.stopLocked();
1335 }
Reena Lee93f824a2011-09-23 17:20:28 -07001336 }
1337
Adam Cohen1a85c582014-09-30 09:48:49 -07001338 public boolean isCurrentCallbacks(Callbacks callbacks) {
1339 return (mCallbacks != null && mCallbacks.get() == callbacks);
1340 }
1341
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001342 public void startLoader(int synchronousBindPage) {
1343 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001344 }
1345
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001346 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001347 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1348 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001349 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350 // Clear any deferred bind-runnables from the synchronized load process
1351 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001352 synchronized (mDeferredBindRunnables) {
1353 mDeferredBindRunnables.clear();
1354 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355
Joe Onorato36115782010-06-17 13:28:48 -04001356 // Don't bother to start the thread if we know it's not going to do anything
1357 if (mCallbacks != null && mCallbacks.get() != null) {
1358 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001359 stopLoaderLocked();
1360 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001361 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001362 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1364 } else {
1365 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1366 sWorker.post(mLoaderTask);
1367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
1370 }
1371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372 void bindRemainingSynchronousPages() {
1373 // Post the remaining side pages to be loaded
1374 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001375 Runnable[] deferredBindRunnables = null;
1376 synchronized (mDeferredBindRunnables) {
1377 deferredBindRunnables = mDeferredBindRunnables.toArray(
1378 new Runnable[mDeferredBindRunnables.size()]);
1379 mDeferredBindRunnables.clear();
1380 }
1381 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001382 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 }
1385 }
1386
Joe Onorato36115782010-06-17 13:28:48 -04001387 public void stopLoader() {
1388 synchronized (mLock) {
1389 if (mLoaderTask != null) {
1390 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 }
1392 }
Joe Onorato36115782010-06-17 13:28:48 -04001393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001395 /**
1396 * Loads the workspace screen ids in an ordered list.
1397 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001398 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001399 final ContentResolver contentResolver = context.getContentResolver();
1400 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001401
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001402 // Get screens ordered by rank.
1403 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1404 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1405 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001406 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001407 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001408 while (sc.moveToNext()) {
1409 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001410 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001411 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001412 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1413 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001414 }
1415 }
1416 } finally {
1417 sc.close();
1418 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001419 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001420 }
1421
Michael Jurkac57b7a82011-08-09 22:02:20 -07001422 public boolean isAllAppsLoaded() {
1423 return mAllAppsLoaded;
1424 }
1425
Joe Onorato36115782010-06-17 13:28:48 -04001426 /**
1427 * Runnable for the thread that loads the contents of the launcher:
1428 * - workspace icons
1429 * - widgets
1430 * - all apps icons
1431 */
1432 private class LoaderTask implements Runnable {
1433 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001434 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001435 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001436 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001437 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001439 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001440 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001441 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
1443
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001444 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001445 mIsLoadingAndBindingWorkspace = true;
1446
Joe Onorato36115782010-06-17 13:28:48 -04001447 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001448 if (DEBUG_LOADERS) {
1449 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001451
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001452 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001453 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001454 synchronized (LoaderTask.this) {
1455 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001456 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001457 }
1458 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 }
1460 }
1461
Joe Onorato36115782010-06-17 13:28:48 -04001462 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001464 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001465
Joe Onorato36115782010-06-17 13:28:48 -04001466 private void waitForIdle() {
1467 // Wait until the either we're stopped or the other threads are done.
1468 // This way we don't start loading all apps until the workspace has settled
1469 // down.
1470 synchronized (LoaderTask.this) {
1471 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001472
Joe Onorato36115782010-06-17 13:28:48 -04001473 mHandler.postIdle(new Runnable() {
1474 public void run() {
1475 synchronized (LoaderTask.this) {
1476 mLoadAndBindStepFinished = true;
1477 if (DEBUG_LOADERS) {
1478 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001479 }
Joe Onorato36115782010-06-17 13:28:48 -04001480 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001481 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001482 }
Joe Onorato36115782010-06-17 13:28:48 -04001483 });
1484
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001485 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001486 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001487 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1488 // wait no longer than 1sec at a time
1489 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001490 } catch (InterruptedException ex) {
1491 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001492 }
1493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494 if (DEBUG_LOADERS) {
1495 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001496 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001497 + "ms for previous step to finish binding");
1498 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001499 }
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001501
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001502 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001503 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001504 // Ensure that we have a valid page index to load synchronously
1505 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1506 "valid page index");
1507 }
1508 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1509 // Ensure that we don't try and bind a specified page when the pages have not been
1510 // loaded already (we should load everything asynchronously in that case)
1511 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1512 }
1513 synchronized (mLock) {
1514 if (mIsLoaderTaskRunning) {
1515 // Ensure that we are never running the background loading at this point since
1516 // we also touch the background collections
1517 throw new RuntimeException("Error! Background loading is already running");
1518 }
1519 }
1520
1521 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1522 // data structures, we can't allow any other thread to touch that data, but because
1523 // this call is synchronous, we can get away with not locking).
1524
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001525 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001526 // operations from the previous activity. We need to ensure that all queued operations
1527 // are executed before any synchronous binding work is done.
1528 mHandler.flush();
1529
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 // Divide the set of loaded items into those that we are binding synchronously, and
1531 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001532 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1534 // arise from that.
1535 onlyBindAllApps();
1536 }
1537
Joe Onorato36115782010-06-17 13:28:48 -04001538 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001539 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001540 if (mStopped) {
1541 return;
1542 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001543 mIsLoaderTaskRunning = true;
1544 }
Joe Onorato36115782010-06-17 13:28:48 -04001545 // Optimize for end-user experience: if the Launcher is up and // running with the
1546 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1547 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001548 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001549 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001550 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001551
Joe Onorato36115782010-06-17 13:28:48 -04001552 if (mStopped) {
1553 break keep_running;
1554 }
1555
Joe Onorato36115782010-06-17 13:28:48 -04001556 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001557
1558 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001559 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1560 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001561 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562
Joe Onorato36115782010-06-17 13:28:48 -04001563 // Clear out this reference, otherwise we end up holding it until all of the
1564 // callback runnables are done.
1565 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001566
Joe Onorato36115782010-06-17 13:28:48 -04001567 synchronized (mLock) {
1568 // If we are still the last one to be scheduled, remove ourselves.
1569 if (mLoaderTask == this) {
1570 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001572 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001573 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575 }
1576
1577 public void stopLocked() {
1578 synchronized (LoaderTask.this) {
1579 mStopped = true;
1580 this.notify();
1581 }
1582 }
1583
1584 /**
1585 * Gets the callbacks object. If we've been stopped, or if the launcher object
1586 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1587 * object that was around when the deferred message was scheduled, and if there's
1588 * a new Callbacks object around then also return null. This will save us from
1589 * calling onto it with data that will be ignored.
1590 */
1591 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1592 synchronized (mLock) {
1593 if (mStopped) {
1594 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001595 }
Joe Onorato36115782010-06-17 13:28:48 -04001596
1597 if (mCallbacks == null) {
1598 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001599 }
Joe Onorato36115782010-06-17 13:28:48 -04001600
1601 final Callbacks callbacks = mCallbacks.get();
1602 if (callbacks != oldCallbacks) {
1603 return null;
1604 }
1605 if (callbacks == null) {
1606 Log.w(TAG, "no mCallbacks");
1607 return null;
1608 }
1609
1610 return callbacks;
1611 }
1612 }
1613
1614 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001615 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1616 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001617 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001618 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001619 final int countX = profile.numColumns;
1620 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001621
Adam Cohendcd297f2013-06-18 13:13:40 -07001622 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001623 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001624 // Return early if we detect that an item is under the hotseat button
1625 if (mCallbacks == null ||
1626 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001627 Log.e(TAG, "Error loading shortcut into hotseat " + item
1628 + " into position (" + item.screenId + ":" + item.cellX + ","
1629 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001630 return false;
1631 }
1632
Dan Sandler295ae182013-12-10 16:05:47 -05001633 final ItemInfo[][] hotseatItems =
1634 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1635
Adam Cohen2e6da152015-05-06 11:42:25 -07001636 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001637 Log.e(TAG, "Error loading shortcut " + item
1638 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001639 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001640 + ")");
1641 return false;
1642 }
1643
Dan Sandler295ae182013-12-10 16:05:47 -05001644 if (hotseatItems != null) {
1645 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001646 Log.e(TAG, "Error loading shortcut into hotseat " + item
1647 + " into position (" + item.screenId + ":" + item.cellX + ","
1648 + item.cellY + ") occupied by "
1649 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1650 [(int) item.screenId][0]);
1651 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001652 } else {
1653 hotseatItems[(int) item.screenId][0] = item;
1654 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001655 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001656 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001657 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001658 items[(int) item.screenId][0] = item;
1659 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001660 return true;
1661 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001662 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1663 if (!workspaceScreens.contains((Long) item.screenId)) {
1664 // The item has an invalid screen id.
1665 return false;
1666 }
1667 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001668 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001669 return true;
1670 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001671
Adam Cohendcd297f2013-06-18 13:13:40 -07001672 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001673 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001674 occupied.put(item.screenId, items);
1675 }
1676
Dan Sandler295ae182013-12-10 16:05:47 -05001677 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001678 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1679 item.cellX < 0 || item.cellY < 0 ||
1680 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1681 Log.e(TAG, "Error loading shortcut " + item
1682 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1683 + item.cellX + "," + item.cellY
1684 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1685 return false;
1686 }
1687
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001688 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001689 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1690 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001692 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001694 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001695 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001697 return false;
1698 }
1699 }
1700 }
1701 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1702 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001704 }
1705 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001706
Joe Onorato36115782010-06-17 13:28:48 -04001707 return true;
1708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709
Winson Chungba9c37f2013-08-30 14:11:37 -07001710 /** Clears all the sBg data structures */
1711 private void clearSBgDataStructures() {
1712 synchronized (sBgLock) {
1713 sBgWorkspaceItems.clear();
1714 sBgAppWidgets.clear();
1715 sBgFolders.clear();
1716 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001717 sBgWorkspaceScreens.clear();
1718 }
1719 }
1720
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001721 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001722 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001723
Joe Onorato36115782010-06-17 13:28:48 -04001724 final Context context = mContext;
1725 final ContentResolver contentResolver = context.getContentResolver();
1726 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001727 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001728 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1729 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001730 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001731
Winson Chung892c74d2013-08-22 16:15:50 -07001732 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001733 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001734 int countX = profile.numColumns;
1735 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001736
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001737 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001738 long migrationStartTime = System.currentTimeMillis();
1739 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001740 try {
1741 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1742 // Clear the flags before starting the task, so that we do not run the task
1743 // again, in case there was an uncaught error.
1744 MigrateFromRestoreTask.clearFlags(mContext);
1745 task.execute();
1746 } catch (Exception e) {
1747 Log.e(TAG, "Error during grid migration", e);
1748
1749 // Clear workspace.
1750 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1751 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001752 Log.v(TAG, "Workspace migration completed in "
1753 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001754 }
1755
Dan Sandlerd5024042014-01-09 15:01:33 -05001756 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1757 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1758 LauncherAppState.getLauncherProvider().deleteDatabase();
1759 }
1760
1761 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1762 // append the user's Launcher2 shortcuts
1763 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1764 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1765 } else {
1766 // Make sure the default workspace is loaded
1767 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001768 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001769 }
Adam Cohene25af792013-06-06 23:08:25 -07001770
Winson Chung2abf94d2012-07-18 18:16:38 -07001771 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001772 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001773 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001774 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001775 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001776
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001778 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001779 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001780 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001781 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001782
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 // +1 for the hotseat (it can be larger than the workspace)
1784 // Load workspace in reverse order to ensure that latest items are loaded first (and
1785 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001786 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 try {
1789 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1790 final int intentIndex = c.getColumnIndexOrThrow
1791 (LauncherSettings.Favorites.INTENT);
1792 final int titleIndex = c.getColumnIndexOrThrow
1793 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 final int containerIndex = c.getColumnIndexOrThrow(
1795 LauncherSettings.Favorites.CONTAINER);
1796 final int itemTypeIndex = c.getColumnIndexOrThrow(
1797 LauncherSettings.Favorites.ITEM_TYPE);
1798 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1799 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001800 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1801 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001802 final int screenIndex = c.getColumnIndexOrThrow(
1803 LauncherSettings.Favorites.SCREEN);
1804 final int cellXIndex = c.getColumnIndexOrThrow
1805 (LauncherSettings.Favorites.CELLX);
1806 final int cellYIndex = c.getColumnIndexOrThrow
1807 (LauncherSettings.Favorites.CELLY);
1808 final int spanXIndex = c.getColumnIndexOrThrow
1809 (LauncherSettings.Favorites.SPANX);
1810 final int spanYIndex = c.getColumnIndexOrThrow(
1811 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001812 final int rankIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001814 final int restoredIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001816 final int profileIdIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001818 final int optionsIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001820 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821
Sunny Goyal7f834d22015-04-21 10:10:23 -07001822 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1823 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1824 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1825 }
1826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 ShortcutInfo info;
1828 String intentDescription;
1829 LauncherAppWidgetInfo appWidgetInfo;
1830 int container;
1831 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001832 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001834 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 while (!mStopped && c.moveToNext()) {
1837 try {
1838 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001839 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001840 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001841 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 switch (itemType) {
1844 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1845 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001846 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001848 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001849 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001850 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001851 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001852 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001853 if (user == null) {
1854 // User has been deleted remove the item.
1855 itemsToRemove.add(id);
1856 continue;
1857 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 try {
1859 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001860 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001861 if (cn != null && cn.getPackageName() != null) {
1862 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1863 cn.getPackageName(), user);
1864 boolean validComponent = validPkg &&
1865 launcherApps.isActivityEnabledForProfile(cn, user);
1866
1867 if (validComponent) {
1868 if (restored) {
1869 // no special handling necessary for this item
1870 restoredRows.add(id);
1871 restored = false;
1872 }
1873 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001874 intent = null;
1875 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1876 // We allow auto install apps to have their intent
1877 // updated after an install.
1878 intent = manager.getLaunchIntentForPackage(
1879 cn.getPackageName());
1880 if (intent != null) {
1881 ContentValues values = new ContentValues();
1882 values.put(LauncherSettings.Favorites.INTENT,
1883 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001884 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001885 }
1886 }
1887
1888 if (intent == null) {
1889 // The app is installed but the component is no
1890 // longer available.
1891 Launcher.addDumpLog(TAG,
1892 "Invalid component removed: " + cn, true);
1893 itemsToRemove.add(id);
1894 continue;
1895 } else {
1896 // no special handling necessary for this item
1897 restoredRows.add(id);
1898 restored = false;
1899 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001900 } else if (restored) {
1901 // Package is not yet available but might be
1902 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001903 Launcher.addDumpLog(TAG,
1904 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001905
1906 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1907 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001908 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001909 // App restore has started. Update the flag
1910 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1911 ContentValues values = new ContentValues();
1912 values.put(LauncherSettings.Favorites.RESTORED,
1913 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001914 updateItem(id, values);
1915 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1916 // This is a common app. Try to replace this.
1917 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1918 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1919 if (parser.findDefaultApp()) {
1920 // Default app found. Replace it.
1921 intent = parser.parsedIntent;
1922 cn = intent.getComponent();
1923 ContentValues values = parser.parsedValues;
1924 values.put(LauncherSettings.Favorites.RESTORED, 0);
1925 updateItem(id, values);
1926 restored = false;
1927 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001928
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001929 } else if (REMOVE_UNRESTORED_ICONS) {
1930 Launcher.addDumpLog(TAG,
1931 "Unrestored package removed: " + cn, true);
1932 itemsToRemove.add(id);
1933 continue;
1934 }
Sunny Goyal94485362014-09-18 16:13:58 -07001935 } else if (REMOVE_UNRESTORED_ICONS) {
1936 Launcher.addDumpLog(TAG,
1937 "Unrestored package removed: " + cn, true);
1938 itemsToRemove.add(id);
1939 continue;
1940 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001941 } else if (launcherApps.isAppEnabled(
1942 manager, cn.getPackageName(),
1943 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1944 // Package is present but not available.
1945 allowMissingTarget = true;
1946 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1947 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001948 // SdCard is not ready yet. Package might get available,
1949 // once it is ready.
1950 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1951 + " (check again later)", true);
1952 HashSet<String> pkgs = sPendingPackages.get(user);
1953 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001954 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001955 sPendingPackages.put(user, pkgs);
1956 }
1957 pkgs.add(cn.getPackageName());
1958 allowMissingTarget = true;
1959 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001960
1961 } else {
1962 // Do not wait for external media load anymore.
1963 // Log the invalid package, and remove it
1964 Launcher.addDumpLog(TAG,
1965 "Invalid package removed: " + cn, true);
1966 itemsToRemove.add(id);
1967 continue;
Winson Chungee055712013-07-30 14:46:24 -07001968 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001969 } else if (cn == null) {
1970 // For shortcuts with no component, keep them as they are
1971 restoredRows.add(id);
1972 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001973 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001975 Launcher.addDumpLog(TAG,
1976 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001977 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 continue;
1979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980
Sunny Goyal34b65272015-03-11 16:56:52 -07001981 boolean useLowResIcon = container >= 0 &&
1982 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1983
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001984 if (itemReplaced) {
1985 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001986 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001987 cursorIconInfo.iconIndex, titleIndex,
1988 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001989 } else {
1990 // Don't replace items for other profiles.
1991 itemsToRemove.add(id);
1992 continue;
1993 }
1994 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001995 if (user.equals(UserHandleCompat.myUserHandle())) {
1996 Launcher.addDumpLog(TAG,
1997 "constructing info for partially restored package",
1998 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001999 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002000 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002001 intent = getRestoredItemIntent(c, context, intent);
2002 } else {
2003 // Don't restore items for other profiles.
2004 itemsToRemove.add(id);
2005 continue;
2006 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002007 } else if (itemType ==
2008 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002009 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 cursorIconInfo.iconIndex, titleIndex,
2011 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002013 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 // App shortcuts that used to be automatically added to Launcher
2016 // didn't always have the correct intent flags set, so do that
2017 // here
2018 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002019 intent.getCategories() != null &&
2020 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002021 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 intent.addFlags(
2023 Intent.FLAG_ACTIVITY_NEW_TASK |
2024 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2025 }
Michael Jurka96879562012-03-22 05:54:33 -07002026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002029 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002032 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 info.cellX = c.getInt(cellXIndex);
2034 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002035 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002036 info.spanX = 1;
2037 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002038 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002039 if (info.promisedIntent != null) {
2040 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2041 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002042 info.isDisabled = disabledState;
2043 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2044 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2045 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002046
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002048 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002049 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 break;
2051 }
2052
Sunny Goyal756adbc2015-04-16 15:20:51 -07002053 if (restored) {
2054 ComponentName cn = info.getTargetComponent();
2055 if (cn != null) {
2056 Integer progress = installingPkgs.get(cn.getPackageName());
2057 if (progress != null) {
2058 info.setInstallProgress(progress);
2059 } else {
2060 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2061 }
2062 }
2063 }
2064
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 switch (container) {
2066 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2067 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2068 sBgWorkspaceItems.add(info);
2069 break;
2070 default:
2071 // Item is in a user folder
2072 FolderInfo folderInfo =
2073 findOrMakeFolder(sBgFolders, container);
2074 folderInfo.add(info);
2075 break;
2076 }
2077 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002078 } else {
2079 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 }
2081 break;
2082
2083 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2084 id = c.getLong(idIndex);
2085 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2086
Sunny Goyala508e4f2015-05-21 09:33:57 -07002087 // Do not trim the folder label, as is was set by the user.
2088 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002091 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 folderInfo.cellX = c.getInt(cellXIndex);
2093 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002094 folderInfo.spanX = 1;
2095 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002096 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097
Daniel Sandler8802e962010-05-26 16:28:16 -04002098 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002099 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002100 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002101 break;
2102 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002103
Joe Onorato9c1289c2009-08-17 11:03:03 -04002104 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2106 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2107 sBgWorkspaceItems.add(folderInfo);
2108 break;
Joe Onorato36115782010-06-17 13:28:48 -04002109 }
Joe Onorato17a89222011-02-08 17:26:11 -08002110
Chris Wrenf4d08112014-01-16 18:13:56 -05002111 if (restored) {
2112 // no special handling required for restored folders
2113 restoredRows.add(id);
2114 }
2115
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2117 sBgFolders.put(folderInfo.id, folderInfo);
2118 break;
2119
2120 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002121 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002123 boolean customWidget = itemType ==
2124 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2125
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002127 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002128 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002129 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002130 user = allUsers.get(serialNumber);
2131 if (user == null) {
2132 itemsToRemove.add(id);
2133 continue;
2134 }
2135
Sunny Goyalff572272014-07-23 13:58:07 -07002136 final ComponentName component =
2137 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002138
Sunny Goyal651077b2014-06-30 14:15:31 -07002139 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002140 final boolean isIdValid = (restoreStatus &
2141 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002142 final boolean wasProviderReady = (restoreStatus &
2143 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002144
Adam Cohen59400422014-03-05 18:07:04 -08002145 final LauncherAppWidgetProviderInfo provider =
2146 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002147 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002148 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002149
2150 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002151 if (!isSafeMode && !customWidget &&
2152 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002153 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002154 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002155
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002157 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 itemsToRemove.add(id);
2159 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002160 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2162 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002163
Sunny Goyal53f96722015-07-13 19:54:53 -07002164 // The provider is available. So the widget is either
2165 // available or not available. We do not need to track
2166 // any future restore updates.
2167 int status = restoreStatus &
2168 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002169 if (!wasProviderReady) {
2170 // If provider was not previously ready, update the
2171 // status and UI flag.
2172
2173 // Id would be valid only if the widget restore broadcast was received.
2174 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002175 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002176 } else {
2177 status &= ~LauncherAppWidgetInfo
2178 .FLAG_PROVIDER_NOT_READY;
2179 }
2180 }
2181 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002182 } else {
2183 Log.v(TAG, "Widget restore pending id=" + id
2184 + " appWidgetId=" + appWidgetId
2185 + " status =" + restoreStatus);
2186 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002187 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002188 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002189 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002190
2191 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2192 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002193 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002194 // App restore has started. Update the flag
2195 appWidgetInfo.restoreStatus |=
2196 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002197 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002198 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002199 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002200 itemsToRemove.add(id);
2201 continue;
2202 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002203
2204 appWidgetInfo.installProgress =
2205 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002206 }
Sunny Goyalff572272014-07-23 13:58:07 -07002207
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002209 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 appWidgetInfo.cellX = c.getInt(cellXIndex);
2211 appWidgetInfo.cellY = c.getInt(cellYIndex);
2212 appWidgetInfo.spanX = c.getInt(spanXIndex);
2213 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002214 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002215
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2217 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2218 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002219 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2220 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 continue;
2222 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002224 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002226 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002227 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 break;
2229 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002230
Adam Cohen59400422014-03-05 18:07:04 -08002231 if (!customWidget) {
2232 String providerName =
2233 appWidgetInfo.providerName.flattenToString();
2234 if (!providerName.equals(savedProvider) ||
2235 (appWidgetInfo.restoreStatus != restoreStatus)) {
2236 ContentValues values = new ContentValues();
2237 values.put(
2238 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2239 providerName);
2240 values.put(LauncherSettings.Favorites.RESTORED,
2241 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002242 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002243 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002244 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2246 sBgAppWidgets.add(appWidgetInfo);
2247 }
Joe Onorato36115782010-06-17 13:28:48 -04002248 break;
2249 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002251 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002252 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 }
2254 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002255 if (c != null) {
2256 c.close();
2257 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 }
2259
Winson Chungba9c37f2013-08-30 14:11:37 -07002260 // Break early if we've stopped loading
2261 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002262 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002263 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002264 }
2265
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002268 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2269 Utilities.createDbSelectionQuery(
2270 LauncherSettings.Favorites._ID, itemsToRemove), null);
2271 if (DEBUG_LOADERS) {
2272 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2273 LauncherSettings.Favorites._ID, itemsToRemove));
2274 }
2275
2276 // Remove any empty folder
2277 for (long folderId : LauncherAppState.getLauncherProvider()
2278 .deleteEmptyFolders()) {
2279 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2280 sBgFolders.remove(folderId);
2281 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002282 }
2283 }
2284
Sunny Goyal317698b2015-07-29 11:45:41 -07002285 // Sort all the folder items and make sure the first 3 items are high resolution.
2286 for (FolderInfo folder : sBgFolders) {
2287 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2288 int pos = 0;
2289 for (ShortcutInfo info : folder.contents) {
2290 if (info.usingLowResIcon) {
2291 info.updateIcon(mIconCache, false);
2292 }
2293 pos ++;
2294 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2295 break;
2296 }
2297 }
2298 }
2299
Chris Wrenf4d08112014-01-16 18:13:56 -05002300 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002301 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002302 ContentValues values = new ContentValues();
2303 values.put(LauncherSettings.Favorites.RESTORED, 0);
2304 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2305 Utilities.createDbSelectionQuery(
2306 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002307 }
2308
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002309 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2310 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002311 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002312 null, sWorker);
2313 }
2314
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002315 // Remove any empty screens
2316 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002317 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002318 long screenId = item.screenId;
2319 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2320 unusedScreens.contains(screenId)) {
2321 unusedScreens.remove(screenId);
2322 }
2323 }
2324
2325 // If there are any empty screens remove them, and update.
2326 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002327 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 }
2330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 if (DEBUG_LOADERS) {
2332 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2333 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002335 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002337
Sunny Goyale2df0622015-04-24 11:27:00 -07002338 for (int i = 0; i < nScreens; i++) {
2339 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002340 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 line += " | ";
2342 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002343 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002344 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002345 if (x < screen.length && y < screen[x].length) {
2346 line += (screen[x][y] != null) ? "#" : ".";
2347 } else {
2348 line += "!";
2349 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 }
Joe Onorato36115782010-06-17 13:28:48 -04002351 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002354 }
Joe Onorato36115782010-06-17 13:28:48 -04002355 }
Adam Cohene25af792013-06-06 23:08:25 -07002356 }
2357
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002358 /**
2359 * Partially updates the item without any notification. Must be called on the worker thread.
2360 */
2361 private void updateItem(long itemId, ContentValues update) {
2362 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002363 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002364 update,
2365 BaseColumns._ID + "= ?",
2366 new String[]{Long.toString(itemId)});
2367 }
2368
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 /** Filters the set of items who are directly or indirectly (via another container) on the
2370 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002371 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 ArrayList<ItemInfo> allWorkspaceItems,
2373 ArrayList<ItemInfo> currentScreenItems,
2374 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002375 // Purge any null ItemInfos
2376 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2377 while (iter.hasNext()) {
2378 ItemInfo i = iter.next();
2379 if (i == null) {
2380 iter.remove();
2381 }
2382 }
2383
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 // Order the set of items by their containers first, this allows use to walk through the
2385 // list sequentially, build up a list of containers that are in the specified screen,
2386 // as well as all items in those containers.
2387 Set<Long> itemsOnScreen = new HashSet<Long>();
2388 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2389 @Override
2390 public int compare(ItemInfo lhs, ItemInfo rhs) {
2391 return (int) (lhs.container - rhs.container);
2392 }
2393 });
2394 for (ItemInfo info : allWorkspaceItems) {
2395 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002396 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397 currentScreenItems.add(info);
2398 itemsOnScreen.add(info.id);
2399 } else {
2400 otherScreenItems.add(info);
2401 }
2402 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2403 currentScreenItems.add(info);
2404 itemsOnScreen.add(info.id);
2405 } else {
2406 if (itemsOnScreen.contains(info.container)) {
2407 currentScreenItems.add(info);
2408 itemsOnScreen.add(info.id);
2409 } else {
2410 otherScreenItems.add(info);
2411 }
2412 }
2413 }
2414 }
2415
2416 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002417 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 ArrayList<LauncherAppWidgetInfo> appWidgets,
2419 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2420 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421
2422 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002423 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002425 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 currentScreenWidgets.add(widget);
2427 } else {
2428 otherScreenWidgets.add(widget);
2429 }
2430 }
2431 }
2432
2433 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002434 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002435 LongArrayMap<ItemInfo> itemsIdMap,
2436 LongArrayMap<FolderInfo> folders,
2437 LongArrayMap<FolderInfo> currentScreenFolders,
2438 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439
Sunny Goyale2df0622015-04-24 11:27:00 -07002440 int total = folders.size();
2441 for (int i = 0; i < total; i++) {
2442 long id = folders.keyAt(i);
2443 FolderInfo folder = folders.valueAt(i);
2444
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002446 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002447 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002448 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 currentScreenFolders.put(id, folder);
2450 } else {
2451 otherScreenFolders.put(id, folder);
2452 }
2453 }
2454 }
2455
2456 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2457 * right) */
2458 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002459 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002460 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461 // XXX: review this
2462 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002463 @Override
2464 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002465 int cellCountX = (int) profile.numColumns;
2466 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002467 int screenOffset = cellCountX * cellCountY;
2468 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002469 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002470 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002472 rhs.cellY * cellCountX + rhs.cellX);
2473 return (int) (lr - rr);
2474 }
2475 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002477
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2479 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002480 final Runnable r = new Runnable() {
2481 @Override
2482 public void run() {
2483 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2484 if (callbacks != null) {
2485 callbacks.bindScreens(orderedScreens);
2486 }
2487 }
2488 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002489 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002490 }
2491
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2493 final ArrayList<ItemInfo> workspaceItems,
2494 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002495 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002497
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 final boolean postOnMainThread = (deferredBindRunnables != null);
2499
2500 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002501 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002503 final int start = i;
2504 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 final Runnable r = new Runnable() {
2506 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002508 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002510 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2511 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 }
2513 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 };
2515 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002516 synchronized (deferredBindRunnables) {
2517 deferredBindRunnables.add(r);
2518 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002520 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 }
Joe Onorato36115782010-06-17 13:28:48 -04002522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523
2524 // Bind the folders
2525 if (!folders.isEmpty()) {
2526 final Runnable r = new Runnable() {
2527 public void run() {
2528 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2529 if (callbacks != null) {
2530 callbacks.bindFolders(folders);
2531 }
2532 }
2533 };
2534 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002535 synchronized (deferredBindRunnables) {
2536 deferredBindRunnables.add(r);
2537 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002539 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 }
2541 }
2542
2543 // Bind the widgets, one at a time
2544 N = appWidgets.size();
2545 for (int i = 0; i < N; i++) {
2546 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2547 final Runnable r = new Runnable() {
2548 public void run() {
2549 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2550 if (callbacks != null) {
2551 callbacks.bindAppWidget(widget);
2552 }
2553 }
2554 };
2555 if (postOnMainThread) {
2556 deferredBindRunnables.add(r);
2557 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002558 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 }
2560 }
2561 }
2562
2563 /**
2564 * Binds all loaded data to actual views on the main thread.
2565 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002566 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 final long t = SystemClock.uptimeMillis();
2568 Runnable r;
2569
2570 // Don't use these two variables in any of the callback runnables.
2571 // Otherwise we hold a reference to them.
2572 final Callbacks oldCallbacks = mCallbacks.get();
2573 if (oldCallbacks == null) {
2574 // This launcher has exited and nobody bothered to tell us. Just bail.
2575 Log.w(TAG, "LoaderTask running with no launcher");
2576 return;
2577 }
2578
Winson Chung9b9fb962013-11-15 15:39:34 -08002579 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002580 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2581 ArrayList<LauncherAppWidgetInfo> appWidgets =
2582 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002583 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002584
2585 final LongArrayMap<FolderInfo> folders;
2586 final LongArrayMap<ItemInfo> itemsIdMap;
2587
Winson Chung2abf94d2012-07-18 18:16:38 -07002588 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002589 workspaceItems.addAll(sBgWorkspaceItems);
2590 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002591 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002592
2593 folders = sBgFolders.clone();
2594 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 }
2596
Derek Prothro7aff3992013-12-10 14:00:37 -05002597 final boolean isLoadingSynchronously =
2598 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002599 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002600 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002601 if (currScreen >= orderedScreenIds.size()) {
2602 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002603 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002604 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002605 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002606 final long currentScreenId = currentScreen < 0
2607 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002608
2609 // Load all the items that are on the current page first (and in the process, unbind
2610 // all the existing workspace items before we call startBinding() below.
2611 unbindWorkspaceItemsOnMainThread();
2612
2613 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002614 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2615 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2616 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2617 new ArrayList<LauncherAppWidgetInfo>();
2618 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2619 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002620 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2621 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622
Winson Chung9b9fb962013-11-15 15:39:34 -08002623 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002624 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002625 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002626 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002627 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 otherFolders);
2629 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2630 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2631
2632 // Tell the workspace that we're about to start binding items
2633 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002634 public void run() {
2635 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2636 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002638 }
2639 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002640 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002641 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002642
Adam Cohendcd297f2013-06-18 13:13:40 -07002643 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2644
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645 // Load items on the current page
2646 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2647 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002648 if (isLoadingSynchronously) {
2649 r = new Runnable() {
2650 public void run() {
2651 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002652 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002653 callbacks.onPageBoundSynchronously(currentScreen);
2654 }
2655 }
2656 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002657 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002658 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002659
Winson Chung4a2afa32012-07-19 14:53:05 -07002660 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2661 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002662 synchronized (mDeferredBindRunnables) {
2663 mDeferredBindRunnables.clear();
2664 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002665 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002666 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002667
2668 // Tell the workspace that we're done binding items
2669 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002670 public void run() {
2671 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2672 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002673 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002674 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002675
Sunny Goyal639e9062015-08-19 19:17:06 -07002676 mIsLoadingAndBindingWorkspace = false;
2677
2678 // Run all the bind complete runnables after workspace is bound.
2679 if (!mBindCompleteRunnables.isEmpty()) {
2680 synchronized (mBindCompleteRunnables) {
2681 for (final Runnable r : mBindCompleteRunnables) {
2682 runOnWorkerThread(r);
2683 }
2684 mBindCompleteRunnables.clear();
2685 }
2686 }
2687
Winson Chung98e030b2012-05-07 16:01:11 -07002688 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002689 if (DEBUG_LOADERS) {
2690 Log.d(TAG, "bound workspace in "
2691 + (SystemClock.uptimeMillis()-t) + "ms");
2692 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002693
Joe Onorato36115782010-06-17 13:28:48 -04002694 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002695 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002696 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002697 synchronized (mDeferredBindRunnables) {
2698 mDeferredBindRunnables.add(r);
2699 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002700 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002701 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002702 }
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002704
Joe Onorato36115782010-06-17 13:28:48 -04002705 private void loadAndBindAllApps() {
2706 if (DEBUG_LOADERS) {
2707 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2708 }
2709 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002710 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002711 synchronized (LoaderTask.this) {
2712 if (mStopped) {
2713 return;
2714 }
2715 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002716 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002717 synchronized (LoaderTask.this) {
2718 if (mStopped) {
2719 return;
2720 }
2721 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002722 }
Joe Onorato36115782010-06-17 13:28:48 -04002723 } else {
2724 onlyBindAllApps();
2725 }
2726 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002727
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002728 private void updateIconCache() {
2729 // Ignore packages which have a promise icon.
2730 HashSet<String> packagesToIgnore = new HashSet<>();
2731 synchronized (sBgLock) {
2732 for (ItemInfo info : sBgItemsIdMap) {
2733 if (info instanceof ShortcutInfo) {
2734 ShortcutInfo si = (ShortcutInfo) info;
2735 if (si.isPromise() && si.getTargetComponent() != null) {
2736 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2737 }
2738 } else if (info instanceof LauncherAppWidgetInfo) {
2739 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2740 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2741 packagesToIgnore.add(lawi.providerName.getPackageName());
2742 }
2743 }
2744 }
2745 }
2746 mIconCache.updateDbIcons(packagesToIgnore);
2747 }
2748
Joe Onorato36115782010-06-17 13:28:48 -04002749 private void onlyBindAllApps() {
2750 final Callbacks oldCallbacks = mCallbacks.get();
2751 if (oldCallbacks == null) {
2752 // This launcher has exited and nobody bothered to tell us. Just bail.
2753 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2754 return;
2755 }
2756
2757 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002758 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002759 final ArrayList<AppInfo> list
2760 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002761 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002762 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002763 public void run() {
2764 final long t = SystemClock.uptimeMillis();
2765 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2766 if (callbacks != null) {
2767 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002768 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002769 }
2770 if (DEBUG_LOADERS) {
2771 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2772 + (SystemClock.uptimeMillis()-t) + "ms");
2773 }
2774 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002775 };
2776 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002777 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002778 r.run();
2779 } else {
2780 mHandler.post(r);
2781 }
Joe Onorato36115782010-06-17 13:28:48 -04002782 }
2783
Winson Chung64359a52013-07-08 17:17:08 -07002784 private void loadAllApps() {
2785 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002786
Joe Onorato36115782010-06-17 13:28:48 -04002787 final Callbacks oldCallbacks = mCallbacks.get();
2788 if (oldCallbacks == null) {
2789 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002790 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002791 return;
2792 }
2793
Kenny Guyed131872014-04-30 03:02:21 +01002794 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2795
Winson Chung64359a52013-07-08 17:17:08 -07002796 // Clear the list of apps
2797 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002798 for (UserHandleCompat user : profiles) {
2799 // Query for the set of apps
2800 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002801 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002802 if (DEBUG_LOADERS) {
2803 Log.d(TAG, "getActivityList took "
2804 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2805 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2806 }
2807 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002808 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002809 if (apps == null || apps.isEmpty()) {
2810 return;
2811 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002812
Kenny Guyed131872014-04-30 03:02:21 +01002813 // Create the ApplicationInfos
2814 for (int i = 0; i < apps.size(); i++) {
2815 LauncherActivityInfoCompat app = apps.get(i);
2816 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002817 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002818 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002819
Sunny Goyal756a28a2015-04-23 17:07:55 -07002820 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2821 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002822 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002823
2824 @Override
2825 public void run() {
2826 heuristic.processUserApps(apps);
2827 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002828 };
2829 runOnMainThread(new Runnable() {
2830
2831 @Override
2832 public void run() {
2833 // Check isLoadingWorkspace on the UI thread, as it is updated on
2834 // the UI thread.
2835 if (mIsLoadingAndBindingWorkspace) {
2836 synchronized (mBindCompleteRunnables) {
2837 mBindCompleteRunnables.add(r);
2838 }
2839 } else {
2840 runOnWorkerThread(r);
2841 }
2842 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002843 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002844 }
Winson Chung64359a52013-07-08 17:17:08 -07002845 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002846 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002847 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2848 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002849
2850 // Post callback on main thread
2851 mHandler.post(new Runnable() {
2852 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002853
Winson Chung64359a52013-07-08 17:17:08 -07002854 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002855 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002856 if (callbacks != null) {
2857 callbacks.bindAllApplications(added);
2858 if (DEBUG_LOADERS) {
2859 Log.d(TAG, "bound " + added.size() + " apps in "
2860 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2861 }
2862 } else {
2863 Log.i(TAG, "not binding apps: no Launcher activity");
2864 }
2865 }
2866 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002867 // Cleanup any data stored for a deleted user.
2868 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002869
Sunny Goyal2d648b02015-08-11 13:56:28 -07002870 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002871 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002872 Log.d(TAG, "Icons processed in "
2873 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002874 }
2875 }
2876
2877 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002878 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002879 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002880 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2881 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2882 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2883 }
Joe Onorato36115782010-06-17 13:28:48 -04002884 }
2885 }
2886
Sunny Goyal75b0f552015-05-20 21:57:06 -07002887 /**
2888 * Called when the icons for packages have been updated in the icon cache.
2889 */
2890 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2891 final Callbacks callbacks = getCallback();
2892 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2893 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2894
2895 // If any package icon has changed (app was updated while launcher was dead),
2896 // update the corresponding shortcuts.
2897 synchronized (sBgLock) {
2898 for (ItemInfo info : sBgItemsIdMap) {
2899 if (info instanceof ShortcutInfo && user.equals(info.user)
2900 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2901 ShortcutInfo si = (ShortcutInfo) info;
2902 ComponentName cn = si.getTargetComponent();
2903 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2904 si.updateIcon(mIconCache);
2905 updatedShortcuts.add(si);
2906 }
2907 }
2908 }
2909 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2910 }
2911
2912 if (!updatedShortcuts.isEmpty()) {
2913 final UserHandleCompat userFinal = user;
2914 mHandler.post(new Runnable() {
2915
2916 public void run() {
2917 Callbacks cb = getCallback();
2918 if (cb != null && callbacks == cb) {
2919 cb.bindShortcutsChanged(updatedShortcuts,
2920 new ArrayList<ShortcutInfo>(), userFinal);
2921 }
2922 }
2923 });
2924 }
2925
2926 if (!updatedApps.isEmpty()) {
2927 mHandler.post(new Runnable() {
2928
2929 public void run() {
2930 Callbacks cb = getCallback();
2931 if (cb != null && callbacks == cb) {
2932 cb.bindAppsUpdated(updatedApps);
2933 }
2934 }
2935 });
2936 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002937
2938 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002939 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002940 }
2941
Joe Onorato36115782010-06-17 13:28:48 -04002942 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002943 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002944 }
2945
Adam Cohen091440a2015-03-18 14:16:05 -07002946 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002947
2948 @Override
2949 public void onReceive(Context context, Intent intent) {
2950 synchronized (sBgLock) {
2951 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2952 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002953 final PackageManager manager = context.getPackageManager();
2954 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2955 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002956 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2957 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002958 packagesRemoved.clear();
2959 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002960 for (String pkg : entry.getValue()) {
2961 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002962 boolean packageOnSdcard = launcherApps.isAppEnabled(
2963 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2964 if (packageOnSdcard) {
2965 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2966 packagesUnavailable.add(pkg);
2967 } else {
2968 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2969 packagesRemoved.add(pkg);
2970 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002971 }
2972 }
2973 if (!packagesRemoved.isEmpty()) {
2974 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2975 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2976 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002977 if (!packagesUnavailable.isEmpty()) {
2978 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2979 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2980 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002981 }
Sunny Goyal34942622014-08-29 17:20:55 -07002982 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002983 }
2984 }
2985 }
2986
Joe Onorato36115782010-06-17 13:28:48 -04002987 private class PackageUpdatedTask implements Runnable {
2988 int mOp;
2989 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002990 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002991
2992 public static final int OP_NONE = 0;
2993 public static final int OP_ADD = 1;
2994 public static final int OP_UPDATE = 2;
2995 public static final int OP_REMOVE = 3; // uninstlled
2996 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2997
2998
Kenny Guyed131872014-04-30 03:02:21 +01002999 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003000 mOp = op;
3001 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003002 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003003 }
3004
3005 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003006 if (!mHasLoaderCompletedOnce) {
3007 // Loader has not yet run.
3008 return;
3009 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003010 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003011
3012 final String[] packages = mPackages;
3013 final int N = packages.length;
3014 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003015 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003016 for (int i=0; i<N; i++) {
3017 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003018 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003019 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003020 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003021
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003022 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3023 if (heuristic != null) {
3024 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003025 }
Joe Onorato36115782010-06-17 13:28:48 -04003026 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003027 }
Joe Onorato36115782010-06-17 13:28:48 -04003028 case OP_UPDATE:
3029 for (int i=0; i<N; i++) {
3030 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003031 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003032 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003033 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003034 }
3035 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003036 case OP_REMOVE: {
3037 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3038 if (heuristic != null) {
3039 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003040 }
Joe Onorato36115782010-06-17 13:28:48 -04003041 for (int i=0; i<N; i++) {
3042 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003043 mIconCache.removeIconsForPkg(packages[i], mUser);
3044 }
3045 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003046 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003047 case OP_UNAVAILABLE:
3048 for (int i=0; i<N; i++) {
3049 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3050 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003051 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003052 }
3053 break;
3054 }
3055
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003056 ArrayList<AppInfo> added = null;
3057 ArrayList<AppInfo> modified = null;
3058 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003059
Adam Cohen487f7dd2012-06-28 18:12:10 -07003060 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003061 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003062 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003063 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003064 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003066 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003067 }
Winson Chung5d55f332012-07-16 20:45:03 -07003068 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003069 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003070 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003071 }
3072
Sunny Goyale0f58d72014-11-10 18:05:31 -08003073 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003074 if (callbacks == null) {
3075 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3076 return;
3077 }
3078
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3080 new HashMap<ComponentName, AppInfo>();
3081
Joe Onorato36115782010-06-17 13:28:48 -04003082 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003083 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084 for (AppInfo ai : added) {
3085 addedOrUpdatedApps.put(ai.componentName, ai);
3086 }
Joe Onorato36115782010-06-17 13:28:48 -04003087 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003088
Joe Onorato36115782010-06-17 13:28:48 -04003089 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003090 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 for (AppInfo ai : modified) {
3092 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003093 }
3094
Joe Onorato36115782010-06-17 13:28:48 -04003095 mHandler.post(new Runnable() {
3096 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003097 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003098 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003099 callbacks.bindAppsUpdated(modifiedFinal);
3100 }
3101 }
3102 });
3103 }
Winson Chung83892cc2013-05-01 16:53:33 -07003104
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003106 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3108 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3109 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3110
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003111 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003113 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3115 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003116 boolean infoUpdated = false;
3117 boolean shortcutUpdated = false;
3118
3119 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003120 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003122 Bitmap icon = Utilities.createIconBitmap(
3123 si.iconResource.packageName,
3124 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003125 if (icon != null) {
3126 si.setIcon(icon);
3127 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003128 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 }
3130 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131
3132 ComponentName cn = si.getTargetComponent();
3133 if (cn != null && packageSet.contains(cn.getPackageName())) {
3134 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3135
3136 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003137 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3138 // Auto install icon
3139 PackageManager pm = context.getPackageManager();
3140 ResolveInfo matched = pm.resolveActivity(
3141 new Intent(Intent.ACTION_MAIN)
3142 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3143 PackageManager.MATCH_DEFAULT_ONLY);
3144 if (matched == null) {
3145 // Try to find the best match activity.
3146 Intent intent = pm.getLaunchIntentForPackage(
3147 cn.getPackageName());
3148 if (intent != null) {
3149 cn = intent.getComponent();
3150 appInfo = addedOrUpdatedApps.get(cn);
3151 }
3152
3153 if ((intent == null) || (appInfo == null)) {
3154 removedShortcuts.add(si);
3155 continue;
3156 }
3157 si.promisedIntent = intent;
3158 }
3159 }
3160
3161 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003162 if (appInfo != null) {
3163 si.flags = appInfo.flags;
3164 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165
Sunny Goyal756adbc2015-04-16 15:20:51 -07003166 si.intent = si.promisedIntent;
3167 si.promisedIntent = null;
3168 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 infoUpdated = true;
3170 si.updateIcon(mIconCache);
3171 }
3172
3173 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3174 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3175 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003176 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 si.contentDescription = appInfo.contentDescription;
3178 infoUpdated = true;
3179 }
3180
3181 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3182 // Since package was just updated, the target must be available now.
3183 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3184 shortcutUpdated = true;
3185 }
3186 }
3187
3188 if (infoUpdated || shortcutUpdated) {
3189 updatedShortcuts.add(si);
3190 }
3191 if (infoUpdated) {
3192 updateItemInDatabase(context, si);
3193 }
3194 } else if (info instanceof LauncherAppWidgetInfo) {
3195 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3196 if (mUser.equals(widgetInfo.user)
3197 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3198 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003199 widgetInfo.restoreStatus &=
3200 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3201 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003202
3203 // adding this flag ensures that launcher shows 'click to setup'
3204 // if the widget has a config activity. In case there is no config
3205 // activity, it will be marked as 'restored' during bind.
3206 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3207
Sunny Goyal4390ace2014-10-13 11:33:11 -07003208 widgets.add(widgetInfo);
3209 updateItemInDatabase(context, widgetInfo);
3210 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003211 }
3212 }
3213 }
3214
Sunny Goyal4390ace2014-10-13 11:33:11 -07003215 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3216 mHandler.post(new Runnable() {
3217
3218 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003219 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003220 if (callbacks == cb && cb != null) {
3221 callbacks.bindShortcutsChanged(
3222 updatedShortcuts, removedShortcuts, mUser);
3223 }
3224 }
3225 });
3226 if (!removedShortcuts.isEmpty()) {
3227 deleteItemsFromDatabase(context, removedShortcuts);
3228 }
3229 }
3230 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003231 mHandler.post(new Runnable() {
3232 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003233 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003234 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003235 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003236 }
3237 }
3238 });
3239 }
3240 }
3241
Winson Chungdf95eb12013-10-16 14:57:07 -07003242 final ArrayList<String> removedPackageNames =
3243 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003244 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 // Mark all packages in the broadcast to be removed
3246 removedPackageNames.addAll(Arrays.asList(packages));
3247 } else if (mOp == OP_UPDATE) {
3248 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003249 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003250 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003252 }
3253 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003254 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003255
Winson Chungdf95eb12013-10-16 14:57:07 -07003256 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003257 final int removeReason;
3258 if (mOp == OP_UNAVAILABLE) {
3259 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3260 } else {
3261 // Remove all the components associated with this package
3262 for (String pn : removedPackageNames) {
3263 deletePackageFromDatabase(context, pn, mUser);
3264 }
3265 // Remove all the specific components
3266 for (AppInfo a : removedApps) {
3267 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3268 deleteItemsFromDatabase(context, infos);
3269 }
3270 removeReason = 0;
3271 }
3272
Winson Chungdf95eb12013-10-16 14:57:07 -07003273 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003274 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003275 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003276 mHandler.post(new Runnable() {
3277 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003278 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003279 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003280 callbacks.bindComponentsRemoved(
3281 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003282 }
3283 }
3284 });
Joe Onoratobe386092009-11-17 17:32:16 -08003285 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003286
Sunny Goyal2d648b02015-08-11 13:56:28 -07003287 // Update widgets
3288 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3289 // Always refresh for a package event on secondary user
3290 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3291
3292 // Refresh widget list, if the package already had a widget.
3293 synchronized (sBgLock) {
3294 if (sBgWidgetProviders != null) {
3295 HashSet<String> pkgSet = new HashSet<>();
3296 Collections.addAll(pkgSet, mPackages);
3297
3298 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3299 needToRefresh |= key.user.equals(mUser) &&
3300 pkgSet.contains(key.componentName.getPackageName());
3301 }
3302 }
3303 }
3304
3305 if (!needToRefresh && mOp != OP_REMOVE) {
3306 // Refresh widget list, if there is any newly added widget
3307 PackageManager pm = context.getPackageManager();
3308 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003309 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003310 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003311 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003312 .setPackage(pkg), 0);
3313 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003314 } catch (RuntimeException e) {
3315 // Ignore the crash. We can live with a state widget list.
3316 Log.e(TAG, "PM call failed for " + pkg, e);
3317 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003318 }
3319 }
3320
3321 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3322 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003323
Adam Cohen4caf2982013-08-20 18:54:31 -07003324 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003325 mHandler.post(new Runnable() {
3326 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003327 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003328 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003329 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003330 }
3331 }
3332 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 }
3334 }
3335
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003336 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3337 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003338 ArrayList<LauncherAppWidgetProviderInfo> results =
3339 new ArrayList<LauncherAppWidgetProviderInfo>();
3340 try {
3341 synchronized (sBgLock) {
3342 if (sBgWidgetProviders == null || refresh) {
3343 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3344 = new HashMap<>();
3345 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3346 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003347
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003348 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3349 for (AppWidgetProviderInfo pInfo : widgets) {
3350 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3351 UserHandleCompat user = wm.getUser(info);
3352 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3353 }
Robin Lee26ace122015-03-16 19:41:43 +00003354
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003355 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3356 for (CustomAppWidget widget : customWidgets) {
3357 info = new LauncherAppWidgetProviderInfo(context, widget);
3358 UserHandleCompat user = wm.getUser(info);
3359 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3360 }
3361 // Replace the global list at the very end, so that if there is an exception,
3362 // previously loaded provider list is used.
3363 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003364 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003365 results.addAll(sBgWidgetProviders.values());
3366 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003367 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003368 } catch (Exception e) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003369 if (e.getCause() instanceof TransactionTooLargeException ||
3370 e.getCause() instanceof DeadObjectException) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003371 // the returned value may be incomplete and will not be refreshed until the next
3372 // time Launcher starts.
3373 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3374 // onResume is called to refresh the widget provider list.
3375 synchronized (sBgLock) {
3376 if (sBgWidgetProviders != null) {
3377 results.addAll(sBgWidgetProviders.values());
3378 }
3379 return results;
3380 }
3381 } else {
3382 throw e;
3383 }
Adam Cohen59400422014-03-05 18:07:04 -08003384 }
3385 }
3386
Robin Lee26ace122015-03-16 19:41:43 +00003387 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3388 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003389 synchronized (sBgLock) {
3390 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003391 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003392 }
Robin Lee26ace122015-03-16 19:41:43 +00003393 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003394 }
3395 }
3396
Sunny Goyal2d648b02015-08-11 13:56:28 -07003397 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003398
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003399 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003400 @Override
3401 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003402 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003403 final WidgetsModel model = mBgWidgetsModel.clone();
3404
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003405 mHandler.post(new Runnable() {
3406 @Override
3407 public void run() {
3408 Callbacks cb = getCallback();
3409 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003410 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003411 }
3412 }
3413 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003414 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003415 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3416 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003417 }
3418 });
3419 }
3420
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003421 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003422 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003423 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003424 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003425 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003426 @Thunk void updateWidgetsModel(boolean refresh) {
3427 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003428 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003429 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003430 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3431 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003432 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003433 }
3434
Adam Cohen091440a2015-03-18 14:16:05 -07003435 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003436 UserHandleCompat user) {
3437 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3438 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003439 }
Adam Cohen556f6132014-01-15 15:18:08 -08003440
Kenny Guyed131872014-04-30 03:02:21 +01003441 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3442 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003443 if (cn == null) {
3444 return false;
3445 }
Kenny Guyed131872014-04-30 03:02:21 +01003446 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3447 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003448 return false;
3449 }
Kenny Guyed131872014-04-30 03:02:21 +01003450 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003451 }
3452
Adam Cohena28b78e2014-05-20 17:03:04 -07003453 public static boolean isValidPackage(Context context, String packageName,
3454 UserHandleCompat user) {
3455 if (packageName == null) {
3456 return false;
3457 }
3458 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3459 return launcherApps.isPackageEnabledForProfile(packageName, user);
3460 }
3461
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003463 * Make an ShortcutInfo object for a restored application or shortcut item that points
3464 * to a package that is not yet installed on the system.
3465 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003466 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003467 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003468 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003469 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003470
3471 Bitmap icon = iconInfo.loadIcon(c, info, context);
3472 // the fallback icon
3473 if (icon == null) {
3474 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3475 } else {
3476 info.setIcon(icon);
3477 }
Sunny Goyal34942622014-08-29 17:20:55 -07003478
3479 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003480 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003481 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003482 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003483 }
Sunny Goyal34942622014-08-29 17:20:55 -07003484 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3485 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003486 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003487 }
Sunny Goyal34942622014-08-29 17:20:55 -07003488 } else {
3489 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3490 }
3491
Winson Chung82b016c2015-05-08 17:00:10 -07003492 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003493 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003494 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003495 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003496 return info;
3497 }
3498
3499 /**
3500 * Make an Intent object for a restored application or shortcut item that points
3501 * to the market page for the item.
3502 */
Adam Cohen091440a2015-03-18 14:16:05 -07003503 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003504 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003505 return getMarketIntent(componentName.getPackageName());
3506 }
3507
3508 static Intent getMarketIntent(String packageName) {
3509 return new Intent(Intent.ACTION_VIEW)
3510 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003511 .scheme("market")
3512 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003513 .appendQueryParameter("id", packageName)
3514 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003515 }
3516
3517 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003518 * Make an ShortcutInfo object for a shortcut that is an application.
3519 *
3520 * If c is not null, then it will be used to fill in missing data like the title and icon.
3521 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003522 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003523 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003524 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003525 if (user == null) {
3526 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003527 return null;
3528 }
3529
Kenny Guyed131872014-04-30 03:02:21 +01003530 ComponentName componentName = intent.getComponent();
3531 if (componentName == null) {
3532 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3533 return null;
3534 }
3535
3536 Intent newIntent = new Intent(intent.getAction(), null);
3537 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3538 newIntent.setComponent(componentName);
3539 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003540 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003541 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3542 return null;
3543 }
3544
3545 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003546 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003547 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3548 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3549 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003550 }
3551
Joe Onorato56d82912010-03-07 14:32:10 -05003552 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003553 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003554 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003555 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003556
Joe Onorato56d82912010-03-07 14:32:10 -05003557 // fall back to the class name of the activity
3558 if (info.title == null) {
3559 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003560 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003561
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003563 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003564 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003565 if (lai != null) {
3566 info.flags = AppInfo.initFlags(lai);
3567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003569 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003570
Sunny Goyale2df0622015-04-24 11:27:00 -07003571 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003572 ItemInfoFilter f) {
3573 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3574 for (ItemInfo i : infos) {
3575 if (i instanceof ShortcutInfo) {
3576 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003577 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003578 if (cn != null && f.filterItem(null, info, cn)) {
3579 filtered.add(info);
3580 }
3581 } else if (i instanceof FolderInfo) {
3582 FolderInfo info = (FolderInfo) i;
3583 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003584 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003585 if (cn != null && f.filterItem(info, s, cn)) {
3586 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003587 }
3588 }
Winson Chung64359a52013-07-08 17:17:08 -07003589 } else if (i instanceof LauncherAppWidgetInfo) {
3590 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3591 ComponentName cn = info.providerName;
3592 if (cn != null && f.filterItem(null, info, cn)) {
3593 filtered.add(info);
3594 }
Winson Chung8a435102012-08-30 17:16:53 -07003595 }
3596 }
Winson Chung64359a52013-07-08 17:17:08 -07003597 return new ArrayList<ItemInfo>(filtered);
3598 }
3599
Adam Cohen091440a2015-03-18 14:16:05 -07003600 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003601 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003602 ItemInfoFilter filter = new ItemInfoFilter() {
3603 @Override
3604 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003605 if (info.user == null) {
3606 return cn.equals(cname);
3607 } else {
3608 return cn.equals(cname) && info.user.equals(user);
3609 }
Winson Chung64359a52013-07-08 17:17:08 -07003610 }
3611 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003612 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003613 }
3614
Sunny Goyal1a745e82014-10-02 15:58:31 -07003615 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003616 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003617 */
Adam Cohen091440a2015-03-18 14:16:05 -07003618 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003619 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003620 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003621 // Non-app shortcuts are only supported for current user.
3622 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003624
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003625 // TODO: If there's an explicit component and we can't install that, delete it.
3626
Winson Chung82b016c2015-05-08 17:00:10 -07003627 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003628
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003629 Bitmap icon = iconInfo.loadIcon(c, info, context);
3630 // the fallback icon
3631 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003632 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003633 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003634 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003635 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 return info;
3637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638
Sunny Goyal2350bc92014-10-14 16:42:54 -07003639 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003640 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3641 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3642 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3643
Adam Cohend9198822011-11-22 16:42:47 -08003644 if (intent == null) {
3645 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3646 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3647 return null;
3648 }
3649
Joe Onorato0589f0f2010-02-08 13:44:00 -08003650 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003651 boolean customIcon = false;
3652 ShortcutIconResource iconResource = null;
3653
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003654 if (bitmap instanceof Bitmap) {
3655 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003656 customIcon = true;
3657 } else {
3658 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003659 if (extra instanceof ShortcutIconResource) {
3660 iconResource = (ShortcutIconResource) extra;
3661 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003662 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003663 }
3664 }
3665
Michael Jurkac9d95c52011-08-29 14:03:34 -07003666 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003667
Kenny Guyed131872014-04-30 03:02:21 +01003668 // Only support intents for current user for now. Intents sent from other
3669 // users wouldn't get here without intent forwarding anyway.
3670 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003671 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003672 icon = mIconCache.getDefaultIcon(info.user);
3673 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003674 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003675 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003676
Winson Chung82b016c2015-05-08 17:00:10 -07003677 info.title = Utilities.trim(name);
3678 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003679 info.intent = intent;
3680 info.customIcon = customIcon;
3681 info.iconResource = iconResource;
3682
3683 return info;
3684 }
3685
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003687 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 * or make a new one.
3689 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003690 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 // See if a placeholder was created for us already
3692 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003693 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003695 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 folders.put(id, folderInfo);
3697 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003698 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003699 }
3700
Joe Onoratobe386092009-11-17 17:32:16 -08003701
Sunny Goyal651077b2014-06-30 14:15:31 -07003702 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3703 return (provider != null) && (provider.provider != null)
3704 && (provider.provider.getPackageName() != null);
3705 }
3706
Joe Onoratobe386092009-11-17 17:32:16 -08003707 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003708 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003709 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3710 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3711 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3712 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003713 if (mLoaderTask != null) {
3714 mLoaderTask.dumpState();
3715 } else {
3716 Log.d(TAG, "mLoaderTask=null");
3717 }
Joe Onoratobe386092009-11-17 17:32:16 -08003718 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003719
3720 public Callbacks getCallback() {
3721 return mCallbacks != null ? mCallbacks.get() : null;
3722 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003723
3724 /**
3725 * @return {@link FolderInfo} if its already loaded.
3726 */
3727 public FolderInfo findFolderById(Long folderId) {
3728 synchronized (sBgLock) {
3729 return sBgFolders.get(folderId);
3730 }
3731 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003732
3733 /**
3734 * @return the looper for the worker thread which can be used to start background tasks.
3735 */
3736 public static Looper getWorkerLooper() {
3737 return sWorkerThread.getLooper();
3738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003739}