blob: a386ebd7f7e8d4ae0f704f73866e6b66e234af21 [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;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070058import com.android.launcher3.config.ProviderConfig;
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 &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700652 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700653 // For all intents and purposes, this is the same object
654 return;
655 }
656 }
657
658 // the modelItem needs to match up perfectly with item if our model is
659 // to be consistent with the database-- for now, just require
660 // modelItem == item or the equality check above
661 String msg = "item: " + ((item != null) ? item.toString() : "null") +
662 "modelItem: " +
663 ((modelItem != null) ? modelItem.toString() : "null") +
664 "Error: ItemInfo passed to checkItemInfo doesn't match original";
665 RuntimeException e = new RuntimeException(msg);
666 if (stackTrace != null) {
667 e.setStackTrace(stackTrace);
668 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800669 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700670 }
671 }
672
Michael Jurka816474f2012-06-25 14:49:02 -0700673 static void checkItemInfo(final ItemInfo item) {
674 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
675 final long itemId = item.id;
676 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700677 public void run() {
678 synchronized (sBgLock) {
679 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700680 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 }
682 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700683 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700684 }
685
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
687 final ItemInfo item, final String callingFunction) {
688 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700689 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 final ContentResolver cr = context.getContentResolver();
691
Adam Cohen487f7dd2012-06-28 18:12:10 -0700692 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700693 Runnable r = new Runnable() {
694 public void run() {
695 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 updateItemArrays(item, itemId, stackTrace);
697 }
698 };
699 runOnWorkerThread(r);
700 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
703 final ArrayList<ItemInfo> items, final String callingFunction) {
704 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700705
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
707 Runnable r = new Runnable() {
708 public void run() {
709 ArrayList<ContentProviderOperation> ops =
710 new ArrayList<ContentProviderOperation>();
711 int count = items.size();
712 for (int i = 0; i < count; i++) {
713 ItemInfo item = items.get(i);
714 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700715 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700717
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
719 updateItemArrays(item, itemId, stackTrace);
720
721 }
722 try {
723 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
724 } catch (Exception e) {
725 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726 }
727 }
728 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700729 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700731
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
733 // Lock on mBgLock *after* the db operation
734 synchronized (sBgLock) {
735 checkItemInfoLocked(itemId, item, stackTrace);
736
737 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
738 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
739 // Item is in a folder, make sure this folder exists
740 if (!sBgFolders.containsKey(item.container)) {
741 // An items container is being set to a that of an item which is not in
742 // the list of Folders.
743 String msg = "item: " + item + " container being set to: " +
744 item.container + ", not in the list of folders";
745 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 }
747 }
748
749 // Items are added/removed from the corresponding FolderInfo elsewhere, such
750 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
751 // that are on the desktop, as appropriate
752 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800753 if (modelItem != null &&
754 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
755 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 switch (modelItem.itemType) {
757 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
758 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
759 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
760 if (!sBgWorkspaceItems.contains(modelItem)) {
761 sBgWorkspaceItems.add(modelItem);
762 }
763 break;
764 default:
765 break;
766 }
767 } else {
768 sBgWorkspaceItems.remove(modelItem);
769 }
770 }
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700775 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700776 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 item.cellX = cellX;
780 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 // We store hotseat items in canonical form which is this orientation invariant position
783 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790
791 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
794 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800795 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700799 }
800
801 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
803 * cellX, cellY have already been updated on the ItemInfos.
804 */
805 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
806 final long container, final int screen) {
807
808 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
809 int count = items.size();
810
811 for (int i = 0; i < count; i++) {
812 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 item.container = container;
814
815 // We store hotseat items in canonical form which is this orientation invariant position
816 // in the hotseat
817 if (context instanceof Launcher && screen < 0 &&
818 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 item.cellY);
821 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 }
824
825 final ContentValues values = new ContentValues();
826 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
827 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
828 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800829 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831
832 contentValues.add(values);
833 }
834 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
835 }
836
837 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800839 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700840 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700842 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800843 item.cellX = cellX;
844 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 item.spanX = spanX;
846 item.spanY = spanY;
847
848 // We store hotseat items in canonical form which is this orientation invariant position
849 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 }
Adam Cohend4844c32011-02-18 19:25:06 -0800856
Adam Cohend4844c32011-02-18 19:25:06 -0800857 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800858 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
860 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800861 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
863 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800865
Michael Jurka816474f2012-06-25 14:49:02 -0700866 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868
869 /**
870 * Update an item to the database in a specified container.
871 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700872 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100874 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800876 }
877
Sunny Goyal756a28a2015-04-23 17:07:55 -0700878 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700879 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700880 synchronized (mLock) {
881 if (!mHasLoaderCompletedOnce ||
882 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
883 throw new RuntimeException("Trying to add shortcut while loader is running");
884 }
885 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700886 }
887 }
888
Adam Cohend4844c32011-02-18 19:25:06 -0800889 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700890 * Returns true if the shortcuts already exists on the workspace. This must be called after
891 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700893 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
894 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700895 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 if (intent.getComponent() != null) {
897 // If component is not null, an intent with null package will produce
898 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700899 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = intent.toUri(0);
902 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700904 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
905 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700906 }
907 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 intentWithPkg = intent.toUri(0);
909 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700910 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911
912 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700913 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700914 if (item instanceof ShortcutInfo) {
915 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700916 Intent targetIntent = info.promisedIntent == null
917 ? info.intent : info.promisedIntent;
918 if (targetIntent != null && info.user.equals(user)) {
919 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
921 return true;
922 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700923 }
924 }
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700928 }
929
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 /**
931 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
932 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700933 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 final ContentResolver cr = context.getContentResolver();
935 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
936 "_id=? and (itemType=? or itemType=?)",
937 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700938 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700939
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 try {
941 if (c.moveToFirst()) {
942 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
943 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
944 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
945 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
946 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
947 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700948 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 FolderInfo folderInfo = null;
951 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700952 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
953 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700955 }
956
Sunny Goyala508e4f2015-05-21 09:33:57 -0700957 // Do not trim the folder label, as is was set by the user.
958 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959 folderInfo.id = id;
960 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700962 folderInfo.cellX = c.getInt(cellXIndex);
963 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700964 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
966 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 } finally {
969 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700970 }
971
972 return null;
973 }
974
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 /**
976 * Add an item to the database in a specified container. Sets the container, screen, cellX and
977 * cellY fields of the item. Also assigns an ID to the item.
978 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700979 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700980 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 item.cellX = cellX;
983 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 // We store hotseat items in canonical form which is this orientation invariant position
985 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992
993 final ContentValues values = new ContentValues();
994 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100995 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
Michael Jurka414300a2013-08-27 15:42:35 +0200997 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700998 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700999
Jason Monk8e19cf22014-03-20 15:06:57 -04001000 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001001 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001003 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001006 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001007 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 sBgItemsIdMap.put(item.id, item);
1009 switch (item.itemType) {
1010 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1011 sBgFolders.put(item.id, (FolderInfo) item);
1012 // Fall through
1013 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1014 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1015 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1016 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1017 sBgWorkspaceItems.add(item);
1018 } else {
1019 if (!sBgFolders.containsKey(item.container)) {
1020 // Adding an item to a folder that doesn't exist.
1021 String msg = "adding item: " + item + " to a folder that " +
1022 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001023 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001025 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 break;
1027 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1028 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1029 break;
1030 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001031 }
1032 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001035 }
1036
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001038 * Creates a new unique child id, for a given cell span across all layouts.
1039 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001040 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001041 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001042 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001043 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001044 }
1045
Sunny Goyal34942622014-08-29 17:20:55 -07001046 private static ArrayList<ItemInfo> getItemsByPackageName(
1047 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001048 ItemInfoFilter filter = new ItemInfoFilter() {
1049 @Override
1050 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1051 return cn.getPackageName().equals(pn) && info.user.equals(user);
1052 }
1053 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001054 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001055 }
1056
1057 /**
1058 * Removes all the items from the database corresponding to the specified package.
1059 */
1060 static void deletePackageFromDatabase(Context context, final String pn,
1061 final UserHandleCompat user) {
1062 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001063 }
1064
1065 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001066 * Removes the specified item from the database
1067 * @param context
1068 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001070 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001071 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1072 items.add(item);
1073 deleteItemsFromDatabase(context, items);
1074 }
1075
1076 /**
1077 * Removes the specified items from the database
1078 * @param context
1079 * @param item
1080 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001081 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001083 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001084 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001085 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001086 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001087 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001088
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001089 // Lock on mBgLock *after* the db operation
1090 synchronized (sBgLock) {
1091 switch (item.itemType) {
1092 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1093 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001094 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 if (info.container == item.id) {
1096 // We are deleting a folder which still contains items that
1097 // think they are contained by that folder.
1098 String msg = "deleting a folder (" + item + ") which still " +
1099 "contains items (" + info + ")";
1100 Log.e(TAG, msg);
1101 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 sBgWorkspaceItems.remove(item);
1104 break;
1105 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1106 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1107 sBgWorkspaceItems.remove(item);
1108 break;
1109 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1110 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1111 break;
1112 }
1113 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115 }
1116 }
Michael Jurka83df1882011-08-31 20:59:26 -07001117 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 }
1120
1121 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 * Update the order of the workspace screens in the database. The array list contains
1123 * a list of screen ids in the order that they should appear.
1124 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001125 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001126 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 final ContentResolver cr = context.getContentResolver();
1128 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1129
1130 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001131 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 while (iter.hasNext()) {
1133 long id = iter.next();
1134 if (id < 0) {
1135 iter.remove();
1136 }
1137 }
1138
1139 Runnable r = new Runnable() {
1140 @Override
1141 public void run() {
Yura085c8532014-02-11 15:15:29 +00001142 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001144 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001145 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 for (int i = 0; i < count; i++) {
1147 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001148 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1150 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001151 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 }
Yura085c8532014-02-11 15:15:29 +00001153
1154 try {
1155 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1156 } catch (Exception ex) {
1157 throw new RuntimeException(ex);
1158 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001159
Winson Chungba9c37f2013-08-30 14:11:37 -07001160 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001161 sBgWorkspaceScreens.clear();
1162 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001163 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 }
1165 };
1166 runOnWorkerThread(r);
1167 }
1168
1169 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 * Remove the contents of the specified folder from the database
1171 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001172 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 final ContentResolver cr = context.getContentResolver();
1174
Michael Jurkac9d95c52011-08-29 14:03:34 -07001175 Runnable r = new Runnable() {
1176 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001177 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001179 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 sBgItemsIdMap.remove(info.id);
1181 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 sBgWorkspaceItems.remove(info);
1183 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001184
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001185 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001186 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001188 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 for (ItemInfo childInfo : info.contents) {
1190 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001192 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 }
1194 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 }
1197
1198 /**
1199 * Set this as the current Launcher activity object for the loader.
1200 */
1201 public void initialize(Callbacks callbacks) {
1202 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001203 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1204 // workspace to prevent leaking Launcher activities on orientation change.
1205 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 mCallbacks = new WeakReference<Callbacks>(callbacks);
1207 }
1208 }
1209
Kenny Guyed131872014-04-30 03:02:21 +01001210 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001211 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001212 int op = PackageUpdatedTask.OP_UPDATE;
1213 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1214 user));
1215 }
1216
1217 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001218 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001219 int op = PackageUpdatedTask.OP_REMOVE;
1220 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1221 user));
1222 }
1223
1224 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001225 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001226 int op = PackageUpdatedTask.OP_ADD;
1227 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1228 user));
1229 }
1230
1231 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001232 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001233 boolean replacing) {
1234 if (!replacing) {
1235 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1236 user));
1237 if (mAppsCanBeOnRemoveableStorage) {
1238 // Only rebind if we support removable storage. It catches the
1239 // case where
1240 // apps on the external sd card need to be reloaded
1241 startLoaderFromBackground();
1242 }
1243 } else {
1244 // If we are replacing then just update the packages in the list
1245 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1246 packageNames, user));
1247 }
1248 }
1249
1250 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001251 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001252 boolean replacing) {
1253 if (!replacing) {
1254 enqueuePackageUpdated(new PackageUpdatedTask(
1255 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1256 user));
1257 }
Kenny Guyed131872014-04-30 03:02:21 +01001258 }
1259
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001260 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1262 * ACTION_PACKAGE_CHANGED.
1263 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001264 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001265 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001266 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001267
Joe Onorato36115782010-06-17 13:28:48 -04001268 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001269 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001270 // If we have changed locale we need to clear out the labels in all apps/workspace.
1271 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001272 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001273 Callbacks callbacks = getCallback();
1274 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001275 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001276 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001277 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1278 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001279 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001280 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001281 }
1282 }
1283
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001284 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001285 resetLoadedState(true, true);
1286
Reena Lee93f824a2011-09-23 17:20:28 -07001287 // Do this here because if the launcher activity is running it will be restarted.
1288 // If it's not running startLoaderFromBackground will merely tell it that it needs
1289 // to reload.
1290 startLoaderFromBackground();
1291 }
1292
Winson Chungf0c6ae02012-03-21 16:10:31 -07001293 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1294 synchronized (mLock) {
1295 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1296 // mWorkspaceLoaded to true later
1297 stopLoaderLocked();
1298 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1299 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1300 }
1301 }
1302
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001303 /**
1304 * When the launcher is in the background, it's possible for it to miss paired
1305 * configuration changes. So whenever we trigger the loader from the background
1306 * tell the launcher that it needs to re-run the loader when it comes back instead
1307 * of doing it now.
1308 */
1309 public void startLoaderFromBackground() {
1310 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001311 Callbacks callbacks = getCallback();
1312 if (callbacks != null) {
1313 // Only actually run the loader if they're not paused.
1314 if (!callbacks.setLoadOnResume()) {
1315 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001316 }
1317 }
1318 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001319 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001320 }
Joe Onorato36115782010-06-17 13:28:48 -04001321 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001322
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001323 /**
1324 * If there is already a loader task running, tell it to stop.
1325 */
1326 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001327 LoaderTask oldTask = mLoaderTask;
1328 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001329 oldTask.stopLocked();
1330 }
Reena Lee93f824a2011-09-23 17:20:28 -07001331 }
1332
Adam Cohen1a85c582014-09-30 09:48:49 -07001333 public boolean isCurrentCallbacks(Callbacks callbacks) {
1334 return (mCallbacks != null && mCallbacks.get() == callbacks);
1335 }
1336
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001337 public void startLoader(int synchronousBindPage) {
1338 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001339 }
1340
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001341 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001342 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1343 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001344 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 // Clear any deferred bind-runnables from the synchronized load process
1346 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001347 synchronized (mDeferredBindRunnables) {
1348 mDeferredBindRunnables.clear();
1349 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350
Joe Onorato36115782010-06-17 13:28:48 -04001351 // Don't bother to start the thread if we know it's not going to do anything
1352 if (mCallbacks != null && mCallbacks.get() != null) {
1353 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001354 stopLoaderLocked();
1355 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001356 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001357 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1359 } else {
1360 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1361 sWorker.post(mLoaderTask);
1362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 }
1365 }
1366
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367 void bindRemainingSynchronousPages() {
1368 // Post the remaining side pages to be loaded
1369 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001370 Runnable[] deferredBindRunnables = null;
1371 synchronized (mDeferredBindRunnables) {
1372 deferredBindRunnables = mDeferredBindRunnables.toArray(
1373 new Runnable[mDeferredBindRunnables.size()]);
1374 mDeferredBindRunnables.clear();
1375 }
1376 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001377 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 }
1380 }
1381
Joe Onorato36115782010-06-17 13:28:48 -04001382 public void stopLoader() {
1383 synchronized (mLock) {
1384 if (mLoaderTask != null) {
1385 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
Joe Onorato36115782010-06-17 13:28:48 -04001388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001390 /**
1391 * Loads the workspace screen ids in an ordered list.
1392 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001393 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001394 final ContentResolver contentResolver = context.getContentResolver();
1395 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001396
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001397 // Get screens ordered by rank.
1398 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1399 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1400 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001401 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001402 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001403 while (sc.moveToNext()) {
1404 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001405 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001406 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001407 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1408 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001409 }
1410 }
1411 } finally {
1412 sc.close();
1413 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001414 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001415 }
1416
Michael Jurkac57b7a82011-08-09 22:02:20 -07001417 public boolean isAllAppsLoaded() {
1418 return mAllAppsLoaded;
1419 }
1420
Joe Onorato36115782010-06-17 13:28:48 -04001421 /**
1422 * Runnable for the thread that loads the contents of the launcher:
1423 * - workspace icons
1424 * - widgets
1425 * - all apps icons
1426 */
1427 private class LoaderTask implements Runnable {
1428 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001429 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001430 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001431 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001432 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001434 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001435 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001436 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001439 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001440 mIsLoadingAndBindingWorkspace = true;
1441
Joe Onorato36115782010-06-17 13:28:48 -04001442 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001443 if (DEBUG_LOADERS) {
1444 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001446
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001447 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001448 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001449 synchronized (LoaderTask.this) {
1450 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001451 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001452 }
1453 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 }
1455 }
1456
Joe Onorato36115782010-06-17 13:28:48 -04001457 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001458 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001459 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001460
Joe Onorato36115782010-06-17 13:28:48 -04001461 private void waitForIdle() {
1462 // Wait until the either we're stopped or the other threads are done.
1463 // This way we don't start loading all apps until the workspace has settled
1464 // down.
1465 synchronized (LoaderTask.this) {
1466 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001467
Joe Onorato36115782010-06-17 13:28:48 -04001468 mHandler.postIdle(new Runnable() {
1469 public void run() {
1470 synchronized (LoaderTask.this) {
1471 mLoadAndBindStepFinished = true;
1472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001476 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478 });
1479
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001480 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001481 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001482 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1483 // wait no longer than 1sec at a time
1484 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001485 } catch (InterruptedException ex) {
1486 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001487 }
1488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 if (DEBUG_LOADERS) {
1490 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001491 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001492 + "ms for previous step to finish binding");
1493 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001494 }
Joe Onorato36115782010-06-17 13:28:48 -04001495 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001498 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 // Ensure that we have a valid page index to load synchronously
1500 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1501 "valid page index");
1502 }
1503 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1504 // Ensure that we don't try and bind a specified page when the pages have not been
1505 // loaded already (we should load everything asynchronously in that case)
1506 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1507 }
1508 synchronized (mLock) {
1509 if (mIsLoaderTaskRunning) {
1510 // Ensure that we are never running the background loading at this point since
1511 // we also touch the background collections
1512 throw new RuntimeException("Error! Background loading is already running");
1513 }
1514 }
1515
1516 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1517 // data structures, we can't allow any other thread to touch that data, but because
1518 // this call is synchronous, we can get away with not locking).
1519
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001520 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001521 // operations from the previous activity. We need to ensure that all queued operations
1522 // are executed before any synchronous binding work is done.
1523 mHandler.flush();
1524
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 // Divide the set of loaded items into those that we are binding synchronously, and
1526 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001527 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1529 // arise from that.
1530 onlyBindAllApps();
1531 }
1532
Joe Onorato36115782010-06-17 13:28:48 -04001533 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001535 if (mStopped) {
1536 return;
1537 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001538 mIsLoaderTaskRunning = true;
1539 }
Joe Onorato36115782010-06-17 13:28:48 -04001540 // Optimize for end-user experience: if the Launcher is up and // running with the
1541 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1542 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001543 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001544 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001545 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001546
Joe Onorato36115782010-06-17 13:28:48 -04001547 if (mStopped) {
1548 break keep_running;
1549 }
1550
Joe Onorato36115782010-06-17 13:28:48 -04001551 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001552
1553 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001554 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1555 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557
Joe Onorato36115782010-06-17 13:28:48 -04001558 // Clear out this reference, otherwise we end up holding it until all of the
1559 // callback runnables are done.
1560 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561
Joe Onorato36115782010-06-17 13:28:48 -04001562 synchronized (mLock) {
1563 // If we are still the last one to be scheduled, remove ourselves.
1564 if (mLoaderTask == this) {
1565 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001568 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001569 }
Joe Onorato36115782010-06-17 13:28:48 -04001570 }
1571
1572 public void stopLocked() {
1573 synchronized (LoaderTask.this) {
1574 mStopped = true;
1575 this.notify();
1576 }
1577 }
1578
1579 /**
1580 * Gets the callbacks object. If we've been stopped, or if the launcher object
1581 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1582 * object that was around when the deferred message was scheduled, and if there's
1583 * a new Callbacks object around then also return null. This will save us from
1584 * calling onto it with data that will be ignored.
1585 */
1586 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1587 synchronized (mLock) {
1588 if (mStopped) {
1589 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001590 }
Joe Onorato36115782010-06-17 13:28:48 -04001591
1592 if (mCallbacks == null) {
1593 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001594 }
Joe Onorato36115782010-06-17 13:28:48 -04001595
1596 final Callbacks callbacks = mCallbacks.get();
1597 if (callbacks != oldCallbacks) {
1598 return null;
1599 }
1600 if (callbacks == null) {
1601 Log.w(TAG, "no mCallbacks");
1602 return null;
1603 }
1604
1605 return callbacks;
1606 }
1607 }
1608
1609 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001610 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001611 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001612 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1613 final int countX = (int) profile.numColumns;
1614 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001615
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001617 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001618 // Return early if we detect that an item is under the hotseat button
1619 if (mCallbacks == null ||
1620 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001621 Log.e(TAG, "Error loading shortcut into hotseat " + item
1622 + " into position (" + item.screenId + ":" + item.cellX + ","
1623 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001624 return false;
1625 }
1626
Dan Sandler295ae182013-12-10 16:05:47 -05001627 final ItemInfo[][] hotseatItems =
1628 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1629
Adam Cohen2e6da152015-05-06 11:42:25 -07001630 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001631 Log.e(TAG, "Error loading shortcut " + item
1632 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001633 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001634 + ")");
1635 return false;
1636 }
1637
Dan Sandler295ae182013-12-10 16:05:47 -05001638 if (hotseatItems != null) {
1639 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001640 Log.e(TAG, "Error loading shortcut into hotseat " + item
1641 + " into position (" + item.screenId + ":" + item.cellX + ","
1642 + item.cellY + ") occupied by "
1643 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1644 [(int) item.screenId][0]);
1645 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001646 } else {
1647 hotseatItems[(int) item.screenId][0] = item;
1648 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001650 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001651 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001652 items[(int) item.screenId][0] = item;
1653 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001654 return true;
1655 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001656 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1657 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001658 return true;
1659 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001660
Adam Cohendcd297f2013-06-18 13:13:40 -07001661 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001662 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001663 occupied.put(item.screenId, items);
1664 }
1665
Dan Sandler295ae182013-12-10 16:05:47 -05001666 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001667 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1668 item.cellX < 0 || item.cellY < 0 ||
1669 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1670 Log.e(TAG, "Error loading shortcut " + item
1671 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1672 + item.cellX + "," + item.cellY
1673 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1674 return false;
1675 }
1676
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001677 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001678 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1679 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001680 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001681 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001682 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001683 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001684 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001685 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001686 return false;
1687 }
1688 }
1689 }
1690 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1691 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001693 }
1694 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001695
Joe Onorato36115782010-06-17 13:28:48 -04001696 return true;
1697 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698
Winson Chungba9c37f2013-08-30 14:11:37 -07001699 /** Clears all the sBg data structures */
1700 private void clearSBgDataStructures() {
1701 synchronized (sBgLock) {
1702 sBgWorkspaceItems.clear();
1703 sBgAppWidgets.clear();
1704 sBgFolders.clear();
1705 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001706 sBgWorkspaceScreens.clear();
1707 }
1708 }
1709
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001710 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001711 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Joe Onorato36115782010-06-17 13:28:48 -04001713 final Context context = mContext;
1714 final ContentResolver contentResolver = context.getContentResolver();
1715 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001716 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001717 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001718 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001719
Winson Chung892c74d2013-08-22 16:15:50 -07001720 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001721 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1722 int countX = (int) profile.numColumns;
1723 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001724
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001725 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001726 long migrationStartTime = System.currentTimeMillis();
1727 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001728 try {
1729 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1730 // Clear the flags before starting the task, so that we do not run the task
1731 // again, in case there was an uncaught error.
1732 MigrateFromRestoreTask.clearFlags(mContext);
1733 task.execute();
1734 } catch (Exception e) {
1735 Log.e(TAG, "Error during grid migration", e);
1736
1737 // Clear workspace.
1738 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1739 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001740 Log.v(TAG, "Workspace migration completed in "
1741 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001742 }
1743
Dan Sandlerd5024042014-01-09 15:01:33 -05001744 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1745 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1746 LauncherAppState.getLauncherProvider().deleteDatabase();
1747 }
1748
1749 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1750 // append the user's Launcher2 shortcuts
1751 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1752 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1753 } else {
1754 // Make sure the default workspace is loaded
1755 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001756 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001757 }
Adam Cohene25af792013-06-06 23:08:25 -07001758
Winson Chung2abf94d2012-07-18 18:16:38 -07001759 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001760 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001761 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001762 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001763
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001765 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001766 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001767 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001768 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001769
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001770 // +1 for the hotseat (it can be larger than the workspace)
1771 // Load workspace in reverse order to ensure that latest items are loaded first (and
1772 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001773 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 try {
1776 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1777 final int intentIndex = c.getColumnIndexOrThrow
1778 (LauncherSettings.Favorites.INTENT);
1779 final int titleIndex = c.getColumnIndexOrThrow
1780 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 final int containerIndex = c.getColumnIndexOrThrow(
1782 LauncherSettings.Favorites.CONTAINER);
1783 final int itemTypeIndex = c.getColumnIndexOrThrow(
1784 LauncherSettings.Favorites.ITEM_TYPE);
1785 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1786 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001787 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1788 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001789 final int screenIndex = c.getColumnIndexOrThrow(
1790 LauncherSettings.Favorites.SCREEN);
1791 final int cellXIndex = c.getColumnIndexOrThrow
1792 (LauncherSettings.Favorites.CELLX);
1793 final int cellYIndex = c.getColumnIndexOrThrow
1794 (LauncherSettings.Favorites.CELLY);
1795 final int spanXIndex = c.getColumnIndexOrThrow
1796 (LauncherSettings.Favorites.SPANX);
1797 final int spanYIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001799 final int rankIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001801 final int restoredIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001803 final int profileIdIndex = c.getColumnIndexOrThrow(
1804 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001805 final int optionsIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001807 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808
Sunny Goyal7f834d22015-04-21 10:10:23 -07001809 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1810 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1811 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1812 }
1813
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001814 ShortcutInfo info;
1815 String intentDescription;
1816 LauncherAppWidgetInfo appWidgetInfo;
1817 int container;
1818 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001819 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001821 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 while (!mStopped && c.moveToNext()) {
1824 try {
1825 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001826 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001827 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001828 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 switch (itemType) {
1831 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1832 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001833 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001835 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001836 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001837 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001838 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001840 if (user == null) {
1841 // User has been deleted remove the item.
1842 itemsToRemove.add(id);
1843 continue;
1844 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 try {
1846 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001847 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001848 if (cn != null && cn.getPackageName() != null) {
1849 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1850 cn.getPackageName(), user);
1851 boolean validComponent = validPkg &&
1852 launcherApps.isActivityEnabledForProfile(cn, user);
1853
1854 if (validComponent) {
1855 if (restored) {
1856 // no special handling necessary for this item
1857 restoredRows.add(id);
1858 restored = false;
1859 }
1860 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001861 intent = null;
1862 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1863 // We allow auto install apps to have their intent
1864 // updated after an install.
1865 intent = manager.getLaunchIntentForPackage(
1866 cn.getPackageName());
1867 if (intent != null) {
1868 ContentValues values = new ContentValues();
1869 values.put(LauncherSettings.Favorites.INTENT,
1870 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001871 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001872 }
1873 }
1874
1875 if (intent == null) {
1876 // The app is installed but the component is no
1877 // longer available.
1878 Launcher.addDumpLog(TAG,
1879 "Invalid component removed: " + cn, true);
1880 itemsToRemove.add(id);
1881 continue;
1882 } else {
1883 // no special handling necessary for this item
1884 restoredRows.add(id);
1885 restored = false;
1886 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001887 } else if (restored) {
1888 // Package is not yet available but might be
1889 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001890 Launcher.addDumpLog(TAG,
1891 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001892
1893 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1894 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001895 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001896 // App restore has started. Update the flag
1897 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1898 ContentValues values = new ContentValues();
1899 values.put(LauncherSettings.Favorites.RESTORED,
1900 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001901 updateItem(id, values);
1902 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1903 // This is a common app. Try to replace this.
1904 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1905 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1906 if (parser.findDefaultApp()) {
1907 // Default app found. Replace it.
1908 intent = parser.parsedIntent;
1909 cn = intent.getComponent();
1910 ContentValues values = parser.parsedValues;
1911 values.put(LauncherSettings.Favorites.RESTORED, 0);
1912 updateItem(id, values);
1913 restored = false;
1914 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001915
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001916 } else if (REMOVE_UNRESTORED_ICONS) {
1917 Launcher.addDumpLog(TAG,
1918 "Unrestored package removed: " + cn, true);
1919 itemsToRemove.add(id);
1920 continue;
1921 }
Sunny Goyal94485362014-09-18 16:13:58 -07001922 } else if (REMOVE_UNRESTORED_ICONS) {
1923 Launcher.addDumpLog(TAG,
1924 "Unrestored package removed: " + cn, true);
1925 itemsToRemove.add(id);
1926 continue;
1927 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001928 } else if (launcherApps.isAppEnabled(
1929 manager, cn.getPackageName(),
1930 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1931 // Package is present but not available.
1932 allowMissingTarget = true;
1933 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1934 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001935 // SdCard is not ready yet. Package might get available,
1936 // once it is ready.
1937 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1938 + " (check again later)", true);
1939 HashSet<String> pkgs = sPendingPackages.get(user);
1940 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001941 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001942 sPendingPackages.put(user, pkgs);
1943 }
1944 pkgs.add(cn.getPackageName());
1945 allowMissingTarget = true;
1946 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001947
1948 } else {
1949 // Do not wait for external media load anymore.
1950 // Log the invalid package, and remove it
1951 Launcher.addDumpLog(TAG,
1952 "Invalid package removed: " + cn, true);
1953 itemsToRemove.add(id);
1954 continue;
Winson Chungee055712013-07-30 14:46:24 -07001955 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001956 } else if (cn == null) {
1957 // For shortcuts with no component, keep them as they are
1958 restoredRows.add(id);
1959 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001960 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001962 Launcher.addDumpLog(TAG,
1963 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001964 continue;
1965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001966
Sunny Goyal34b65272015-03-11 16:56:52 -07001967 boolean useLowResIcon = container >= 0 &&
1968 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1969
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001970 if (itemReplaced) {
1971 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001972 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001973 cursorIconInfo.iconIndex, titleIndex,
1974 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001975 } else {
1976 // Don't replace items for other profiles.
1977 itemsToRemove.add(id);
1978 continue;
1979 }
1980 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001981 if (user.equals(UserHandleCompat.myUserHandle())) {
1982 Launcher.addDumpLog(TAG,
1983 "constructing info for partially restored package",
1984 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001985 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001986 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001987 intent = getRestoredItemIntent(c, context, intent);
1988 } else {
1989 // Don't restore items for other profiles.
1990 itemsToRemove.add(id);
1991 continue;
1992 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001993 } else if (itemType ==
1994 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001995 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001996 cursorIconInfo.iconIndex, titleIndex,
1997 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001999 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 // App shortcuts that used to be automatically added to Launcher
2002 // didn't always have the correct intent flags set, so do that
2003 // here
2004 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002005 intent.getCategories() != null &&
2006 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002007 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 intent.addFlags(
2009 Intent.FLAG_ACTIVITY_NEW_TASK |
2010 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2011 }
Michael Jurka96879562012-03-22 05:54:33 -07002012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002015 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002018 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 info.cellX = c.getInt(cellXIndex);
2020 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002021 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002022 info.spanX = 1;
2023 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002024 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002025 if (info.promisedIntent != null) {
2026 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2027 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002028 info.isDisabled = disabledState;
2029 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2030 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2031 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002032
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002034 if (!checkItemPlacement(occupied, info)) {
2035 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 break;
2037 }
2038
Sunny Goyal756adbc2015-04-16 15:20:51 -07002039 if (restored) {
2040 ComponentName cn = info.getTargetComponent();
2041 if (cn != null) {
2042 Integer progress = installingPkgs.get(cn.getPackageName());
2043 if (progress != null) {
2044 info.setInstallProgress(progress);
2045 } else {
2046 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2047 }
2048 }
2049 }
2050
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 switch (container) {
2052 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2053 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2054 sBgWorkspaceItems.add(info);
2055 break;
2056 default:
2057 // Item is in a user folder
2058 FolderInfo folderInfo =
2059 findOrMakeFolder(sBgFolders, container);
2060 folderInfo.add(info);
2061 break;
2062 }
2063 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002064 } else {
2065 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 }
2067 break;
2068
2069 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2070 id = c.getLong(idIndex);
2071 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2072
Sunny Goyala508e4f2015-05-21 09:33:57 -07002073 // Do not trim the folder label, as is was set by the user.
2074 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002077 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 folderInfo.cellX = c.getInt(cellXIndex);
2079 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002080 folderInfo.spanX = 1;
2081 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002082 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083
Daniel Sandler8802e962010-05-26 16:28:16 -04002084 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002085 if (!checkItemPlacement(occupied, folderInfo)) {
2086 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002087 break;
2088 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002089
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2092 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2093 sBgWorkspaceItems.add(folderInfo);
2094 break;
Joe Onorato36115782010-06-17 13:28:48 -04002095 }
Joe Onorato17a89222011-02-08 17:26:11 -08002096
Chris Wrenf4d08112014-01-16 18:13:56 -05002097 if (restored) {
2098 // no special handling required for restored folders
2099 restoredRows.add(id);
2100 }
2101
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2103 sBgFolders.put(folderInfo.id, folderInfo);
2104 break;
2105
2106 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002107 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002109 boolean customWidget = itemType ==
2110 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2111
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002113 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002114 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002115 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002116 user = allUsers.get(serialNumber);
2117 if (user == null) {
2118 itemsToRemove.add(id);
2119 continue;
2120 }
2121
Sunny Goyalff572272014-07-23 13:58:07 -07002122 final ComponentName component =
2123 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002124
Sunny Goyal651077b2014-06-30 14:15:31 -07002125 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002126 final boolean isIdValid = (restoreStatus &
2127 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002128 final boolean wasProviderReady = (restoreStatus &
2129 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002130
Adam Cohen59400422014-03-05 18:07:04 -08002131 final LauncherAppWidgetProviderInfo provider =
2132 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002133 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002134 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002135
2136 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002137 if (!isSafeMode && !customWidget &&
2138 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002139 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002140 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002141
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002143 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 itemsToRemove.add(id);
2145 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002146 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002147 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2148 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002149
Sunny Goyal53f96722015-07-13 19:54:53 -07002150 // The provider is available. So the widget is either
2151 // available or not available. We do not need to track
2152 // any future restore updates.
2153 int status = restoreStatus &
2154 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002155 if (!wasProviderReady) {
2156 // If provider was not previously ready, update the
2157 // status and UI flag.
2158
2159 // Id would be valid only if the widget restore broadcast was received.
2160 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002161 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002162 } else {
2163 status &= ~LauncherAppWidgetInfo
2164 .FLAG_PROVIDER_NOT_READY;
2165 }
2166 }
2167 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002168 } else {
2169 Log.v(TAG, "Widget restore pending id=" + id
2170 + " appWidgetId=" + appWidgetId
2171 + " status =" + restoreStatus);
2172 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002173 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002174 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002175 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002176
2177 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2178 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002179 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002180 // App restore has started. Update the flag
2181 appWidgetInfo.restoreStatus |=
2182 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002183 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002184 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002185 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002186 itemsToRemove.add(id);
2187 continue;
2188 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002189
2190 appWidgetInfo.installProgress =
2191 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002192 }
Sunny Goyalff572272014-07-23 13:58:07 -07002193
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002195 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 appWidgetInfo.cellX = c.getInt(cellXIndex);
2197 appWidgetInfo.cellY = c.getInt(cellYIndex);
2198 appWidgetInfo.spanX = c.getInt(spanXIndex);
2199 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002200 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2203 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2204 Log.e(TAG, "Widget found where container != " +
2205 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2206 continue;
2207 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002209 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002211 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2212 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 break;
2214 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002215
Adam Cohen59400422014-03-05 18:07:04 -08002216 if (!customWidget) {
2217 String providerName =
2218 appWidgetInfo.providerName.flattenToString();
2219 if (!providerName.equals(savedProvider) ||
2220 (appWidgetInfo.restoreStatus != restoreStatus)) {
2221 ContentValues values = new ContentValues();
2222 values.put(
2223 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2224 providerName);
2225 values.put(LauncherSettings.Favorites.RESTORED,
2226 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002227 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002228 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002229 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2231 sBgAppWidgets.add(appWidgetInfo);
2232 }
Joe Onorato36115782010-06-17 13:28:48 -04002233 break;
2234 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002236 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002237 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 }
2239 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002240 if (c != null) {
2241 c.close();
2242 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 }
2244
Winson Chungba9c37f2013-08-30 14:11:37 -07002245 // Break early if we've stopped loading
2246 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002247 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002248 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002249 }
2250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002253 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2254 Utilities.createDbSelectionQuery(
2255 LauncherSettings.Favorites._ID, itemsToRemove), null);
2256 if (DEBUG_LOADERS) {
2257 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2258 LauncherSettings.Favorites._ID, itemsToRemove));
2259 }
2260
2261 // Remove any empty folder
2262 for (long folderId : LauncherAppState.getLauncherProvider()
2263 .deleteEmptyFolders()) {
2264 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2265 sBgFolders.remove(folderId);
2266 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002267 }
2268 }
2269
Sunny Goyal317698b2015-07-29 11:45:41 -07002270 // Sort all the folder items and make sure the first 3 items are high resolution.
2271 for (FolderInfo folder : sBgFolders) {
2272 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2273 int pos = 0;
2274 for (ShortcutInfo info : folder.contents) {
2275 if (info.usingLowResIcon) {
2276 info.updateIcon(mIconCache, false);
2277 }
2278 pos ++;
2279 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2280 break;
2281 }
2282 }
2283 }
2284
Chris Wrenf4d08112014-01-16 18:13:56 -05002285 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002286 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002287 ContentValues values = new ContentValues();
2288 values.put(LauncherSettings.Favorites.RESTORED, 0);
2289 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2290 Utilities.createDbSelectionQuery(
2291 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002292 }
2293
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002294 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2295 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002296 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002297 null, sWorker);
2298 }
2299
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002300 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002301
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002302 // Remove any empty screens
2303 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002304 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002305 long screenId = item.screenId;
2306 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2307 unusedScreens.contains(screenId)) {
2308 unusedScreens.remove(screenId);
2309 }
2310 }
2311
2312 // If there are any empty screens remove them, and update.
2313 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002314 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 }
2317
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002318 if (DEBUG_LOADERS) {
2319 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2320 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002322 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002324
Sunny Goyale2df0622015-04-24 11:27:00 -07002325 for (int i = 0; i < nScreens; i++) {
2326 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002327 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 line += " | ";
2329 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002330 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002331 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002332 if (x < screen.length && y < screen[x].length) {
2333 line += (screen[x][y] != null) ? "#" : ".";
2334 } else {
2335 line += "!";
2336 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 }
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002341 }
Joe Onorato36115782010-06-17 13:28:48 -04002342 }
Adam Cohene25af792013-06-06 23:08:25 -07002343 }
2344
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002345 /**
2346 * Partially updates the item without any notification. Must be called on the worker thread.
2347 */
2348 private void updateItem(long itemId, ContentValues update) {
2349 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002350 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002351 update,
2352 BaseColumns._ID + "= ?",
2353 new String[]{Long.toString(itemId)});
2354 }
2355
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 /** Filters the set of items who are directly or indirectly (via another container) on the
2357 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002358 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 ArrayList<ItemInfo> allWorkspaceItems,
2360 ArrayList<ItemInfo> currentScreenItems,
2361 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002362 // Purge any null ItemInfos
2363 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2364 while (iter.hasNext()) {
2365 ItemInfo i = iter.next();
2366 if (i == null) {
2367 iter.remove();
2368 }
2369 }
2370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 // Order the set of items by their containers first, this allows use to walk through the
2372 // list sequentially, build up a list of containers that are in the specified screen,
2373 // as well as all items in those containers.
2374 Set<Long> itemsOnScreen = new HashSet<Long>();
2375 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2376 @Override
2377 public int compare(ItemInfo lhs, ItemInfo rhs) {
2378 return (int) (lhs.container - rhs.container);
2379 }
2380 });
2381 for (ItemInfo info : allWorkspaceItems) {
2382 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002383 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 currentScreenItems.add(info);
2385 itemsOnScreen.add(info.id);
2386 } else {
2387 otherScreenItems.add(info);
2388 }
2389 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2390 currentScreenItems.add(info);
2391 itemsOnScreen.add(info.id);
2392 } else {
2393 if (itemsOnScreen.contains(info.container)) {
2394 currentScreenItems.add(info);
2395 itemsOnScreen.add(info.id);
2396 } else {
2397 otherScreenItems.add(info);
2398 }
2399 }
2400 }
2401 }
2402
2403 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 ArrayList<LauncherAppWidgetInfo> appWidgets,
2406 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2407 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408
2409 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002410 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002412 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 currentScreenWidgets.add(widget);
2414 } else {
2415 otherScreenWidgets.add(widget);
2416 }
2417 }
2418 }
2419
2420 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002421 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002422 LongArrayMap<ItemInfo> itemsIdMap,
2423 LongArrayMap<FolderInfo> folders,
2424 LongArrayMap<FolderInfo> currentScreenFolders,
2425 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426
Sunny Goyale2df0622015-04-24 11:27:00 -07002427 int total = folders.size();
2428 for (int i = 0; i < total; i++) {
2429 long id = folders.keyAt(i);
2430 FolderInfo folder = folders.valueAt(i);
2431
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002432 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002433 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002434 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002435 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 currentScreenFolders.put(id, folder);
2437 } else {
2438 otherScreenFolders.put(id, folder);
2439 }
2440 }
2441 }
2442
2443 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2444 * right) */
2445 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002446 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002447 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002448 final int screenCols = profile.numColumns;
2449 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002451 @Override
2452 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002453 if (lhs.container == rhs.container) {
2454 // Within containers, order by their spatial position in that container
2455 switch ((int) lhs.container) {
2456 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2457 long lr = (lhs.screenId * screenCellCount +
2458 lhs.cellY * screenCols + lhs.cellX);
2459 long rr = (rhs.screenId * screenCellCount +
2460 rhs.cellY * screenCols + rhs.cellX);
2461 return (int) (lr - rr);
2462 }
2463 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2464 // We currently use the screen id as the rank
2465 return (int) (lhs.screenId - rhs.screenId);
2466 }
2467 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002468 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002469 throw new RuntimeException("Unexpected container type when " +
2470 "sorting workspace items.");
2471 }
2472 return 0;
2473 }
2474 } else {
2475 // Between containers, order by hotseat, desktop
2476 return (int) (lhs.container - rhs.container);
2477 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002478 }
2479 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002481
Adam Cohendcd297f2013-06-18 13:13:40 -07002482 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2483 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002484 final Runnable r = new Runnable() {
2485 @Override
2486 public void run() {
2487 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2488 if (callbacks != null) {
2489 callbacks.bindScreens(orderedScreens);
2490 }
2491 }
2492 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002493 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002494 }
2495
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2497 final ArrayList<ItemInfo> workspaceItems,
2498 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002499 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002501
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 final boolean postOnMainThread = (deferredBindRunnables != null);
2503
2504 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002505 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002507 final int start = i;
2508 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 final Runnable r = new Runnable() {
2510 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002512 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002514 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2515 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 }
2517 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 };
2519 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002520 synchronized (deferredBindRunnables) {
2521 deferredBindRunnables.add(r);
2522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002524 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 }
Joe Onorato36115782010-06-17 13:28:48 -04002526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527
2528 // Bind the folders
2529 if (!folders.isEmpty()) {
2530 final Runnable r = new Runnable() {
2531 public void run() {
2532 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2533 if (callbacks != null) {
2534 callbacks.bindFolders(folders);
2535 }
2536 }
2537 };
2538 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002539 synchronized (deferredBindRunnables) {
2540 deferredBindRunnables.add(r);
2541 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002543 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 }
2545 }
2546
2547 // Bind the widgets, one at a time
2548 N = appWidgets.size();
2549 for (int i = 0; i < N; i++) {
2550 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2551 final Runnable r = new Runnable() {
2552 public void run() {
2553 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2554 if (callbacks != null) {
2555 callbacks.bindAppWidget(widget);
2556 }
2557 }
2558 };
2559 if (postOnMainThread) {
2560 deferredBindRunnables.add(r);
2561 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002562 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 }
2564 }
2565 }
2566
2567 /**
2568 * Binds all loaded data to actual views on the main thread.
2569 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002570 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571 final long t = SystemClock.uptimeMillis();
2572 Runnable r;
2573
2574 // Don't use these two variables in any of the callback runnables.
2575 // Otherwise we hold a reference to them.
2576 final Callbacks oldCallbacks = mCallbacks.get();
2577 if (oldCallbacks == null) {
2578 // This launcher has exited and nobody bothered to tell us. Just bail.
2579 Log.w(TAG, "LoaderTask running with no launcher");
2580 return;
2581 }
2582
Winson Chung9b9fb962013-11-15 15:39:34 -08002583 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002584 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2585 ArrayList<LauncherAppWidgetInfo> appWidgets =
2586 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002587 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002588
2589 final LongArrayMap<FolderInfo> folders;
2590 final LongArrayMap<ItemInfo> itemsIdMap;
2591
Winson Chung2abf94d2012-07-18 18:16:38 -07002592 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593 workspaceItems.addAll(sBgWorkspaceItems);
2594 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002595 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002596
2597 folders = sBgFolders.clone();
2598 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002599 }
2600
Derek Prothro7aff3992013-12-10 14:00:37 -05002601 final boolean isLoadingSynchronously =
2602 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002603 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002604 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002605 if (currScreen >= orderedScreenIds.size()) {
2606 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002607 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002608 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002609 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002610 final long currentScreenId = currentScreen < 0
2611 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002612
2613 // Load all the items that are on the current page first (and in the process, unbind
2614 // all the existing workspace items before we call startBinding() below.
2615 unbindWorkspaceItemsOnMainThread();
2616
2617 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002618 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2619 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2620 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2621 new ArrayList<LauncherAppWidgetInfo>();
2622 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2623 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002624 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2625 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002626
Winson Chung9b9fb962013-11-15 15:39:34 -08002627 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002629 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002631 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 otherFolders);
2633 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2634 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2635
2636 // Tell the workspace that we're about to start binding items
2637 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002638 public void run() {
2639 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2640 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002642 }
2643 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002644 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002645 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002646
Adam Cohendcd297f2013-06-18 13:13:40 -07002647 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2648
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002649 // Load items on the current page
2650 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2651 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002652 if (isLoadingSynchronously) {
2653 r = new Runnable() {
2654 public void run() {
2655 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002656 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002657 callbacks.onPageBoundSynchronously(currentScreen);
2658 }
2659 }
2660 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002661 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002662 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663
Winson Chung4a2afa32012-07-19 14:53:05 -07002664 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2665 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002666 synchronized (mDeferredBindRunnables) {
2667 mDeferredBindRunnables.clear();
2668 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002669 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002670 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002671
2672 // Tell the workspace that we're done binding items
2673 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002674 public void run() {
2675 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2676 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002677 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002679
Sunny Goyal639e9062015-08-19 19:17:06 -07002680 mIsLoadingAndBindingWorkspace = false;
2681
2682 // Run all the bind complete runnables after workspace is bound.
2683 if (!mBindCompleteRunnables.isEmpty()) {
2684 synchronized (mBindCompleteRunnables) {
2685 for (final Runnable r : mBindCompleteRunnables) {
2686 runOnWorkerThread(r);
2687 }
2688 mBindCompleteRunnables.clear();
2689 }
2690 }
2691
Winson Chung98e030b2012-05-07 16:01:11 -07002692 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002693 if (DEBUG_LOADERS) {
2694 Log.d(TAG, "bound workspace in "
2695 + (SystemClock.uptimeMillis()-t) + "ms");
2696 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002697
Joe Onorato36115782010-06-17 13:28:48 -04002698 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002699 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002700 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002701 synchronized (mDeferredBindRunnables) {
2702 mDeferredBindRunnables.add(r);
2703 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002704 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002705 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002706 }
Joe Onorato36115782010-06-17 13:28:48 -04002707 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002708
Joe Onorato36115782010-06-17 13:28:48 -04002709 private void loadAndBindAllApps() {
2710 if (DEBUG_LOADERS) {
2711 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2712 }
2713 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002714 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002715 synchronized (LoaderTask.this) {
2716 if (mStopped) {
2717 return;
2718 }
2719 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002720 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002721 synchronized (LoaderTask.this) {
2722 if (mStopped) {
2723 return;
2724 }
2725 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002726 }
Joe Onorato36115782010-06-17 13:28:48 -04002727 } else {
2728 onlyBindAllApps();
2729 }
2730 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002731
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002732 private void updateIconCache() {
2733 // Ignore packages which have a promise icon.
2734 HashSet<String> packagesToIgnore = new HashSet<>();
2735 synchronized (sBgLock) {
2736 for (ItemInfo info : sBgItemsIdMap) {
2737 if (info instanceof ShortcutInfo) {
2738 ShortcutInfo si = (ShortcutInfo) info;
2739 if (si.isPromise() && si.getTargetComponent() != null) {
2740 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2741 }
2742 } else if (info instanceof LauncherAppWidgetInfo) {
2743 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2744 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2745 packagesToIgnore.add(lawi.providerName.getPackageName());
2746 }
2747 }
2748 }
2749 }
2750 mIconCache.updateDbIcons(packagesToIgnore);
2751 }
2752
Joe Onorato36115782010-06-17 13:28:48 -04002753 private void onlyBindAllApps() {
2754 final Callbacks oldCallbacks = mCallbacks.get();
2755 if (oldCallbacks == null) {
2756 // This launcher has exited and nobody bothered to tell us. Just bail.
2757 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2758 return;
2759 }
2760
2761 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002762 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002763 final ArrayList<AppInfo> list
2764 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002765 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002766 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002767 public void run() {
2768 final long t = SystemClock.uptimeMillis();
2769 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2770 if (callbacks != null) {
2771 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002772 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002773 }
2774 if (DEBUG_LOADERS) {
2775 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2776 + (SystemClock.uptimeMillis()-t) + "ms");
2777 }
2778 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002779 };
2780 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002781 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002782 r.run();
2783 } else {
2784 mHandler.post(r);
2785 }
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
2787
Winson Chung64359a52013-07-08 17:17:08 -07002788 private void loadAllApps() {
2789 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002790
Joe Onorato36115782010-06-17 13:28:48 -04002791 final Callbacks oldCallbacks = mCallbacks.get();
2792 if (oldCallbacks == null) {
2793 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002794 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002795 return;
2796 }
2797
Kenny Guyed131872014-04-30 03:02:21 +01002798 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2799
Winson Chung64359a52013-07-08 17:17:08 -07002800 // Clear the list of apps
2801 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002802 for (UserHandleCompat user : profiles) {
2803 // Query for the set of apps
2804 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002805 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002806 if (DEBUG_LOADERS) {
2807 Log.d(TAG, "getActivityList took "
2808 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2809 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2810 }
2811 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002812 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002813 if (apps == null || apps.isEmpty()) {
2814 return;
2815 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002816
Kenny Guyed131872014-04-30 03:02:21 +01002817 // Create the ApplicationInfos
2818 for (int i = 0; i < apps.size(); i++) {
2819 LauncherActivityInfoCompat app = apps.get(i);
2820 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002821 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002822 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002823
Sunny Goyal756a28a2015-04-23 17:07:55 -07002824 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2825 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002826 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002827
2828 @Override
2829 public void run() {
2830 heuristic.processUserApps(apps);
2831 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002832 };
2833 runOnMainThread(new Runnable() {
2834
2835 @Override
2836 public void run() {
2837 // Check isLoadingWorkspace on the UI thread, as it is updated on
2838 // the UI thread.
2839 if (mIsLoadingAndBindingWorkspace) {
2840 synchronized (mBindCompleteRunnables) {
2841 mBindCompleteRunnables.add(r);
2842 }
2843 } else {
2844 runOnWorkerThread(r);
2845 }
2846 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002847 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002848 }
Winson Chung64359a52013-07-08 17:17:08 -07002849 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002850 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002851 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2852 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002853
2854 // Post callback on main thread
2855 mHandler.post(new Runnable() {
2856 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002857
Winson Chung64359a52013-07-08 17:17:08 -07002858 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002859 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002860 if (callbacks != null) {
2861 callbacks.bindAllApplications(added);
2862 if (DEBUG_LOADERS) {
2863 Log.d(TAG, "bound " + added.size() + " apps in "
2864 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2865 }
2866 } else {
2867 Log.i(TAG, "not binding apps: no Launcher activity");
2868 }
2869 }
2870 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002871 // Cleanup any data stored for a deleted user.
2872 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002873
Sunny Goyal2d648b02015-08-11 13:56:28 -07002874 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002875 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002876 Log.d(TAG, "Icons processed in "
2877 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002878 }
2879 }
2880
2881 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002882 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002883 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002884 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2885 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2886 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2887 }
Joe Onorato36115782010-06-17 13:28:48 -04002888 }
2889 }
2890
Sunny Goyal75b0f552015-05-20 21:57:06 -07002891 /**
2892 * Called when the icons for packages have been updated in the icon cache.
2893 */
2894 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2895 final Callbacks callbacks = getCallback();
2896 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2897 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2898
2899 // If any package icon has changed (app was updated while launcher was dead),
2900 // update the corresponding shortcuts.
2901 synchronized (sBgLock) {
2902 for (ItemInfo info : sBgItemsIdMap) {
2903 if (info instanceof ShortcutInfo && user.equals(info.user)
2904 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2905 ShortcutInfo si = (ShortcutInfo) info;
2906 ComponentName cn = si.getTargetComponent();
2907 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2908 si.updateIcon(mIconCache);
2909 updatedShortcuts.add(si);
2910 }
2911 }
2912 }
2913 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2914 }
2915
2916 if (!updatedShortcuts.isEmpty()) {
2917 final UserHandleCompat userFinal = user;
2918 mHandler.post(new Runnable() {
2919
2920 public void run() {
2921 Callbacks cb = getCallback();
2922 if (cb != null && callbacks == cb) {
2923 cb.bindShortcutsChanged(updatedShortcuts,
2924 new ArrayList<ShortcutInfo>(), userFinal);
2925 }
2926 }
2927 });
2928 }
2929
2930 if (!updatedApps.isEmpty()) {
2931 mHandler.post(new Runnable() {
2932
2933 public void run() {
2934 Callbacks cb = getCallback();
2935 if (cb != null && callbacks == cb) {
2936 cb.bindAppsUpdated(updatedApps);
2937 }
2938 }
2939 });
2940 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002941
2942 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002943 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002944 }
2945
Joe Onorato36115782010-06-17 13:28:48 -04002946 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002947 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002948 }
2949
Adam Cohen091440a2015-03-18 14:16:05 -07002950 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002951
2952 @Override
2953 public void onReceive(Context context, Intent intent) {
2954 synchronized (sBgLock) {
2955 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2956 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002957 final PackageManager manager = context.getPackageManager();
2958 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2959 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002960 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2961 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002962 packagesRemoved.clear();
2963 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002964 for (String pkg : entry.getValue()) {
2965 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002966 boolean packageOnSdcard = launcherApps.isAppEnabled(
2967 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2968 if (packageOnSdcard) {
2969 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2970 packagesUnavailable.add(pkg);
2971 } else {
2972 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2973 packagesRemoved.add(pkg);
2974 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002975 }
2976 }
2977 if (!packagesRemoved.isEmpty()) {
2978 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2979 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2980 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002981 if (!packagesUnavailable.isEmpty()) {
2982 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2983 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2984 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002985 }
Sunny Goyal34942622014-08-29 17:20:55 -07002986 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002987 }
2988 }
2989 }
2990
Joe Onorato36115782010-06-17 13:28:48 -04002991 private class PackageUpdatedTask implements Runnable {
2992 int mOp;
2993 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002994 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002995
2996 public static final int OP_NONE = 0;
2997 public static final int OP_ADD = 1;
2998 public static final int OP_UPDATE = 2;
2999 public static final int OP_REMOVE = 3; // uninstlled
3000 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3001
3002
Kenny Guyed131872014-04-30 03:02:21 +01003003 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003004 mOp = op;
3005 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003006 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003007 }
3008
3009 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003010 if (!mHasLoaderCompletedOnce) {
3011 // Loader has not yet run.
3012 return;
3013 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003014 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003015
3016 final String[] packages = mPackages;
3017 final int N = packages.length;
3018 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003019 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003020 for (int i=0; i<N; i++) {
3021 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003022 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003023 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003024 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003025
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003026 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3027 if (heuristic != null) {
3028 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003029 }
Joe Onorato36115782010-06-17 13:28:48 -04003030 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003031 }
Joe Onorato36115782010-06-17 13:28:48 -04003032 case OP_UPDATE:
3033 for (int i=0; i<N; i++) {
3034 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003035 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003036 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003037 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003038 }
3039 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003040 case OP_REMOVE: {
3041 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3042 if (heuristic != null) {
3043 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003044 }
Joe Onorato36115782010-06-17 13:28:48 -04003045 for (int i=0; i<N; i++) {
3046 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003047 mIconCache.removeIconsForPkg(packages[i], mUser);
3048 }
3049 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003050 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003051 case OP_UNAVAILABLE:
3052 for (int i=0; i<N; i++) {
3053 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3054 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003055 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003056 }
3057 break;
3058 }
3059
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003060 ArrayList<AppInfo> added = null;
3061 ArrayList<AppInfo> modified = null;
3062 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003063
Adam Cohen487f7dd2012-06-28 18:12:10 -07003064 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003066 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003067 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003068 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003069 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003070 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003071 }
Winson Chung5d55f332012-07-16 20:45:03 -07003072 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003073 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003074 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003075 }
3076
Sunny Goyale0f58d72014-11-10 18:05:31 -08003077 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003078 if (callbacks == null) {
3079 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3080 return;
3081 }
3082
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3084 new HashMap<ComponentName, AppInfo>();
3085
Joe Onorato36115782010-06-17 13:28:48 -04003086 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003087 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088 for (AppInfo ai : added) {
3089 addedOrUpdatedApps.put(ai.componentName, ai);
3090 }
Joe Onorato36115782010-06-17 13:28:48 -04003091 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003092
Joe Onorato36115782010-06-17 13:28:48 -04003093 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003094 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 for (AppInfo ai : modified) {
3096 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003097 }
3098
Joe Onorato36115782010-06-17 13:28:48 -04003099 mHandler.post(new Runnable() {
3100 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003101 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003102 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003103 callbacks.bindAppsUpdated(modifiedFinal);
3104 }
3105 }
3106 });
3107 }
Winson Chung83892cc2013-05-01 16:53:33 -07003108
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003110 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3113 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3114
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003116 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003117 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003118 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3119 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 boolean infoUpdated = false;
3121 boolean shortcutUpdated = false;
3122
3123 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003124 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003126 Bitmap icon = Utilities.createIconBitmap(
3127 si.iconResource.packageName,
3128 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 if (icon != null) {
3130 si.setIcon(icon);
3131 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003133 }
3134 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135
3136 ComponentName cn = si.getTargetComponent();
3137 if (cn != null && packageSet.contains(cn.getPackageName())) {
3138 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3139
3140 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003141 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3142 // Auto install icon
3143 PackageManager pm = context.getPackageManager();
3144 ResolveInfo matched = pm.resolveActivity(
3145 new Intent(Intent.ACTION_MAIN)
3146 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3147 PackageManager.MATCH_DEFAULT_ONLY);
3148 if (matched == null) {
3149 // Try to find the best match activity.
3150 Intent intent = pm.getLaunchIntentForPackage(
3151 cn.getPackageName());
3152 if (intent != null) {
3153 cn = intent.getComponent();
3154 appInfo = addedOrUpdatedApps.get(cn);
3155 }
3156
3157 if ((intent == null) || (appInfo == null)) {
3158 removedShortcuts.add(si);
3159 continue;
3160 }
3161 si.promisedIntent = intent;
3162 }
3163 }
3164
3165 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003166 if (appInfo != null) {
3167 si.flags = appInfo.flags;
3168 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169
Sunny Goyal756adbc2015-04-16 15:20:51 -07003170 si.intent = si.promisedIntent;
3171 si.promisedIntent = null;
3172 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003173 infoUpdated = true;
3174 si.updateIcon(mIconCache);
3175 }
3176
3177 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3178 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3179 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003180 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003181 si.contentDescription = appInfo.contentDescription;
3182 infoUpdated = true;
3183 }
3184
3185 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3186 // Since package was just updated, the target must be available now.
3187 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3188 shortcutUpdated = true;
3189 }
3190 }
3191
3192 if (infoUpdated || shortcutUpdated) {
3193 updatedShortcuts.add(si);
3194 }
3195 if (infoUpdated) {
3196 updateItemInDatabase(context, si);
3197 }
3198 } else if (info instanceof LauncherAppWidgetInfo) {
3199 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3200 if (mUser.equals(widgetInfo.user)
3201 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3202 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003203 widgetInfo.restoreStatus &=
3204 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3205 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003206
3207 // adding this flag ensures that launcher shows 'click to setup'
3208 // if the widget has a config activity. In case there is no config
3209 // activity, it will be marked as 'restored' during bind.
3210 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3211
Sunny Goyal4390ace2014-10-13 11:33:11 -07003212 widgets.add(widgetInfo);
3213 updateItemInDatabase(context, widgetInfo);
3214 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003215 }
3216 }
3217 }
3218
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3220 mHandler.post(new Runnable() {
3221
3222 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003224 if (callbacks == cb && cb != null) {
3225 callbacks.bindShortcutsChanged(
3226 updatedShortcuts, removedShortcuts, mUser);
3227 }
3228 }
3229 });
3230 if (!removedShortcuts.isEmpty()) {
3231 deleteItemsFromDatabase(context, removedShortcuts);
3232 }
3233 }
3234 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003235 mHandler.post(new Runnable() {
3236 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003237 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003238 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003239 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003240 }
3241 }
3242 });
3243 }
3244 }
3245
Winson Chungdf95eb12013-10-16 14:57:07 -07003246 final ArrayList<String> removedPackageNames =
3247 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003248 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003249 // Mark all packages in the broadcast to be removed
3250 removedPackageNames.addAll(Arrays.asList(packages));
3251 } else if (mOp == OP_UPDATE) {
3252 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003254 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003255 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003256 }
3257 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003258 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003259
Winson Chungdf95eb12013-10-16 14:57:07 -07003260 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003261 final int removeReason;
3262 if (mOp == OP_UNAVAILABLE) {
3263 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3264 } else {
3265 // Remove all the components associated with this package
3266 for (String pn : removedPackageNames) {
3267 deletePackageFromDatabase(context, pn, mUser);
3268 }
3269 // Remove all the specific components
3270 for (AppInfo a : removedApps) {
3271 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3272 deleteItemsFromDatabase(context, infos);
3273 }
3274 removeReason = 0;
3275 }
3276
Winson Chungdf95eb12013-10-16 14:57:07 -07003277 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003278 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003279 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003280 mHandler.post(new Runnable() {
3281 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003282 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003283 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003284 callbacks.bindComponentsRemoved(
3285 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003286 }
3287 }
3288 });
Joe Onoratobe386092009-11-17 17:32:16 -08003289 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003290
Sunny Goyal2d648b02015-08-11 13:56:28 -07003291 // Update widgets
3292 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3293 // Always refresh for a package event on secondary user
3294 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3295
3296 // Refresh widget list, if the package already had a widget.
3297 synchronized (sBgLock) {
3298 if (sBgWidgetProviders != null) {
3299 HashSet<String> pkgSet = new HashSet<>();
3300 Collections.addAll(pkgSet, mPackages);
3301
3302 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3303 needToRefresh |= key.user.equals(mUser) &&
3304 pkgSet.contains(key.componentName.getPackageName());
3305 }
3306 }
3307 }
3308
3309 if (!needToRefresh && mOp != OP_REMOVE) {
3310 // Refresh widget list, if there is any newly added widget
3311 PackageManager pm = context.getPackageManager();
3312 for (String pkg : mPackages) {
3313 needToRefresh |= !pm.queryBroadcastReceivers(
3314 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
3315 .setPackage(pkg), 0).isEmpty();
3316 }
3317 }
3318
3319 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3320 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003321
Adam Cohen4caf2982013-08-20 18:54:31 -07003322 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003323 mHandler.post(new Runnable() {
3324 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003325 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003326 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003327 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003328 }
3329 }
3330 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 }
3332 }
3333
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003334 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3335 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003336 ArrayList<LauncherAppWidgetProviderInfo> results =
3337 new ArrayList<LauncherAppWidgetProviderInfo>();
3338 try {
3339 synchronized (sBgLock) {
3340 if (sBgWidgetProviders == null || refresh) {
3341 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3342 = new HashMap<>();
3343 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3344 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003345
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003346 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3347 for (AppWidgetProviderInfo pInfo : widgets) {
3348 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3349 UserHandleCompat user = wm.getUser(info);
3350 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3351 }
Robin Lee26ace122015-03-16 19:41:43 +00003352
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003353 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3354 for (CustomAppWidget widget : customWidgets) {
3355 info = new LauncherAppWidgetProviderInfo(context, widget);
3356 UserHandleCompat user = wm.getUser(info);
3357 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3358 }
3359 // Replace the global list at the very end, so that if there is an exception,
3360 // previously loaded provider list is used.
3361 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003362 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003363 results.addAll(sBgWidgetProviders.values());
3364 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003365 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003366 } catch (Exception e) {
3367 if (e.getCause() instanceof TransactionTooLargeException) {
3368 // the returned value may be incomplete and will not be refreshed until the next
3369 // time Launcher starts.
3370 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3371 // onResume is called to refresh the widget provider list.
3372 synchronized (sBgLock) {
3373 if (sBgWidgetProviders != null) {
3374 results.addAll(sBgWidgetProviders.values());
3375 }
3376 return results;
3377 }
3378 } else {
3379 throw e;
3380 }
Adam Cohen59400422014-03-05 18:07:04 -08003381 }
3382 }
3383
Robin Lee26ace122015-03-16 19:41:43 +00003384 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3385 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003386 synchronized (sBgLock) {
3387 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003388 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003389 }
Robin Lee26ace122015-03-16 19:41:43 +00003390 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003391 }
3392 }
3393
Sunny Goyal2d648b02015-08-11 13:56:28 -07003394 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003395
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003396 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003397 @Override
3398 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003399 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003400 final WidgetsModel model = mBgWidgetsModel.clone();
3401
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003402 mHandler.post(new Runnable() {
3403 @Override
3404 public void run() {
3405 Callbacks cb = getCallback();
3406 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003407 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003408 }
3409 }
3410 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003411 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003412 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3413 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003414 }
3415 });
3416 }
3417
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003418 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003419 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003420 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003421 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003422 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003423 @Thunk void updateWidgetsModel(boolean refresh) {
3424 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003425 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003426 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003427 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3428 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003429 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003430 }
3431
Adam Cohen091440a2015-03-18 14:16:05 -07003432 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003433 UserHandleCompat user) {
3434 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3435 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003436 }
Adam Cohen556f6132014-01-15 15:18:08 -08003437
Kenny Guyed131872014-04-30 03:02:21 +01003438 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3439 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003440 if (cn == null) {
3441 return false;
3442 }
Kenny Guyed131872014-04-30 03:02:21 +01003443 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3444 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003445 return false;
3446 }
Kenny Guyed131872014-04-30 03:02:21 +01003447 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003448 }
3449
Adam Cohena28b78e2014-05-20 17:03:04 -07003450 public static boolean isValidPackage(Context context, String packageName,
3451 UserHandleCompat user) {
3452 if (packageName == null) {
3453 return false;
3454 }
3455 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3456 return launcherApps.isPackageEnabledForProfile(packageName, user);
3457 }
3458
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003460 * Make an ShortcutInfo object for a restored application or shortcut item that points
3461 * to a package that is not yet installed on the system.
3462 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003463 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003464 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003465 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003466 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003467
3468 Bitmap icon = iconInfo.loadIcon(c, info, context);
3469 // the fallback icon
3470 if (icon == null) {
3471 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3472 } else {
3473 info.setIcon(icon);
3474 }
Sunny Goyal34942622014-08-29 17:20:55 -07003475
3476 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003477 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003478 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003479 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003480 }
Sunny Goyal34942622014-08-29 17:20:55 -07003481 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3482 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003483 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003484 }
Sunny Goyal34942622014-08-29 17:20:55 -07003485 } else {
3486 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3487 }
3488
Winson Chung82b016c2015-05-08 17:00:10 -07003489 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003490 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003491 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003492 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003493 return info;
3494 }
3495
3496 /**
3497 * Make an Intent object for a restored application or shortcut item that points
3498 * to the market page for the item.
3499 */
Adam Cohen091440a2015-03-18 14:16:05 -07003500 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003501 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003502 return getMarketIntent(componentName.getPackageName());
3503 }
3504
3505 static Intent getMarketIntent(String packageName) {
3506 return new Intent(Intent.ACTION_VIEW)
3507 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003508 .scheme("market")
3509 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003510 .appendQueryParameter("id", packageName)
3511 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003512 }
3513
3514 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003515 * Make an ShortcutInfo object for a shortcut that is an application.
3516 *
3517 * If c is not null, then it will be used to fill in missing data like the title and icon.
3518 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003519 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003520 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003521 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003522 if (user == null) {
3523 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003524 return null;
3525 }
3526
Kenny Guyed131872014-04-30 03:02:21 +01003527 ComponentName componentName = intent.getComponent();
3528 if (componentName == null) {
3529 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3530 return null;
3531 }
3532
3533 Intent newIntent = new Intent(intent.getAction(), null);
3534 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3535 newIntent.setComponent(componentName);
3536 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003537 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003538 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3539 return null;
3540 }
3541
3542 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003543 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003544 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3545 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3546 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003547 }
3548
Joe Onorato56d82912010-03-07 14:32:10 -05003549 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003550 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003551 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003552 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003553
Joe Onorato56d82912010-03-07 14:32:10 -05003554 // fall back to the class name of the activity
3555 if (info.title == null) {
3556 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003557 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003558
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003560 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003561 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003562 if (lai != null) {
3563 info.flags = AppInfo.initFlags(lai);
3564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003566 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003567
Sunny Goyale2df0622015-04-24 11:27:00 -07003568 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003569 ItemInfoFilter f) {
3570 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3571 for (ItemInfo i : infos) {
3572 if (i instanceof ShortcutInfo) {
3573 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003574 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003575 if (cn != null && f.filterItem(null, info, cn)) {
3576 filtered.add(info);
3577 }
3578 } else if (i instanceof FolderInfo) {
3579 FolderInfo info = (FolderInfo) i;
3580 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003581 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003582 if (cn != null && f.filterItem(info, s, cn)) {
3583 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003584 }
3585 }
Winson Chung64359a52013-07-08 17:17:08 -07003586 } else if (i instanceof LauncherAppWidgetInfo) {
3587 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3588 ComponentName cn = info.providerName;
3589 if (cn != null && f.filterItem(null, info, cn)) {
3590 filtered.add(info);
3591 }
Winson Chung8a435102012-08-30 17:16:53 -07003592 }
3593 }
Winson Chung64359a52013-07-08 17:17:08 -07003594 return new ArrayList<ItemInfo>(filtered);
3595 }
3596
Adam Cohen091440a2015-03-18 14:16:05 -07003597 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003598 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003599 ItemInfoFilter filter = new ItemInfoFilter() {
3600 @Override
3601 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003602 if (info.user == null) {
3603 return cn.equals(cname);
3604 } else {
3605 return cn.equals(cname) && info.user.equals(user);
3606 }
Winson Chung64359a52013-07-08 17:17:08 -07003607 }
3608 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003609 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003610 }
3611
Sunny Goyal1a745e82014-10-02 15:58:31 -07003612 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003613 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003614 */
Adam Cohen091440a2015-03-18 14:16:05 -07003615 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003616 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003617 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003618 // Non-app shortcuts are only supported for current user.
3619 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003621
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003622 // TODO: If there's an explicit component and we can't install that, delete it.
3623
Winson Chung82b016c2015-05-08 17:00:10 -07003624 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003625
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003626 Bitmap icon = iconInfo.loadIcon(c, info, context);
3627 // the fallback icon
3628 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003629 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003630 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003632 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 return info;
3634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003635
Sunny Goyal2350bc92014-10-14 16:42:54 -07003636 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003637 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3638 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3639 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3640
Adam Cohend9198822011-11-22 16:42:47 -08003641 if (intent == null) {
3642 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3643 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3644 return null;
3645 }
3646
Joe Onorato0589f0f2010-02-08 13:44:00 -08003647 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003648 boolean customIcon = false;
3649 ShortcutIconResource iconResource = null;
3650
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003651 if (bitmap instanceof Bitmap) {
3652 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003653 customIcon = true;
3654 } else {
3655 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003656 if (extra instanceof ShortcutIconResource) {
3657 iconResource = (ShortcutIconResource) extra;
3658 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003659 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003660 }
3661 }
3662
Michael Jurkac9d95c52011-08-29 14:03:34 -07003663 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003664
Kenny Guyed131872014-04-30 03:02:21 +01003665 // Only support intents for current user for now. Intents sent from other
3666 // users wouldn't get here without intent forwarding anyway.
3667 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003668 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003669 icon = mIconCache.getDefaultIcon(info.user);
3670 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003671 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003672 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003673
Winson Chung82b016c2015-05-08 17:00:10 -07003674 info.title = Utilities.trim(name);
3675 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003676 info.intent = intent;
3677 info.customIcon = customIcon;
3678 info.iconResource = iconResource;
3679
3680 return info;
3681 }
3682
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003684 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 * or make a new one.
3686 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003687 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 // See if a placeholder was created for us already
3689 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003690 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003692 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 folders.put(id, folderInfo);
3694 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003695 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 }
3697
Joe Onoratobe386092009-11-17 17:32:16 -08003698
Sunny Goyal651077b2014-06-30 14:15:31 -07003699 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3700 return (provider != null) && (provider.provider != null)
3701 && (provider.provider.getPackageName() != null);
3702 }
3703
Joe Onoratobe386092009-11-17 17:32:16 -08003704 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003705 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003706 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3707 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3708 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3709 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003710 if (mLoaderTask != null) {
3711 mLoaderTask.dumpState();
3712 } else {
3713 Log.d(TAG, "mLoaderTask=null");
3714 }
Joe Onoratobe386092009-11-17 17:32:16 -08003715 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003716
3717 public Callbacks getCallback() {
3718 return mCallbacks != null ? mCallbacks.get() : null;
3719 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003720
3721 /**
3722 * @return {@link FolderInfo} if its already loaded.
3723 */
3724 public FolderInfo findFolderById(Long folderId) {
3725 synchronized (sBgLock) {
3726 return sBgFolders.get(folderId);
3727 }
3728 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003729
3730 /**
3731 * @return the looper for the worker thread which can be used to start background tasks.
3732 */
3733 public static Looper getWorkerLooper() {
3734 return sWorkerThread.getLooper();
3735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736}