blob: b390f13ac008449821d6adabdd9da29ca14d6a12 [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. */
262 private static void runOnWorkerThread(Runnable r) {
263 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
Sunny Goyal756a28a2015-04-23 17:07:55 -0700271 /**
272 * Runs the specified runnable after the loader is complete
273 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700274 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700275 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
276 synchronized (mBindCompleteRunnables) {
277 mBindCompleteRunnables.add(r);
278 }
279 } else {
280 runOnWorkerThread(r);
281 }
282 }
283
Winson Chunge43a1e72014-01-15 10:33:02 -0800284 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
285 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800286 }
287
Sunny Goyal756adbc2015-04-16 15:20:51 -0700288 public void setPackageState(final PackageInstallInfo installInfo) {
289 Runnable updateRunnable = new Runnable() {
290
291 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500292 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 synchronized (sBgLock) {
294 final HashSet<ItemInfo> updates = new HashSet<>();
295
296 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
297 // Ignore install success events as they are handled by Package add events.
298 return;
299 }
300
Sunny Goyale2df0622015-04-24 11:27:00 -0700301 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700302 if (info instanceof ShortcutInfo) {
303 ShortcutInfo si = (ShortcutInfo) info;
304 ComponentName cn = si.getTargetComponent();
305 if (si.isPromise() && (cn != null)
306 && installInfo.packageName.equals(cn.getPackageName())) {
307 si.setInstallProgress(installInfo.progress);
308
309 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
310 // Mark this info as broken.
311 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
312 }
313 updates.add(si);
314 }
315 }
316 }
317
318 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
319 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
320 widget.installProgress = installInfo.progress;
321 updates.add(widget);
322 }
323 }
324
325 if (!updates.isEmpty()) {
326 // Push changes to the callback.
327 Runnable r = new Runnable() {
328 public void run() {
329 Callbacks callbacks = getCallback();
330 if (callbacks != null) {
331 callbacks.bindRestoreItemsChange(updates);
332 }
333 }
334 };
335 mHandler.post(r);
336 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500337 }
338 }
339 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500341 }
342
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 /**
344 * Updates the icons and label of all pending icons for the provided package name.
345 */
346 public void updateSessionDisplayInfo(final String packageName) {
347 Runnable updateRunnable = new Runnable() {
348
349 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700350 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700351 synchronized (sBgLock) {
352 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
353 final UserHandleCompat user = UserHandleCompat.myUserHandle();
354
Sunny Goyale2df0622015-04-24 11:27:00 -0700355 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700356 if (info instanceof ShortcutInfo) {
357 ShortcutInfo si = (ShortcutInfo) info;
358 ComponentName cn = si.getTargetComponent();
359 if (si.isPromise() && (cn != null)
360 && packageName.equals(cn.getPackageName())) {
361 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
362 // For auto install apps update the icon as well as label.
363 mIconCache.getTitleAndIcon(si,
364 si.promisedIntent, user,
365 si.shouldUseLowResIcon());
366 } else {
367 // Only update the icon for restored apps.
368 si.updateIcon(mIconCache);
369 }
370 updates.add(si);
371 }
372 }
373 }
374
375 if (!updates.isEmpty()) {
376 // Push changes to the callback.
377 Runnable r = new Runnable() {
378 public void run() {
379 Callbacks callbacks = getCallback();
380 if (callbacks != null) {
381 callbacks.bindShortcutsChanged(updates,
382 new ArrayList<ShortcutInfo>(), user);
383 }
384 }
385 };
386 mHandler.post(r);
387 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700388 }
389 }
390 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700391 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700392 }
393
Adam Cohen76a47a12014-02-05 11:47:43 -0800394 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800395 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800396
397 if (allAppsApps == null) {
398 throw new RuntimeException("allAppsApps must not be null");
399 }
400 if (allAppsApps.isEmpty()) {
401 return;
402 }
403
404 // Process the newly added applications and add them to the database first
405 Runnable r = new Runnable() {
406 public void run() {
407 runOnMainThread(new Runnable() {
408 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800409 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500411 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800412 }
413 }
414 });
415 }
416 };
417 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700418 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800419
Sunny Goyala9116722015-04-29 13:55:58 -0700420 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 int[] xy, int spanX, int spanY) {
422 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700423 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
424 final int xCount = (int) profile.numColumns;
425 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 boolean[][] occupied = new boolean[xCount][yCount];
427 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700428 for (ItemInfo r : occupiedPos) {
429 int right = r.cellX + r.spanX;
430 int bottom = r.cellY + r.spanY;
431 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
432 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 occupied[x][y] = true;
434 }
435 }
436 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800437 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700438 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439 }
440
441 /**
442 * Find a position on the screen for the given size or adds a new screen.
443 * @return screenId and the coordinates for the item.
444 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700445 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 ArrayList<Long> workspaceScreens,
448 ArrayList<Long> addedWorkspaceScreensFinal,
449 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700450 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451
Sunny Goyala9116722015-04-29 13:55:58 -0700452 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700453 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700454 synchronized (sBgLock) {
455 for (ItemInfo info : sBgItemsIdMap) {
456 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
457 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
458 if (items == null) {
459 items = new ArrayList<>();
460 screenItems.put(info.screenId, items);
461 }
462 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
466
467 // Find appropriate space for the item.
468 long screenId = 0;
469 int[] cordinates = new int[2];
470 boolean found = false;
471
472 int screenCount = workspaceScreens.size();
473 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700474 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800475 if (preferredScreenIndex < screenCount) {
476 screenId = workspaceScreens.get(preferredScreenIndex);
477 found = findNextAvailableIconSpaceInScreen(
478 screenItems.get(screenId), cordinates, spanX, spanY);
479 }
480
481 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700482 // Search on any of the screens starting from the first screen.
483 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484 screenId = workspaceScreens.get(screen);
485 if (findNextAvailableIconSpaceInScreen(
486 screenItems.get(screenId), cordinates, spanX, spanY)) {
487 // We found a space for it
488 found = true;
489 break;
490 }
491 }
492 }
493
494 if (!found) {
495 // Still no position found. Add a new screen to the end.
496 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
497
498 // Save the screen id for binding in the workspace
499 workspaceScreens.add(screenId);
500 addedWorkspaceScreensFinal.add(screenId);
501
502 // If we still can't find an empty space, then God help us all!!!
503 if (!findNextAvailableIconSpaceInScreen(
504 screenItems.get(screenId), cordinates, spanX, spanY)) {
505 throw new RuntimeException("Can't find space to add the item");
506 }
507 }
508 return Pair.create(screenId, cordinates);
509 }
510
511 /**
512 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800513 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700515 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800517 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700518 return;
Winson Chung997a9232013-07-24 15:33:46 -0700519 }
Winson Chung64359a52013-07-08 17:17:08 -0700520 // Process the newly added applications and add them to the database first
521 Runnable r = new Runnable() {
522 public void run() {
523 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
524 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
525
Winson Chung76828c82013-08-19 15:43:29 -0700526 // Get the list of workspace screens. We need to append to this list and
527 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
528 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800529 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700530 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700532 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700534 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700536 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 }
Winson Chung76828c82013-08-19 15:43:29 -0700538
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700540 Pair<Long, int[]> coords = findSpaceForItem(context,
541 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800542 1, 1);
543 long screenId = coords.first;
544 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700545
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 ItemInfo itemInfo;
547 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
548 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800549 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700551 } else {
552 throw new RuntimeException("Unexpected info type");
553 }
Winson Chung94d67682013-09-25 16:29:40 -0700554
Winson Chung64359a52013-07-08 17:17:08 -0700555 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700556 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700557 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700558 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700559 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700560 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700561 }
562 }
563
Winson Chung76828c82013-08-19 15:43:29 -0700564 // Update the workspace screens
565 updateWorkspaceScreenOrder(context, workspaceScreens);
566
Adam Cohen76a47a12014-02-05 11:47:43 -0800567 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700568 runOnMainThread(new Runnable() {
569 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800570 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700571 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700572 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
573 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700574 if (!addedShortcutsFinal.isEmpty()) {
575 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
576 long lastScreenId = info.screenId;
577 for (ItemInfo i : addedShortcutsFinal) {
578 if (i.screenId == lastScreenId) {
579 addAnimated.add(i);
580 } else {
581 addNotAnimated.add(i);
582 }
Winson Chung997a9232013-07-24 15:33:46 -0700583 }
584 }
Winson Chungd64d1762013-08-20 14:37:16 -0700585 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800586 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700587 }
Winson Chung64359a52013-07-08 17:17:08 -0700588 }
Winson Chung997a9232013-07-24 15:33:46 -0700589 });
590 }
Winson Chung64359a52013-07-08 17:17:08 -0700591 }
592 };
593 runOnWorkerThread(r);
594 }
595
Sunny Goyald33860f2015-04-23 16:02:20 -0700596 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700597 if (sWorkerThread.getThreadId() == Process.myTid()) {
598 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
599 "main thread");
600 }
601
602 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400603 synchronized (mDeferredBindRunnables) {
604 mDeferredBindRunnables.clear();
605 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700606
607 // Remove any queued UI runnables
608 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700609 // Unbind all the workspace items
610 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700611 }
612
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700613 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700614 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700615 // Ensure that we don't use the same workspace items data structure on the main thread
616 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700617 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700618 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700619 tmpItems.addAll(sBgWorkspaceItems);
620 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700621 }
622 Runnable r = new Runnable() {
623 @Override
624 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700625 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 item.unbind();
627 }
628 }
629 };
630 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700631 }
632
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 /**
634 * Adds an item to the DB if it was not created previously, or move it to a new
635 * <container, screen, cellX, cellY>
636 */
637 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 if (item.container == ItemInfo.NO_ID) {
640 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700641 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 } else {
643 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
646 }
647
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700648 static void checkItemInfoLocked(
649 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
650 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
651 if (modelItem != null && item != modelItem) {
652 // check all the data is consistent
653 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
654 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
655 ShortcutInfo shortcut = (ShortcutInfo) item;
656 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
657 modelShortcut.intent.filterEquals(shortcut.intent) &&
658 modelShortcut.id == shortcut.id &&
659 modelShortcut.itemType == shortcut.itemType &&
660 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700662 modelShortcut.cellX == shortcut.cellX &&
663 modelShortcut.cellY == shortcut.cellY &&
664 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700665 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700666 // For all intents and purposes, this is the same object
667 return;
668 }
669 }
670
671 // the modelItem needs to match up perfectly with item if our model is
672 // to be consistent with the database-- for now, just require
673 // modelItem == item or the equality check above
674 String msg = "item: " + ((item != null) ? item.toString() : "null") +
675 "modelItem: " +
676 ((modelItem != null) ? modelItem.toString() : "null") +
677 "Error: ItemInfo passed to checkItemInfo doesn't match original";
678 RuntimeException e = new RuntimeException(msg);
679 if (stackTrace != null) {
680 e.setStackTrace(stackTrace);
681 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800682 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700683 }
684 }
685
Michael Jurka816474f2012-06-25 14:49:02 -0700686 static void checkItemInfo(final ItemInfo item) {
687 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
688 final long itemId = item.id;
689 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700690 public void run() {
691 synchronized (sBgLock) {
692 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700693 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700694 }
695 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700696 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700697 }
698
Michael Jurkac9d95c52011-08-29 14:03:34 -0700699 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
700 final ItemInfo item, final String callingFunction) {
701 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700702 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703 final ContentResolver cr = context.getContentResolver();
704
Adam Cohen487f7dd2012-06-28 18:12:10 -0700705 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706 Runnable r = new Runnable() {
707 public void run() {
708 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700709 updateItemArrays(item, itemId, stackTrace);
710 }
711 };
712 runOnWorkerThread(r);
713 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700714
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700715 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
716 final ArrayList<ItemInfo> items, final String callingFunction) {
717 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700718
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
720 Runnable r = new Runnable() {
721 public void run() {
722 ArrayList<ContentProviderOperation> ops =
723 new ArrayList<ContentProviderOperation>();
724 int count = items.size();
725 for (int i = 0; i < count; i++) {
726 ItemInfo item = items.get(i);
727 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700728 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700730
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700731 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
732 updateItemArrays(item, itemId, stackTrace);
733
734 }
735 try {
736 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
737 } catch (Exception e) {
738 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700739 }
740 }
741 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700742 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700744
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
746 // Lock on mBgLock *after* the db operation
747 synchronized (sBgLock) {
748 checkItemInfoLocked(itemId, item, stackTrace);
749
750 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
751 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
752 // Item is in a folder, make sure this folder exists
753 if (!sBgFolders.containsKey(item.container)) {
754 // An items container is being set to a that of an item which is not in
755 // the list of Folders.
756 String msg = "item: " + item + " container being set to: " +
757 item.container + ", not in the list of folders";
758 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 }
760 }
761
762 // Items are added/removed from the corresponding FolderInfo elsewhere, such
763 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
764 // that are on the desktop, as appropriate
765 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800766 if (modelItem != null &&
767 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
768 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 switch (modelItem.itemType) {
770 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
771 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
772 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
773 if (!sBgWorkspaceItems.contains(modelItem)) {
774 sBgWorkspaceItems.add(modelItem);
775 }
776 break;
777 default:
778 break;
779 }
780 } else {
781 sBgWorkspaceItems.remove(modelItem);
782 }
783 }
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700788 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700789 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 item.cellX = cellX;
793 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700794
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 // We store hotseat items in canonical form which is this orientation invariant position
796 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803
804 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
807 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800808 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700812 }
813
814 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
816 * cellX, cellY have already been updated on the ItemInfos.
817 */
818 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
819 final long container, final int screen) {
820
821 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
822 int count = items.size();
823
824 for (int i = 0; i < count; i++) {
825 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700826 item.container = container;
827
828 // We store hotseat items in canonical form which is this orientation invariant position
829 // in the hotseat
830 if (context instanceof Launcher && screen < 0 &&
831 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700832 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700833 item.cellY);
834 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 }
837
838 final ContentValues values = new ContentValues();
839 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
840 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
841 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800842 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700844
845 contentValues.add(values);
846 }
847 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
848 }
849
850 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800852 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700855 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800856 item.cellX = cellX;
857 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 item.spanX = spanX;
859 item.spanY = spanY;
860
861 // We store hotseat items in canonical form which is this orientation invariant position
862 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700867 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700868 }
Adam Cohend4844c32011-02-18 19:25:06 -0800869
Adam Cohend4844c32011-02-18 19:25:06 -0800870 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800871 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
873 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800874 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
876 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800878
Michael Jurka816474f2012-06-25 14:49:02 -0700879 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700880 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700881
882 /**
883 * Update an item to the database in a specified container.
884 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700885 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100887 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800889 }
890
Sunny Goyal756a28a2015-04-23 17:07:55 -0700891 private void assertWorkspaceLoaded() {
Sunny Goyal6c56c682015-07-16 14:09:05 -0700892 if (ProviderConfig.IS_DOGFOOD_BUILD && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700893 throw new RuntimeException("Trying to add shortcut while loader is running");
894 }
895 }
896
Adam Cohend4844c32011-02-18 19:25:06 -0800897 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700898 * Returns true if the shortcuts already exists on the workspace. This must be called after
899 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700901 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
902 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700903 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 if (intent.getComponent() != null) {
905 // If component is not null, an intent with null package will produce
906 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700907 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 intentWithPkg = intent.toUri(0);
910 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
913 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 }
915 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 intentWithPkg = intent.toUri(0);
917 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700919
920 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700921 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700922 if (item instanceof ShortcutInfo) {
923 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700924 Intent targetIntent = info.promisedIntent == null
925 ? info.intent : info.promisedIntent;
926 if (targetIntent != null && info.user.equals(user)) {
927 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700928 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
929 return true;
930 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 }
932 }
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700935 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700936 }
937
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 /**
939 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
940 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700941 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 final ContentResolver cr = context.getContentResolver();
943 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
944 "_id=? and (itemType=? or itemType=?)",
945 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700946 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 try {
949 if (c.moveToFirst()) {
950 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
951 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
952 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
953 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
954 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
955 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700956 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 FolderInfo folderInfo = null;
959 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700963 }
964
Sunny Goyala508e4f2015-05-21 09:33:57 -0700965 // Do not trim the folder label, as is was set by the user.
966 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 folderInfo.id = id;
968 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700969 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700970 folderInfo.cellX = c.getInt(cellXIndex);
971 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700972 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973
974 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 } finally {
977 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700978 }
979
980 return null;
981 }
982
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 /**
984 * Add an item to the database in a specified container. Sets the container, screen, cellX and
985 * cellY fields of the item. Also assigns an ID to the item.
986 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700987 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700988 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 item.cellX = cellX;
991 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 // We store hotseat items in canonical form which is this orientation invariant position
993 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
1001 final ContentValues values = new ContentValues();
1002 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001003 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004
Michael Jurka414300a2013-08-27 15:42:35 +02001005 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001007
Jason Monk8e19cf22014-03-20 15:06:57 -04001008 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001009 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001011 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001014 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001015 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 sBgItemsIdMap.put(item.id, item);
1017 switch (item.itemType) {
1018 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1019 sBgFolders.put(item.id, (FolderInfo) item);
1020 // Fall through
1021 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1022 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1023 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1024 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1025 sBgWorkspaceItems.add(item);
1026 } else {
1027 if (!sBgFolders.containsKey(item.container)) {
1028 // Adding an item to a folder that doesn't exist.
1029 String msg = "adding item: " + item + " to a folder that " +
1030 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001031 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001033 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034 break;
1035 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1036 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1037 break;
1038 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001039 }
1040 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001041 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001043 }
1044
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001046 * Creates a new unique child id, for a given cell span across all layouts.
1047 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001048 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001051 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001052 }
1053
Sunny Goyal34942622014-08-29 17:20:55 -07001054 private static ArrayList<ItemInfo> getItemsByPackageName(
1055 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001056 ItemInfoFilter filter = new ItemInfoFilter() {
1057 @Override
1058 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1059 return cn.getPackageName().equals(pn) && info.user.equals(user);
1060 }
1061 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001062 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001063 }
1064
1065 /**
1066 * Removes all the items from the database corresponding to the specified package.
1067 */
1068 static void deletePackageFromDatabase(Context context, final String pn,
1069 final UserHandleCompat user) {
1070 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001071 }
1072
1073 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001074 * Removes the specified item from the database
1075 * @param context
1076 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001078 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001079 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1080 items.add(item);
1081 deleteItemsFromDatabase(context, items);
1082 }
1083
1084 /**
1085 * Removes the specified items from the database
1086 * @param context
1087 * @param item
1088 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001089 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001091 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001092 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001093 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001094 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001096
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001097 // Lock on mBgLock *after* the db operation
1098 synchronized (sBgLock) {
1099 switch (item.itemType) {
1100 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1101 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001102 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 if (info.container == item.id) {
1104 // We are deleting a folder which still contains items that
1105 // think they are contained by that folder.
1106 String msg = "deleting a folder (" + item + ") which still " +
1107 "contains items (" + info + ")";
1108 Log.e(TAG, msg);
1109 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001111 sBgWorkspaceItems.remove(item);
1112 break;
1113 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1114 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1115 sBgWorkspaceItems.remove(item);
1116 break;
1117 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1118 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1119 break;
1120 }
1121 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001123 }
1124 }
Michael Jurka83df1882011-08-31 20:59:26 -07001125 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 }
1128
1129 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 * Update the order of the workspace screens in the database. The array list contains
1131 * a list of screen ids in the order that they should appear.
1132 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001133 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001134 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 final ContentResolver cr = context.getContentResolver();
1136 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1137
1138 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001139 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001140 while (iter.hasNext()) {
1141 long id = iter.next();
1142 if (id < 0) {
1143 iter.remove();
1144 }
1145 }
1146
1147 Runnable r = new Runnable() {
1148 @Override
1149 public void run() {
Yura085c8532014-02-11 15:15:29 +00001150 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001152 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001153 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 for (int i = 0; i < count; i++) {
1155 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001156 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1158 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001159 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 }
Yura085c8532014-02-11 15:15:29 +00001161
1162 try {
1163 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1164 } catch (Exception ex) {
1165 throw new RuntimeException(ex);
1166 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001167
Winson Chungba9c37f2013-08-30 14:11:37 -07001168 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001169 sBgWorkspaceScreens.clear();
1170 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001171 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001172 }
1173 };
1174 runOnWorkerThread(r);
1175 }
1176
1177 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 * Remove the contents of the specified folder from the database
1179 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001180 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 final ContentResolver cr = context.getContentResolver();
1182
Michael Jurkac9d95c52011-08-29 14:03:34 -07001183 Runnable r = new Runnable() {
1184 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001185 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001187 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 sBgItemsIdMap.remove(info.id);
1189 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 sBgWorkspaceItems.remove(info);
1191 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001192
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001193 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001194 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001196 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 for (ItemInfo childInfo : info.contents) {
1198 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001200 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001201 }
1202 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 }
1205
1206 /**
1207 * Set this as the current Launcher activity object for the loader.
1208 */
1209 public void initialize(Callbacks callbacks) {
1210 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001211 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1212 // workspace to prevent leaking Launcher activities on orientation change.
1213 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 mCallbacks = new WeakReference<Callbacks>(callbacks);
1215 }
1216 }
1217
Kenny Guyed131872014-04-30 03:02:21 +01001218 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001219 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001220 int op = PackageUpdatedTask.OP_UPDATE;
1221 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1222 user));
1223 }
1224
1225 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001226 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001227 int op = PackageUpdatedTask.OP_REMOVE;
1228 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1229 user));
1230 }
1231
1232 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001233 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001234 int op = PackageUpdatedTask.OP_ADD;
1235 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1236 user));
1237 }
1238
1239 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001240 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001241 boolean replacing) {
1242 if (!replacing) {
1243 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1244 user));
1245 if (mAppsCanBeOnRemoveableStorage) {
1246 // Only rebind if we support removable storage. It catches the
1247 // case where
1248 // apps on the external sd card need to be reloaded
1249 startLoaderFromBackground();
1250 }
1251 } else {
1252 // If we are replacing then just update the packages in the list
1253 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1254 packageNames, user));
1255 }
1256 }
1257
1258 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001259 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001260 boolean replacing) {
1261 if (!replacing) {
1262 enqueuePackageUpdated(new PackageUpdatedTask(
1263 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1264 user));
1265 }
Kenny Guyed131872014-04-30 03:02:21 +01001266 }
1267
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001268 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1270 * ACTION_PACKAGE_CHANGED.
1271 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001272 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001273 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001274 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001275
Joe Onorato36115782010-06-17 13:28:48 -04001276 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001277 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001278 // If we have changed locale we need to clear out the labels in all apps/workspace.
1279 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001280 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001281 Callbacks callbacks = getCallback();
1282 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001283 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001284 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001285 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1286 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001287 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001288 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001289 }
1290 }
1291
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001292 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001293 resetLoadedState(true, true);
1294
Reena Lee93f824a2011-09-23 17:20:28 -07001295 // Do this here because if the launcher activity is running it will be restarted.
1296 // If it's not running startLoaderFromBackground will merely tell it that it needs
1297 // to reload.
1298 startLoaderFromBackground();
1299 }
1300
Winson Chungf0c6ae02012-03-21 16:10:31 -07001301 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1302 synchronized (mLock) {
1303 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1304 // mWorkspaceLoaded to true later
1305 stopLoaderLocked();
1306 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1307 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1308 }
1309 }
1310
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001311 /**
1312 * When the launcher is in the background, it's possible for it to miss paired
1313 * configuration changes. So whenever we trigger the loader from the background
1314 * tell the launcher that it needs to re-run the loader when it comes back instead
1315 * of doing it now.
1316 */
1317 public void startLoaderFromBackground() {
1318 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001319 Callbacks callbacks = getCallback();
1320 if (callbacks != null) {
1321 // Only actually run the loader if they're not paused.
1322 if (!callbacks.setLoadOnResume()) {
1323 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001324 }
1325 }
1326 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001327 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001328 }
Joe Onorato36115782010-06-17 13:28:48 -04001329 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001330
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001331 /**
1332 * If there is already a loader task running, tell it to stop.
1333 */
1334 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001335 LoaderTask oldTask = mLoaderTask;
1336 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001337 oldTask.stopLocked();
1338 }
Reena Lee93f824a2011-09-23 17:20:28 -07001339 }
1340
Adam Cohen1a85c582014-09-30 09:48:49 -07001341 public boolean isCurrentCallbacks(Callbacks callbacks) {
1342 return (mCallbacks != null && mCallbacks.get() == callbacks);
1343 }
1344
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001345 public void startLoader(int synchronousBindPage) {
1346 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001347 }
1348
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001350 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1351 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001352 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 // Clear any deferred bind-runnables from the synchronized load process
1354 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001355 synchronized (mDeferredBindRunnables) {
1356 mDeferredBindRunnables.clear();
1357 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358
Joe Onorato36115782010-06-17 13:28:48 -04001359 // Don't bother to start the thread if we know it's not going to do anything
1360 if (mCallbacks != null && mCallbacks.get() != null) {
1361 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001362 stopLoaderLocked();
1363 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001364 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001365 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1367 } else {
1368 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1369 sWorker.post(mLoaderTask);
1370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373 }
1374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 void bindRemainingSynchronousPages() {
1376 // Post the remaining side pages to be loaded
1377 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001378 Runnable[] deferredBindRunnables = null;
1379 synchronized (mDeferredBindRunnables) {
1380 deferredBindRunnables = mDeferredBindRunnables.toArray(
1381 new Runnable[mDeferredBindRunnables.size()]);
1382 mDeferredBindRunnables.clear();
1383 }
1384 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001385 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001388
1389 // Run all the bind complete runnables after workspace is bound.
1390 if (!mBindCompleteRunnables.isEmpty()) {
1391 synchronized (mBindCompleteRunnables) {
1392 for (final Runnable r : mBindCompleteRunnables) {
1393 runOnWorkerThread(r);
1394 }
1395 mBindCompleteRunnables.clear();
1396 }
1397 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 }
1399
Joe Onorato36115782010-06-17 13:28:48 -04001400 public void stopLoader() {
1401 synchronized (mLock) {
1402 if (mLoaderTask != null) {
1403 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 }
1405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001408 /**
1409 * Loads the workspace screen ids in an ordered list.
1410 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001411 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001412 final ContentResolver contentResolver = context.getContentResolver();
1413 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001414
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001415 // Get screens ordered by rank.
1416 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1417 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1418 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001419 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001420 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001421 while (sc.moveToNext()) {
1422 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001423 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001424 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001425 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1426 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001427 }
1428 }
1429 } finally {
1430 sc.close();
1431 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001432 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001433 }
1434
Michael Jurkac57b7a82011-08-09 22:02:20 -07001435 public boolean isAllAppsLoaded() {
1436 return mAllAppsLoaded;
1437 }
1438
Winson Chung36a62fe2012-05-06 18:04:42 -07001439 boolean isLoadingWorkspace() {
1440 synchronized (mLock) {
1441 if (mLoaderTask != null) {
1442 return mLoaderTask.isLoadingWorkspace();
1443 }
1444 }
1445 return false;
1446 }
1447
Joe Onorato36115782010-06-17 13:28:48 -04001448 /**
1449 * Runnable for the thread that loads the contents of the launcher:
1450 * - workspace icons
1451 * - widgets
1452 * - all apps icons
1453 */
1454 private class LoaderTask implements Runnable {
1455 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001456 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001457 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001458 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001459 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001461 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001462 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001463 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 }
1465
Winson Chung36a62fe2012-05-06 18:04:42 -07001466 boolean isLoadingWorkspace() {
1467 return mIsLoadingAndBindingWorkspace;
1468 }
1469
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001470 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001471 mIsLoadingAndBindingWorkspace = true;
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001474 if (DEBUG_LOADERS) {
1475 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001477
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001478 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001479 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001480 synchronized (LoaderTask.this) {
1481 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001482 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001483 }
1484 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 }
1486 }
1487
Joe Onorato36115782010-06-17 13:28:48 -04001488 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001489 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001490 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001491
Joe Onorato36115782010-06-17 13:28:48 -04001492 private void waitForIdle() {
1493 // Wait until the either we're stopped or the other threads are done.
1494 // This way we don't start loading all apps until the workspace has settled
1495 // down.
1496 synchronized (LoaderTask.this) {
1497 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001498
Joe Onorato36115782010-06-17 13:28:48 -04001499 mHandler.postIdle(new Runnable() {
1500 public void run() {
1501 synchronized (LoaderTask.this) {
1502 mLoadAndBindStepFinished = true;
1503 if (DEBUG_LOADERS) {
1504 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001505 }
Joe Onorato36115782010-06-17 13:28:48 -04001506 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001507 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509 });
1510
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001511 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001512 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001513 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1514 // wait no longer than 1sec at a time
1515 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001516 } catch (InterruptedException ex) {
1517 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001518 }
1519 }
Joe Onorato36115782010-06-17 13:28:48 -04001520 if (DEBUG_LOADERS) {
1521 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001522 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001523 + "ms for previous step to finish binding");
1524 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001525 }
Joe Onorato36115782010-06-17 13:28:48 -04001526 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001527
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001529 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 // Ensure that we have a valid page index to load synchronously
1531 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1532 "valid page index");
1533 }
1534 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1535 // Ensure that we don't try and bind a specified page when the pages have not been
1536 // loaded already (we should load everything asynchronously in that case)
1537 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1538 }
1539 synchronized (mLock) {
1540 if (mIsLoaderTaskRunning) {
1541 // Ensure that we are never running the background loading at this point since
1542 // we also touch the background collections
1543 throw new RuntimeException("Error! Background loading is already running");
1544 }
1545 }
1546
1547 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1548 // data structures, we can't allow any other thread to touch that data, but because
1549 // this call is synchronous, we can get away with not locking).
1550
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001551 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001552 // operations from the previous activity. We need to ensure that all queued operations
1553 // are executed before any synchronous binding work is done.
1554 mHandler.flush();
1555
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001556 // Divide the set of loaded items into those that we are binding synchronously, and
1557 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001558 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1560 // arise from that.
1561 onlyBindAllApps();
1562 }
1563
Joe Onorato36115782010-06-17 13:28:48 -04001564 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001566 if (mStopped) {
1567 return;
1568 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001569 mIsLoaderTaskRunning = true;
1570 }
Joe Onorato36115782010-06-17 13:28:48 -04001571 // Optimize for end-user experience: if the Launcher is up and // running with the
1572 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1573 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001574 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001575 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001576 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001577
Joe Onorato36115782010-06-17 13:28:48 -04001578 if (mStopped) {
1579 break keep_running;
1580 }
1581
Joe Onorato36115782010-06-17 13:28:48 -04001582 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001583
1584 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001585 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1586 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001587 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588
Joe Onorato36115782010-06-17 13:28:48 -04001589 // Clear out this reference, otherwise we end up holding it until all of the
1590 // callback runnables are done.
1591 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592
Joe Onorato36115782010-06-17 13:28:48 -04001593 synchronized (mLock) {
1594 // If we are still the last one to be scheduled, remove ourselves.
1595 if (mLoaderTask == this) {
1596 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001598 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001599 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001600 }
Joe Onorato36115782010-06-17 13:28:48 -04001601 }
1602
1603 public void stopLocked() {
1604 synchronized (LoaderTask.this) {
1605 mStopped = true;
1606 this.notify();
1607 }
1608 }
1609
1610 /**
1611 * Gets the callbacks object. If we've been stopped, or if the launcher object
1612 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1613 * object that was around when the deferred message was scheduled, and if there's
1614 * a new Callbacks object around then also return null. This will save us from
1615 * calling onto it with data that will be ignored.
1616 */
1617 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1618 synchronized (mLock) {
1619 if (mStopped) {
1620 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622
1623 if (mCallbacks == null) {
1624 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626
1627 final Callbacks callbacks = mCallbacks.get();
1628 if (callbacks != oldCallbacks) {
1629 return null;
1630 }
1631 if (callbacks == null) {
1632 Log.w(TAG, "no mCallbacks");
1633 return null;
1634 }
1635
1636 return callbacks;
1637 }
1638 }
1639
1640 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001641 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001642 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001643 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1644 final int countX = (int) profile.numColumns;
1645 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001646
Adam Cohendcd297f2013-06-18 13:13:40 -07001647 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001648 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001649 // Return early if we detect that an item is under the hotseat button
1650 if (mCallbacks == null ||
1651 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001652 Log.e(TAG, "Error loading shortcut into hotseat " + item
1653 + " into position (" + item.screenId + ":" + item.cellX + ","
1654 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001655 return false;
1656 }
1657
Dan Sandler295ae182013-12-10 16:05:47 -05001658 final ItemInfo[][] hotseatItems =
1659 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1660
Adam Cohen2e6da152015-05-06 11:42:25 -07001661 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001662 Log.e(TAG, "Error loading shortcut " + item
1663 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001664 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001665 + ")");
1666 return false;
1667 }
1668
Dan Sandler295ae182013-12-10 16:05:47 -05001669 if (hotseatItems != null) {
1670 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001671 Log.e(TAG, "Error loading shortcut into hotseat " + item
1672 + " into position (" + item.screenId + ":" + item.cellX + ","
1673 + item.cellY + ") occupied by "
1674 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1675 [(int) item.screenId][0]);
1676 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001677 } else {
1678 hotseatItems[(int) item.screenId][0] = item;
1679 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001680 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001681 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001682 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001683 items[(int) item.screenId][0] = item;
1684 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001685 return true;
1686 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001687 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1688 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001689 return true;
1690 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001691
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001693 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001694 occupied.put(item.screenId, items);
1695 }
1696
Dan Sandler295ae182013-12-10 16:05:47 -05001697 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001698 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1699 item.cellX < 0 || item.cellY < 0 ||
1700 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1701 Log.e(TAG, "Error loading shortcut " + item
1702 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1703 + item.cellX + "," + item.cellY
1704 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1705 return false;
1706 }
1707
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001708 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001709 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1710 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001712 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001713 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001714 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001715 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001717 return false;
1718 }
1719 }
1720 }
1721 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1722 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001723 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001724 }
1725 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001726
Joe Onorato36115782010-06-17 13:28:48 -04001727 return true;
1728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729
Winson Chungba9c37f2013-08-30 14:11:37 -07001730 /** Clears all the sBg data structures */
1731 private void clearSBgDataStructures() {
1732 synchronized (sBgLock) {
1733 sBgWorkspaceItems.clear();
1734 sBgAppWidgets.clear();
1735 sBgFolders.clear();
1736 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001737 sBgWorkspaceScreens.clear();
1738 }
1739 }
1740
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001741 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001742 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743
Joe Onorato36115782010-06-17 13:28:48 -04001744 final Context context = mContext;
1745 final ContentResolver contentResolver = context.getContentResolver();
1746 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001747 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001748 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001749 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001750
Winson Chung892c74d2013-08-22 16:15:50 -07001751 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001752 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1753 int countX = (int) profile.numColumns;
1754 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001755
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001756 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001757 long migrationStartTime = System.currentTimeMillis();
1758 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001759 try {
1760 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1761 // Clear the flags before starting the task, so that we do not run the task
1762 // again, in case there was an uncaught error.
1763 MigrateFromRestoreTask.clearFlags(mContext);
1764 task.execute();
1765 } catch (Exception e) {
1766 Log.e(TAG, "Error during grid migration", e);
1767
1768 // Clear workspace.
1769 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1770 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001771 Log.v(TAG, "Workspace migration completed in "
1772 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001773 }
1774
Dan Sandlerd5024042014-01-09 15:01:33 -05001775 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1776 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1777 LauncherAppState.getLauncherProvider().deleteDatabase();
1778 }
1779
1780 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1781 // append the user's Launcher2 shortcuts
1782 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1783 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1784 } else {
1785 // Make sure the default workspace is loaded
1786 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001787 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001788 }
Adam Cohene25af792013-06-06 23:08:25 -07001789
Winson Chung2abf94d2012-07-18 18:16:38 -07001790 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001791 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001792 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001793 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001794
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001796 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001797 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001798 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001799 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001800
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 // +1 for the hotseat (it can be larger than the workspace)
1802 // Load workspace in reverse order to ensure that latest items are loaded first (and
1803 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001804 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 try {
1807 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1808 final int intentIndex = c.getColumnIndexOrThrow
1809 (LauncherSettings.Favorites.INTENT);
1810 final int titleIndex = c.getColumnIndexOrThrow
1811 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 final int containerIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.CONTAINER);
1814 final int itemTypeIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.ITEM_TYPE);
1816 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001818 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 final int screenIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.SCREEN);
1822 final int cellXIndex = c.getColumnIndexOrThrow
1823 (LauncherSettings.Favorites.CELLX);
1824 final int cellYIndex = c.getColumnIndexOrThrow
1825 (LauncherSettings.Favorites.CELLY);
1826 final int spanXIndex = c.getColumnIndexOrThrow
1827 (LauncherSettings.Favorites.SPANX);
1828 final int spanYIndex = c.getColumnIndexOrThrow(
1829 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001830 final int rankIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001832 final int restoredIndex = c.getColumnIndexOrThrow(
1833 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001834 final int profileIdIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001836 final int optionsIndex = c.getColumnIndexOrThrow(
1837 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001838 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839
Sunny Goyal7f834d22015-04-21 10:10:23 -07001840 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1841 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1842 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1843 }
1844
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 ShortcutInfo info;
1846 String intentDescription;
1847 LauncherAppWidgetInfo appWidgetInfo;
1848 int container;
1849 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001850 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001852 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 while (!mStopped && c.moveToNext()) {
1855 try {
1856 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001857 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001858 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001859 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 switch (itemType) {
1862 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1863 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001864 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001866 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001867 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001868 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001869 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001870 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001871 if (user == null) {
1872 // User has been deleted remove the item.
1873 itemsToRemove.add(id);
1874 continue;
1875 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 try {
1877 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001878 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001879 if (cn != null && cn.getPackageName() != null) {
1880 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1881 cn.getPackageName(), user);
1882 boolean validComponent = validPkg &&
1883 launcherApps.isActivityEnabledForProfile(cn, user);
1884
1885 if (validComponent) {
1886 if (restored) {
1887 // no special handling necessary for this item
1888 restoredRows.add(id);
1889 restored = false;
1890 }
1891 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001892 intent = null;
1893 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1894 // We allow auto install apps to have their intent
1895 // updated after an install.
1896 intent = manager.getLaunchIntentForPackage(
1897 cn.getPackageName());
1898 if (intent != null) {
1899 ContentValues values = new ContentValues();
1900 values.put(LauncherSettings.Favorites.INTENT,
1901 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001902 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001903 }
1904 }
1905
1906 if (intent == null) {
1907 // The app is installed but the component is no
1908 // longer available.
1909 Launcher.addDumpLog(TAG,
1910 "Invalid component removed: " + cn, true);
1911 itemsToRemove.add(id);
1912 continue;
1913 } else {
1914 // no special handling necessary for this item
1915 restoredRows.add(id);
1916 restored = false;
1917 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001918 } else if (restored) {
1919 // Package is not yet available but might be
1920 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001921 Launcher.addDumpLog(TAG,
1922 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001923
1924 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1925 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001926 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001927 // App restore has started. Update the flag
1928 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1929 ContentValues values = new ContentValues();
1930 values.put(LauncherSettings.Favorites.RESTORED,
1931 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001932 updateItem(id, values);
1933 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1934 // This is a common app. Try to replace this.
1935 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1936 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1937 if (parser.findDefaultApp()) {
1938 // Default app found. Replace it.
1939 intent = parser.parsedIntent;
1940 cn = intent.getComponent();
1941 ContentValues values = parser.parsedValues;
1942 values.put(LauncherSettings.Favorites.RESTORED, 0);
1943 updateItem(id, values);
1944 restored = false;
1945 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001946
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001947 } else if (REMOVE_UNRESTORED_ICONS) {
1948 Launcher.addDumpLog(TAG,
1949 "Unrestored package removed: " + cn, true);
1950 itemsToRemove.add(id);
1951 continue;
1952 }
Sunny Goyal94485362014-09-18 16:13:58 -07001953 } else if (REMOVE_UNRESTORED_ICONS) {
1954 Launcher.addDumpLog(TAG,
1955 "Unrestored package removed: " + cn, true);
1956 itemsToRemove.add(id);
1957 continue;
1958 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001959 } else if (launcherApps.isAppEnabled(
1960 manager, cn.getPackageName(),
1961 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1962 // Package is present but not available.
1963 allowMissingTarget = true;
1964 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1965 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001966 // SdCard is not ready yet. Package might get available,
1967 // once it is ready.
1968 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1969 + " (check again later)", true);
1970 HashSet<String> pkgs = sPendingPackages.get(user);
1971 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001972 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001973 sPendingPackages.put(user, pkgs);
1974 }
1975 pkgs.add(cn.getPackageName());
1976 allowMissingTarget = true;
1977 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001978
1979 } else {
1980 // Do not wait for external media load anymore.
1981 // Log the invalid package, and remove it
1982 Launcher.addDumpLog(TAG,
1983 "Invalid package removed: " + cn, true);
1984 itemsToRemove.add(id);
1985 continue;
Winson Chungee055712013-07-30 14:46:24 -07001986 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001987 } else if (cn == null) {
1988 // For shortcuts with no component, keep them as they are
1989 restoredRows.add(id);
1990 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001991 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001993 Launcher.addDumpLog(TAG,
1994 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001995 continue;
1996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997
Sunny Goyal34b65272015-03-11 16:56:52 -07001998 boolean useLowResIcon = container >= 0 &&
1999 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2000
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002001 if (itemReplaced) {
2002 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002003 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002004 cursorIconInfo.iconIndex, titleIndex,
2005 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002006 } else {
2007 // Don't replace items for other profiles.
2008 itemsToRemove.add(id);
2009 continue;
2010 }
2011 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002012 if (user.equals(UserHandleCompat.myUserHandle())) {
2013 Launcher.addDumpLog(TAG,
2014 "constructing info for partially restored package",
2015 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002016 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002017 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002018 intent = getRestoredItemIntent(c, context, intent);
2019 } else {
2020 // Don't restore items for other profiles.
2021 itemsToRemove.add(id);
2022 continue;
2023 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002024 } else if (itemType ==
2025 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002026 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002027 cursorIconInfo.iconIndex, titleIndex,
2028 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002030 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002031
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 // App shortcuts that used to be automatically added to Launcher
2033 // didn't always have the correct intent flags set, so do that
2034 // here
2035 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002036 intent.getCategories() != null &&
2037 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002038 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 intent.addFlags(
2040 Intent.FLAG_ACTIVITY_NEW_TASK |
2041 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2042 }
Michael Jurka96879562012-03-22 05:54:33 -07002043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002046 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002049 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 info.cellX = c.getInt(cellXIndex);
2051 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002052 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002053 info.spanX = 1;
2054 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002055 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002056 if (info.promisedIntent != null) {
2057 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2058 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002059 info.isDisabled = disabledState;
2060 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2061 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2062 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002065 if (!checkItemPlacement(occupied, info)) {
2066 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 break;
2068 }
2069
Sunny Goyal756adbc2015-04-16 15:20:51 -07002070 if (restored) {
2071 ComponentName cn = info.getTargetComponent();
2072 if (cn != null) {
2073 Integer progress = installingPkgs.get(cn.getPackageName());
2074 if (progress != null) {
2075 info.setInstallProgress(progress);
2076 } else {
2077 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2078 }
2079 }
2080 }
2081
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 switch (container) {
2083 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2084 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2085 sBgWorkspaceItems.add(info);
2086 break;
2087 default:
2088 // Item is in a user folder
2089 FolderInfo folderInfo =
2090 findOrMakeFolder(sBgFolders, container);
2091 folderInfo.add(info);
2092 break;
2093 }
2094 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002095 } else {
2096 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 }
2098 break;
2099
2100 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2101 id = c.getLong(idIndex);
2102 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2103
Sunny Goyala508e4f2015-05-21 09:33:57 -07002104 // Do not trim the folder label, as is was set by the user.
2105 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002108 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 folderInfo.cellX = c.getInt(cellXIndex);
2110 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002111 folderInfo.spanX = 1;
2112 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002113 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114
Daniel Sandler8802e962010-05-26 16:28:16 -04002115 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002116 if (!checkItemPlacement(occupied, folderInfo)) {
2117 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002118 break;
2119 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002120
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2123 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2124 sBgWorkspaceItems.add(folderInfo);
2125 break;
Joe Onorato36115782010-06-17 13:28:48 -04002126 }
Joe Onorato17a89222011-02-08 17:26:11 -08002127
Chris Wrenf4d08112014-01-16 18:13:56 -05002128 if (restored) {
2129 // no special handling required for restored folders
2130 restoredRows.add(id);
2131 }
2132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2134 sBgFolders.put(folderInfo.id, folderInfo);
2135 break;
2136
2137 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002138 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002140 boolean customWidget = itemType ==
2141 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2142
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002144 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002145 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002146 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002147 user = allUsers.get(serialNumber);
2148 if (user == null) {
2149 itemsToRemove.add(id);
2150 continue;
2151 }
2152
Sunny Goyalff572272014-07-23 13:58:07 -07002153 final ComponentName component =
2154 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002155
Sunny Goyal651077b2014-06-30 14:15:31 -07002156 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002157 final boolean isIdValid = (restoreStatus &
2158 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002159 final boolean wasProviderReady = (restoreStatus &
2160 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002161
Adam Cohen59400422014-03-05 18:07:04 -08002162 final LauncherAppWidgetProviderInfo provider =
2163 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002164 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002165 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002166
2167 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002168 if (!isSafeMode && !customWidget &&
2169 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002170 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002171 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002172
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002174 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 itemsToRemove.add(id);
2176 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002177 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002178 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2179 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002180
Sunny Goyal53f96722015-07-13 19:54:53 -07002181 // The provider is available. So the widget is either
2182 // available or not available. We do not need to track
2183 // any future restore updates.
2184 int status = restoreStatus &
2185 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002186 if (!wasProviderReady) {
2187 // If provider was not previously ready, update the
2188 // status and UI flag.
2189
2190 // Id would be valid only if the widget restore broadcast was received.
2191 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002192 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002193 } else {
2194 status &= ~LauncherAppWidgetInfo
2195 .FLAG_PROVIDER_NOT_READY;
2196 }
2197 }
2198 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002199 } else {
2200 Log.v(TAG, "Widget restore pending id=" + id
2201 + " appWidgetId=" + appWidgetId
2202 + " status =" + restoreStatus);
2203 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002204 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002205 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002206 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002207
2208 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2209 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002210 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002211 // App restore has started. Update the flag
2212 appWidgetInfo.restoreStatus |=
2213 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002214 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002215 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002216 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002217 itemsToRemove.add(id);
2218 continue;
2219 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002220
2221 appWidgetInfo.installProgress =
2222 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002223 }
Sunny Goyalff572272014-07-23 13:58:07 -07002224
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 appWidgetInfo.cellX = c.getInt(cellXIndex);
2228 appWidgetInfo.cellY = c.getInt(cellYIndex);
2229 appWidgetInfo.spanX = c.getInt(spanXIndex);
2230 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002231 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002232
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2234 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2235 Log.e(TAG, "Widget found where container != " +
2236 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2237 continue;
2238 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002240 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002242 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2243 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 break;
2245 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002246
Adam Cohen59400422014-03-05 18:07:04 -08002247 if (!customWidget) {
2248 String providerName =
2249 appWidgetInfo.providerName.flattenToString();
2250 if (!providerName.equals(savedProvider) ||
2251 (appWidgetInfo.restoreStatus != restoreStatus)) {
2252 ContentValues values = new ContentValues();
2253 values.put(
2254 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2255 providerName);
2256 values.put(LauncherSettings.Favorites.RESTORED,
2257 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002258 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002259 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002260 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2262 sBgAppWidgets.add(appWidgetInfo);
2263 }
Joe Onorato36115782010-06-17 13:28:48 -04002264 break;
2265 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002267 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002268 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 }
2270 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002271 if (c != null) {
2272 c.close();
2273 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 }
2275
Winson Chungba9c37f2013-08-30 14:11:37 -07002276 // Break early if we've stopped loading
2277 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002278 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002279 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002280 }
2281
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002284 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2285 Utilities.createDbSelectionQuery(
2286 LauncherSettings.Favorites._ID, itemsToRemove), null);
2287 if (DEBUG_LOADERS) {
2288 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2289 LauncherSettings.Favorites._ID, itemsToRemove));
2290 }
2291
2292 // Remove any empty folder
2293 for (long folderId : LauncherAppState.getLauncherProvider()
2294 .deleteEmptyFolders()) {
2295 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2296 sBgFolders.remove(folderId);
2297 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002298 }
2299 }
2300
Sunny Goyal317698b2015-07-29 11:45:41 -07002301 // Sort all the folder items and make sure the first 3 items are high resolution.
2302 for (FolderInfo folder : sBgFolders) {
2303 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2304 int pos = 0;
2305 for (ShortcutInfo info : folder.contents) {
2306 if (info.usingLowResIcon) {
2307 info.updateIcon(mIconCache, false);
2308 }
2309 pos ++;
2310 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2311 break;
2312 }
2313 }
2314 }
2315
Chris Wrenf4d08112014-01-16 18:13:56 -05002316 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002317 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002318 ContentValues values = new ContentValues();
2319 values.put(LauncherSettings.Favorites.RESTORED, 0);
2320 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2321 Utilities.createDbSelectionQuery(
2322 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002323 }
2324
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002325 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2326 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002327 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002328 null, sWorker);
2329 }
2330
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002331 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002332
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002333 // Remove any empty screens
2334 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002335 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002336 long screenId = item.screenId;
2337 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2338 unusedScreens.contains(screenId)) {
2339 unusedScreens.remove(screenId);
2340 }
2341 }
2342
2343 // If there are any empty screens remove them, and update.
2344 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002345 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 }
2348
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 if (DEBUG_LOADERS) {
2350 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2351 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002352 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002353 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002355
Sunny Goyale2df0622015-04-24 11:27:00 -07002356 for (int i = 0; i < nScreens; i++) {
2357 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002358 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 line += " | ";
2360 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002361 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002362 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002363 if (x < screen.length && y < screen[x].length) {
2364 line += (screen[x][y] != null) ? "#" : ".";
2365 } else {
2366 line += "!";
2367 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 }
Joe Onorato36115782010-06-17 13:28:48 -04002369 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002372 }
Joe Onorato36115782010-06-17 13:28:48 -04002373 }
Adam Cohene25af792013-06-06 23:08:25 -07002374 }
2375
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002376 /**
2377 * Partially updates the item without any notification. Must be called on the worker thread.
2378 */
2379 private void updateItem(long itemId, ContentValues update) {
2380 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002381 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002382 update,
2383 BaseColumns._ID + "= ?",
2384 new String[]{Long.toString(itemId)});
2385 }
2386
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 /** Filters the set of items who are directly or indirectly (via another container) on the
2388 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002389 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 ArrayList<ItemInfo> allWorkspaceItems,
2391 ArrayList<ItemInfo> currentScreenItems,
2392 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002393 // Purge any null ItemInfos
2394 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2395 while (iter.hasNext()) {
2396 ItemInfo i = iter.next();
2397 if (i == null) {
2398 iter.remove();
2399 }
2400 }
2401
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 // Order the set of items by their containers first, this allows use to walk through the
2403 // list sequentially, build up a list of containers that are in the specified screen,
2404 // as well as all items in those containers.
2405 Set<Long> itemsOnScreen = new HashSet<Long>();
2406 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2407 @Override
2408 public int compare(ItemInfo lhs, ItemInfo rhs) {
2409 return (int) (lhs.container - rhs.container);
2410 }
2411 });
2412 for (ItemInfo info : allWorkspaceItems) {
2413 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002414 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 currentScreenItems.add(info);
2416 itemsOnScreen.add(info.id);
2417 } else {
2418 otherScreenItems.add(info);
2419 }
2420 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2421 currentScreenItems.add(info);
2422 itemsOnScreen.add(info.id);
2423 } else {
2424 if (itemsOnScreen.contains(info.container)) {
2425 currentScreenItems.add(info);
2426 itemsOnScreen.add(info.id);
2427 } else {
2428 otherScreenItems.add(info);
2429 }
2430 }
2431 }
2432 }
2433
2434 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002435 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 ArrayList<LauncherAppWidgetInfo> appWidgets,
2437 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2438 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439
2440 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002441 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002443 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 currentScreenWidgets.add(widget);
2445 } else {
2446 otherScreenWidgets.add(widget);
2447 }
2448 }
2449 }
2450
2451 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002452 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002453 LongArrayMap<ItemInfo> itemsIdMap,
2454 LongArrayMap<FolderInfo> folders,
2455 LongArrayMap<FolderInfo> currentScreenFolders,
2456 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457
Sunny Goyale2df0622015-04-24 11:27:00 -07002458 int total = folders.size();
2459 for (int i = 0; i < total; i++) {
2460 long id = folders.keyAt(i);
2461 FolderInfo folder = folders.valueAt(i);
2462
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002464 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002466 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 currentScreenFolders.put(id, folder);
2468 } else {
2469 otherScreenFolders.put(id, folder);
2470 }
2471 }
2472 }
2473
2474 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2475 * right) */
2476 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002477 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002478 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002479 final int screenCols = profile.numColumns;
2480 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002482 @Override
2483 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002484 if (lhs.container == rhs.container) {
2485 // Within containers, order by their spatial position in that container
2486 switch ((int) lhs.container) {
2487 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2488 long lr = (lhs.screenId * screenCellCount +
2489 lhs.cellY * screenCols + lhs.cellX);
2490 long rr = (rhs.screenId * screenCellCount +
2491 rhs.cellY * screenCols + rhs.cellX);
2492 return (int) (lr - rr);
2493 }
2494 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2495 // We currently use the screen id as the rank
2496 return (int) (lhs.screenId - rhs.screenId);
2497 }
2498 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002499 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002500 throw new RuntimeException("Unexpected container type when " +
2501 "sorting workspace items.");
2502 }
2503 return 0;
2504 }
2505 } else {
2506 // Between containers, order by hotseat, desktop
2507 return (int) (lhs.container - rhs.container);
2508 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002509 }
2510 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002512
Adam Cohendcd297f2013-06-18 13:13:40 -07002513 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2514 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002515 final Runnable r = new Runnable() {
2516 @Override
2517 public void run() {
2518 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2519 if (callbacks != null) {
2520 callbacks.bindScreens(orderedScreens);
2521 }
2522 }
2523 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002524 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002525 }
2526
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2528 final ArrayList<ItemInfo> workspaceItems,
2529 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002530 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002532
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 final boolean postOnMainThread = (deferredBindRunnables != null);
2534
2535 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002536 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002538 final int start = i;
2539 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 final Runnable r = new Runnable() {
2541 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002543 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002544 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002545 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2546 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 }
2548 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 };
2550 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002551 synchronized (deferredBindRunnables) {
2552 deferredBindRunnables.add(r);
2553 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002555 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 }
Joe Onorato36115782010-06-17 13:28:48 -04002557 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558
2559 // Bind the folders
2560 if (!folders.isEmpty()) {
2561 final Runnable r = new Runnable() {
2562 public void run() {
2563 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2564 if (callbacks != null) {
2565 callbacks.bindFolders(folders);
2566 }
2567 }
2568 };
2569 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002570 synchronized (deferredBindRunnables) {
2571 deferredBindRunnables.add(r);
2572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002574 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 }
2576 }
2577
2578 // Bind the widgets, one at a time
2579 N = appWidgets.size();
2580 for (int i = 0; i < N; i++) {
2581 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2582 final Runnable r = new Runnable() {
2583 public void run() {
2584 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2585 if (callbacks != null) {
2586 callbacks.bindAppWidget(widget);
2587 }
2588 }
2589 };
2590 if (postOnMainThread) {
2591 deferredBindRunnables.add(r);
2592 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002593 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 }
2595 }
2596 }
2597
2598 /**
2599 * Binds all loaded data to actual views on the main thread.
2600 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002601 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002602 final long t = SystemClock.uptimeMillis();
2603 Runnable r;
2604
2605 // Don't use these two variables in any of the callback runnables.
2606 // Otherwise we hold a reference to them.
2607 final Callbacks oldCallbacks = mCallbacks.get();
2608 if (oldCallbacks == null) {
2609 // This launcher has exited and nobody bothered to tell us. Just bail.
2610 Log.w(TAG, "LoaderTask running with no launcher");
2611 return;
2612 }
2613
Winson Chung9b9fb962013-11-15 15:39:34 -08002614 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002615 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2616 ArrayList<LauncherAppWidgetInfo> appWidgets =
2617 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002618 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002619
2620 final LongArrayMap<FolderInfo> folders;
2621 final LongArrayMap<ItemInfo> itemsIdMap;
2622
Winson Chung2abf94d2012-07-18 18:16:38 -07002623 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002624 workspaceItems.addAll(sBgWorkspaceItems);
2625 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002626 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002627
2628 folders = sBgFolders.clone();
2629 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 }
2631
Derek Prothro7aff3992013-12-10 14:00:37 -05002632 final boolean isLoadingSynchronously =
2633 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002634 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002635 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002636 if (currScreen >= orderedScreenIds.size()) {
2637 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002638 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002639 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002640 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002641 final long currentScreenId = currentScreen < 0
2642 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002643
2644 // Load all the items that are on the current page first (and in the process, unbind
2645 // all the existing workspace items before we call startBinding() below.
2646 unbindWorkspaceItemsOnMainThread();
2647
2648 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002649 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2650 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2651 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2652 new ArrayList<LauncherAppWidgetInfo>();
2653 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2654 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002655 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2656 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002657
Winson Chung9b9fb962013-11-15 15:39:34 -08002658 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002659 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002660 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002662 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663 otherFolders);
2664 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2665 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2666
2667 // Tell the workspace that we're about to start binding items
2668 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002669 public void run() {
2670 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2671 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002672 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002673 }
2674 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002675 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002676 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002677
Adam Cohendcd297f2013-06-18 13:13:40 -07002678 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 // Load items on the current page
2681 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2682 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002683 if (isLoadingSynchronously) {
2684 r = new Runnable() {
2685 public void run() {
2686 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002687 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002688 callbacks.onPageBoundSynchronously(currentScreen);
2689 }
2690 }
2691 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002692 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002693 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694
Winson Chung4a2afa32012-07-19 14:53:05 -07002695 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2696 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002697 synchronized (mDeferredBindRunnables) {
2698 mDeferredBindRunnables.clear();
2699 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002700 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002701 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002702
2703 // Tell the workspace that we're done binding items
2704 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002705 public void run() {
2706 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2707 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002708 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002709 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002710
Winson Chung98e030b2012-05-07 16:01:11 -07002711 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002712 if (DEBUG_LOADERS) {
2713 Log.d(TAG, "bound workspace in "
2714 + (SystemClock.uptimeMillis()-t) + "ms");
2715 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002716
2717 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002718 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002719 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002720 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002721 synchronized (mDeferredBindRunnables) {
2722 mDeferredBindRunnables.add(r);
2723 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002724 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002725 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002726 }
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002728
Joe Onorato36115782010-06-17 13:28:48 -04002729 private void loadAndBindAllApps() {
2730 if (DEBUG_LOADERS) {
2731 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2732 }
2733 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002734 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002735 synchronized (LoaderTask.this) {
2736 if (mStopped) {
2737 return;
2738 }
2739 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002740 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002741 synchronized (LoaderTask.this) {
2742 if (mStopped) {
2743 return;
2744 }
2745 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002746 }
Joe Onorato36115782010-06-17 13:28:48 -04002747 } else {
2748 onlyBindAllApps();
2749 }
2750 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002751
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002752 private void updateIconCache() {
2753 // Ignore packages which have a promise icon.
2754 HashSet<String> packagesToIgnore = new HashSet<>();
2755 synchronized (sBgLock) {
2756 for (ItemInfo info : sBgItemsIdMap) {
2757 if (info instanceof ShortcutInfo) {
2758 ShortcutInfo si = (ShortcutInfo) info;
2759 if (si.isPromise() && si.getTargetComponent() != null) {
2760 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2761 }
2762 } else if (info instanceof LauncherAppWidgetInfo) {
2763 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2764 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2765 packagesToIgnore.add(lawi.providerName.getPackageName());
2766 }
2767 }
2768 }
2769 }
2770 mIconCache.updateDbIcons(packagesToIgnore);
2771 }
2772
Joe Onorato36115782010-06-17 13:28:48 -04002773 private void onlyBindAllApps() {
2774 final Callbacks oldCallbacks = mCallbacks.get();
2775 if (oldCallbacks == null) {
2776 // This launcher has exited and nobody bothered to tell us. Just bail.
2777 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2778 return;
2779 }
2780
2781 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002782 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002783 final ArrayList<AppInfo> list
2784 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002785 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002786 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002787 public void run() {
2788 final long t = SystemClock.uptimeMillis();
2789 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2790 if (callbacks != null) {
2791 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002792 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002793 }
2794 if (DEBUG_LOADERS) {
2795 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2796 + (SystemClock.uptimeMillis()-t) + "ms");
2797 }
2798 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002799 };
2800 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002801 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002802 r.run();
2803 } else {
2804 mHandler.post(r);
2805 }
Joe Onorato36115782010-06-17 13:28:48 -04002806 }
2807
Winson Chung64359a52013-07-08 17:17:08 -07002808 private void loadAllApps() {
2809 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002810
Joe Onorato36115782010-06-17 13:28:48 -04002811 final Callbacks oldCallbacks = mCallbacks.get();
2812 if (oldCallbacks == null) {
2813 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002814 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002815 return;
2816 }
2817
Kenny Guyed131872014-04-30 03:02:21 +01002818 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2819
Winson Chung64359a52013-07-08 17:17:08 -07002820 // Clear the list of apps
2821 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002822 for (UserHandleCompat user : profiles) {
2823 // Query for the set of apps
2824 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002825 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002826 if (DEBUG_LOADERS) {
2827 Log.d(TAG, "getActivityList took "
2828 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2829 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2830 }
2831 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002832 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002833 if (apps == null || apps.isEmpty()) {
2834 return;
2835 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002836
Kenny Guyed131872014-04-30 03:02:21 +01002837 // Create the ApplicationInfos
2838 for (int i = 0; i < apps.size(); i++) {
2839 LauncherActivityInfoCompat app = apps.get(i);
2840 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002841 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002842 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002843
Sunny Goyal756a28a2015-04-23 17:07:55 -07002844 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2845 if (heuristic != null) {
2846 runAfterBindCompletes(new Runnable() {
2847
2848 @Override
2849 public void run() {
2850 heuristic.processUserApps(apps);
2851 }
2852 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002853 }
Winson Chung64359a52013-07-08 17:17:08 -07002854 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002855 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002856 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2857 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002858
2859 // Post callback on main thread
2860 mHandler.post(new Runnable() {
2861 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002862
Winson Chung64359a52013-07-08 17:17:08 -07002863 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002864 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002865 if (callbacks != null) {
2866 callbacks.bindAllApplications(added);
2867 if (DEBUG_LOADERS) {
2868 Log.d(TAG, "bound " + added.size() + " apps in "
2869 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2870 }
2871 } else {
2872 Log.i(TAG, "not binding apps: no Launcher activity");
2873 }
2874 }
2875 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002876 // Cleanup any data stored for a deleted user.
2877 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002878
Sunny Goyal2d648b02015-08-11 13:56:28 -07002879 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002880 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002881 Log.d(TAG, "Icons processed in "
2882 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002883 }
2884 }
2885
2886 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002887 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002888 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002889 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2890 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2891 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2892 }
Joe Onorato36115782010-06-17 13:28:48 -04002893 }
2894 }
2895
Sunny Goyal75b0f552015-05-20 21:57:06 -07002896 /**
2897 * Called when the icons for packages have been updated in the icon cache.
2898 */
2899 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2900 final Callbacks callbacks = getCallback();
2901 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2902 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2903
2904 // If any package icon has changed (app was updated while launcher was dead),
2905 // update the corresponding shortcuts.
2906 synchronized (sBgLock) {
2907 for (ItemInfo info : sBgItemsIdMap) {
2908 if (info instanceof ShortcutInfo && user.equals(info.user)
2909 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2910 ShortcutInfo si = (ShortcutInfo) info;
2911 ComponentName cn = si.getTargetComponent();
2912 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2913 si.updateIcon(mIconCache);
2914 updatedShortcuts.add(si);
2915 }
2916 }
2917 }
2918 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2919 }
2920
2921 if (!updatedShortcuts.isEmpty()) {
2922 final UserHandleCompat userFinal = user;
2923 mHandler.post(new Runnable() {
2924
2925 public void run() {
2926 Callbacks cb = getCallback();
2927 if (cb != null && callbacks == cb) {
2928 cb.bindShortcutsChanged(updatedShortcuts,
2929 new ArrayList<ShortcutInfo>(), userFinal);
2930 }
2931 }
2932 });
2933 }
2934
2935 if (!updatedApps.isEmpty()) {
2936 mHandler.post(new Runnable() {
2937
2938 public void run() {
2939 Callbacks cb = getCallback();
2940 if (cb != null && callbacks == cb) {
2941 cb.bindAppsUpdated(updatedApps);
2942 }
2943 }
2944 });
2945 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002946
2947 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002948 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002949 }
2950
Joe Onorato36115782010-06-17 13:28:48 -04002951 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002952 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002953 }
2954
Adam Cohen091440a2015-03-18 14:16:05 -07002955 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002956
2957 @Override
2958 public void onReceive(Context context, Intent intent) {
2959 synchronized (sBgLock) {
2960 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2961 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002962 final PackageManager manager = context.getPackageManager();
2963 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2964 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002965 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2966 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002967 packagesRemoved.clear();
2968 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002969 for (String pkg : entry.getValue()) {
2970 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002971 boolean packageOnSdcard = launcherApps.isAppEnabled(
2972 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2973 if (packageOnSdcard) {
2974 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2975 packagesUnavailable.add(pkg);
2976 } else {
2977 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2978 packagesRemoved.add(pkg);
2979 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002980 }
2981 }
2982 if (!packagesRemoved.isEmpty()) {
2983 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2984 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2985 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002986 if (!packagesUnavailable.isEmpty()) {
2987 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2988 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2989 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002990 }
Sunny Goyal34942622014-08-29 17:20:55 -07002991 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002992 }
2993 }
2994 }
2995
Joe Onorato36115782010-06-17 13:28:48 -04002996 private class PackageUpdatedTask implements Runnable {
2997 int mOp;
2998 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002999 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003000
3001 public static final int OP_NONE = 0;
3002 public static final int OP_ADD = 1;
3003 public static final int OP_UPDATE = 2;
3004 public static final int OP_REMOVE = 3; // uninstlled
3005 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3006
3007
Kenny Guyed131872014-04-30 03:02:21 +01003008 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003009 mOp = op;
3010 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003011 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003012 }
3013
3014 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003015 if (!mHasLoaderCompletedOnce) {
3016 // Loader has not yet run.
3017 return;
3018 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003019 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003020
3021 final String[] packages = mPackages;
3022 final int N = packages.length;
3023 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003024 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003027 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003028 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003029 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003030
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003031 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3032 if (heuristic != null) {
3033 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003034 }
Joe Onorato36115782010-06-17 13:28:48 -04003035 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003036 }
Joe Onorato36115782010-06-17 13:28:48 -04003037 case OP_UPDATE:
3038 for (int i=0; i<N; i++) {
3039 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003040 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003041 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003042 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003043 }
3044 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003045 case OP_REMOVE: {
3046 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3047 if (heuristic != null) {
3048 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003049 }
Joe Onorato36115782010-06-17 13:28:48 -04003050 for (int i=0; i<N; i++) {
3051 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003052 mIconCache.removeIconsForPkg(packages[i], mUser);
3053 }
3054 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003055 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003056 case OP_UNAVAILABLE:
3057 for (int i=0; i<N; i++) {
3058 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3059 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003060 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
3062 break;
3063 }
3064
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 ArrayList<AppInfo> added = null;
3066 ArrayList<AppInfo> modified = null;
3067 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003068
Adam Cohen487f7dd2012-06-28 18:12:10 -07003069 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003070 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003071 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003072 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003073 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003074 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003075 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003076 }
Winson Chung5d55f332012-07-16 20:45:03 -07003077 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003078 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003079 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003080 }
3081
Sunny Goyale0f58d72014-11-10 18:05:31 -08003082 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003083 if (callbacks == null) {
3084 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3085 return;
3086 }
3087
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3089 new HashMap<ComponentName, AppInfo>();
3090
Joe Onorato36115782010-06-17 13:28:48 -04003091 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003092 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003093 for (AppInfo ai : added) {
3094 addedOrUpdatedApps.put(ai.componentName, ai);
3095 }
Joe Onorato36115782010-06-17 13:28:48 -04003096 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003097
Joe Onorato36115782010-06-17 13:28:48 -04003098 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003099 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 for (AppInfo ai : modified) {
3101 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003102 }
3103
Joe Onorato36115782010-06-17 13:28:48 -04003104 mHandler.post(new Runnable() {
3105 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003106 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003107 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003108 callbacks.bindAppsUpdated(modifiedFinal);
3109 }
3110 }
3111 });
3112 }
Winson Chung83892cc2013-05-01 16:53:33 -07003113
Sunny Goyal4390ace2014-10-13 11:33:11 -07003114 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003116 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3117 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3118 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3119
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003120 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003121 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003122 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3124 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 boolean infoUpdated = false;
3126 boolean shortcutUpdated = false;
3127
3128 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003131 Bitmap icon = Utilities.createIconBitmap(
3132 si.iconResource.packageName,
3133 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003134 if (icon != null) {
3135 si.setIcon(icon);
3136 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003137 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003138 }
3139 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003140
3141 ComponentName cn = si.getTargetComponent();
3142 if (cn != null && packageSet.contains(cn.getPackageName())) {
3143 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3144
3145 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3147 // Auto install icon
3148 PackageManager pm = context.getPackageManager();
3149 ResolveInfo matched = pm.resolveActivity(
3150 new Intent(Intent.ACTION_MAIN)
3151 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3152 PackageManager.MATCH_DEFAULT_ONLY);
3153 if (matched == null) {
3154 // Try to find the best match activity.
3155 Intent intent = pm.getLaunchIntentForPackage(
3156 cn.getPackageName());
3157 if (intent != null) {
3158 cn = intent.getComponent();
3159 appInfo = addedOrUpdatedApps.get(cn);
3160 }
3161
3162 if ((intent == null) || (appInfo == null)) {
3163 removedShortcuts.add(si);
3164 continue;
3165 }
3166 si.promisedIntent = intent;
3167 }
3168 }
3169
3170 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003171 if (appInfo != null) {
3172 si.flags = appInfo.flags;
3173 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174
Sunny Goyal756adbc2015-04-16 15:20:51 -07003175 si.intent = si.promisedIntent;
3176 si.promisedIntent = null;
3177 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 infoUpdated = true;
3179 si.updateIcon(mIconCache);
3180 }
3181
3182 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3183 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3184 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003185 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003186 si.contentDescription = appInfo.contentDescription;
3187 infoUpdated = true;
3188 }
3189
3190 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3191 // Since package was just updated, the target must be available now.
3192 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3193 shortcutUpdated = true;
3194 }
3195 }
3196
3197 if (infoUpdated || shortcutUpdated) {
3198 updatedShortcuts.add(si);
3199 }
3200 if (infoUpdated) {
3201 updateItemInDatabase(context, si);
3202 }
3203 } else if (info instanceof LauncherAppWidgetInfo) {
3204 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3205 if (mUser.equals(widgetInfo.user)
3206 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3207 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003208 widgetInfo.restoreStatus &=
3209 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3210 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003211
3212 // adding this flag ensures that launcher shows 'click to setup'
3213 // if the widget has a config activity. In case there is no config
3214 // activity, it will be marked as 'restored' during bind.
3215 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3216
Sunny Goyal4390ace2014-10-13 11:33:11 -07003217 widgets.add(widgetInfo);
3218 updateItemInDatabase(context, widgetInfo);
3219 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003220 }
3221 }
3222 }
3223
Sunny Goyal4390ace2014-10-13 11:33:11 -07003224 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3225 mHandler.post(new Runnable() {
3226
3227 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003228 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003229 if (callbacks == cb && cb != null) {
3230 callbacks.bindShortcutsChanged(
3231 updatedShortcuts, removedShortcuts, mUser);
3232 }
3233 }
3234 });
3235 if (!removedShortcuts.isEmpty()) {
3236 deleteItemsFromDatabase(context, removedShortcuts);
3237 }
3238 }
3239 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003240 mHandler.post(new Runnable() {
3241 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003242 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003243 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003244 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003245 }
3246 }
3247 });
3248 }
3249 }
3250
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 final ArrayList<String> removedPackageNames =
3252 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003253 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003254 // Mark all packages in the broadcast to be removed
3255 removedPackageNames.addAll(Arrays.asList(packages));
3256 } else if (mOp == OP_UPDATE) {
3257 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003258 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003259 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003260 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003261 }
3262 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003263 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003264
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003266 final int removeReason;
3267 if (mOp == OP_UNAVAILABLE) {
3268 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3269 } else {
3270 // Remove all the components associated with this package
3271 for (String pn : removedPackageNames) {
3272 deletePackageFromDatabase(context, pn, mUser);
3273 }
3274 // Remove all the specific components
3275 for (AppInfo a : removedApps) {
3276 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3277 deleteItemsFromDatabase(context, infos);
3278 }
3279 removeReason = 0;
3280 }
3281
Winson Chungdf95eb12013-10-16 14:57:07 -07003282 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003283 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003284 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003285 mHandler.post(new Runnable() {
3286 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003287 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003288 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003289 callbacks.bindComponentsRemoved(
3290 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003291 }
3292 }
3293 });
Joe Onoratobe386092009-11-17 17:32:16 -08003294 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003295
Sunny Goyal2d648b02015-08-11 13:56:28 -07003296 // Update widgets
3297 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3298 // Always refresh for a package event on secondary user
3299 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3300
3301 // Refresh widget list, if the package already had a widget.
3302 synchronized (sBgLock) {
3303 if (sBgWidgetProviders != null) {
3304 HashSet<String> pkgSet = new HashSet<>();
3305 Collections.addAll(pkgSet, mPackages);
3306
3307 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3308 needToRefresh |= key.user.equals(mUser) &&
3309 pkgSet.contains(key.componentName.getPackageName());
3310 }
3311 }
3312 }
3313
3314 if (!needToRefresh && mOp != OP_REMOVE) {
3315 // Refresh widget list, if there is any newly added widget
3316 PackageManager pm = context.getPackageManager();
3317 for (String pkg : mPackages) {
3318 needToRefresh |= !pm.queryBroadcastReceivers(
3319 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
3320 .setPackage(pkg), 0).isEmpty();
3321 }
3322 }
3323
3324 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3325 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003326
Adam Cohen4caf2982013-08-20 18:54:31 -07003327 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003328 mHandler.post(new Runnable() {
3329 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003330 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003331 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003332 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003333 }
3334 }
3335 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
3337 }
3338
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003339 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3340 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003341 ArrayList<LauncherAppWidgetProviderInfo> results =
3342 new ArrayList<LauncherAppWidgetProviderInfo>();
3343 try {
3344 synchronized (sBgLock) {
3345 if (sBgWidgetProviders == null || refresh) {
3346 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3347 = new HashMap<>();
3348 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3349 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003350
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003351 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3352 for (AppWidgetProviderInfo pInfo : widgets) {
3353 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3354 UserHandleCompat user = wm.getUser(info);
3355 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3356 }
Robin Lee26ace122015-03-16 19:41:43 +00003357
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003358 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3359 for (CustomAppWidget widget : customWidgets) {
3360 info = new LauncherAppWidgetProviderInfo(context, widget);
3361 UserHandleCompat user = wm.getUser(info);
3362 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3363 }
3364 // Replace the global list at the very end, so that if there is an exception,
3365 // previously loaded provider list is used.
3366 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003367 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003368 results.addAll(sBgWidgetProviders.values());
3369 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003370 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003371 } catch (Exception e) {
3372 if (e.getCause() instanceof TransactionTooLargeException) {
3373 // the returned value may be incomplete and will not be refreshed until the next
3374 // time Launcher starts.
3375 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3376 // onResume is called to refresh the widget provider list.
3377 synchronized (sBgLock) {
3378 if (sBgWidgetProviders != null) {
3379 results.addAll(sBgWidgetProviders.values());
3380 }
3381 return results;
3382 }
3383 } else {
3384 throw e;
3385 }
Adam Cohen59400422014-03-05 18:07:04 -08003386 }
3387 }
3388
Robin Lee26ace122015-03-16 19:41:43 +00003389 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3390 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003391 synchronized (sBgLock) {
3392 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003393 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003394 }
Robin Lee26ace122015-03-16 19:41:43 +00003395 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003396 }
3397 }
3398
Sunny Goyal2d648b02015-08-11 13:56:28 -07003399 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003400
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003401 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003402 @Override
3403 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003404 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003405 final WidgetsModel model = mBgWidgetsModel.clone();
3406
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003407 mHandler.post(new Runnable() {
3408 @Override
3409 public void run() {
3410 Callbacks cb = getCallback();
3411 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003412 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003413 }
3414 }
3415 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003416 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003417 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3418 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003419 }
3420 });
3421 }
3422
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003423 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003424 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003425 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003426 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003427 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003428 @Thunk void updateWidgetsModel(boolean refresh) {
3429 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003430 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003431 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003432 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3433 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003434 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003435 }
3436
Adam Cohen091440a2015-03-18 14:16:05 -07003437 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003438 UserHandleCompat user) {
3439 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3440 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003441 }
Adam Cohen556f6132014-01-15 15:18:08 -08003442
Kenny Guyed131872014-04-30 03:02:21 +01003443 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3444 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003445 if (cn == null) {
3446 return false;
3447 }
Kenny Guyed131872014-04-30 03:02:21 +01003448 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3449 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003450 return false;
3451 }
Kenny Guyed131872014-04-30 03:02:21 +01003452 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003453 }
3454
Adam Cohena28b78e2014-05-20 17:03:04 -07003455 public static boolean isValidPackage(Context context, String packageName,
3456 UserHandleCompat user) {
3457 if (packageName == null) {
3458 return false;
3459 }
3460 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3461 return launcherApps.isPackageEnabledForProfile(packageName, user);
3462 }
3463
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003465 * Make an ShortcutInfo object for a restored application or shortcut item that points
3466 * to a package that is not yet installed on the system.
3467 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003468 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003469 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003470 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003471 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003472
3473 Bitmap icon = iconInfo.loadIcon(c, info, context);
3474 // the fallback icon
3475 if (icon == null) {
3476 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3477 } else {
3478 info.setIcon(icon);
3479 }
Sunny Goyal34942622014-08-29 17:20:55 -07003480
3481 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003482 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003483 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003484 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003485 }
Sunny Goyal34942622014-08-29 17:20:55 -07003486 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3487 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003488 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003489 }
Sunny Goyal34942622014-08-29 17:20:55 -07003490 } else {
3491 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3492 }
3493
Winson Chung82b016c2015-05-08 17:00:10 -07003494 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003495 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003496 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003497 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003498 return info;
3499 }
3500
3501 /**
3502 * Make an Intent object for a restored application or shortcut item that points
3503 * to the market page for the item.
3504 */
Adam Cohen091440a2015-03-18 14:16:05 -07003505 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003506 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003507 return getMarketIntent(componentName.getPackageName());
3508 }
3509
3510 static Intent getMarketIntent(String packageName) {
3511 return new Intent(Intent.ACTION_VIEW)
3512 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003513 .scheme("market")
3514 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003515 .appendQueryParameter("id", packageName)
3516 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003517 }
3518
3519 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003520 * Make an ShortcutInfo object for a shortcut that is an application.
3521 *
3522 * If c is not null, then it will be used to fill in missing data like the title and icon.
3523 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003524 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003525 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003526 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003527 if (user == null) {
3528 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003529 return null;
3530 }
3531
Kenny Guyed131872014-04-30 03:02:21 +01003532 ComponentName componentName = intent.getComponent();
3533 if (componentName == null) {
3534 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3535 return null;
3536 }
3537
3538 Intent newIntent = new Intent(intent.getAction(), null);
3539 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3540 newIntent.setComponent(componentName);
3541 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003542 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003543 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3544 return null;
3545 }
3546
3547 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003548 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003549 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3550 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3551 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003552 }
3553
Joe Onorato56d82912010-03-07 14:32:10 -05003554 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003555 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003556 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003557 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003558
Joe Onorato56d82912010-03-07 14:32:10 -05003559 // fall back to the class name of the activity
3560 if (info.title == null) {
3561 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003562 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003563
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003565 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003566 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003567 if (lai != null) {
3568 info.flags = AppInfo.initFlags(lai);
3569 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003571 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003572
Sunny Goyale2df0622015-04-24 11:27:00 -07003573 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003574 ItemInfoFilter f) {
3575 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3576 for (ItemInfo i : infos) {
3577 if (i instanceof ShortcutInfo) {
3578 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003579 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003580 if (cn != null && f.filterItem(null, info, cn)) {
3581 filtered.add(info);
3582 }
3583 } else if (i instanceof FolderInfo) {
3584 FolderInfo info = (FolderInfo) i;
3585 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003586 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003587 if (cn != null && f.filterItem(info, s, cn)) {
3588 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003589 }
3590 }
Winson Chung64359a52013-07-08 17:17:08 -07003591 } else if (i instanceof LauncherAppWidgetInfo) {
3592 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3593 ComponentName cn = info.providerName;
3594 if (cn != null && f.filterItem(null, info, cn)) {
3595 filtered.add(info);
3596 }
Winson Chung8a435102012-08-30 17:16:53 -07003597 }
3598 }
Winson Chung64359a52013-07-08 17:17:08 -07003599 return new ArrayList<ItemInfo>(filtered);
3600 }
3601
Adam Cohen091440a2015-03-18 14:16:05 -07003602 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003603 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003604 ItemInfoFilter filter = new ItemInfoFilter() {
3605 @Override
3606 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003607 if (info.user == null) {
3608 return cn.equals(cname);
3609 } else {
3610 return cn.equals(cname) && info.user.equals(user);
3611 }
Winson Chung64359a52013-07-08 17:17:08 -07003612 }
3613 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003614 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003615 }
3616
Sunny Goyal1a745e82014-10-02 15:58:31 -07003617 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003618 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003619 */
Adam Cohen091440a2015-03-18 14:16:05 -07003620 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003621 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003622 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003623 // Non-app shortcuts are only supported for current user.
3624 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003626
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003627 // TODO: If there's an explicit component and we can't install that, delete it.
3628
Winson Chung82b016c2015-05-08 17:00:10 -07003629 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003630
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003631 Bitmap icon = iconInfo.loadIcon(c, info, context);
3632 // the fallback icon
3633 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003634 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003635 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003636 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003637 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 return info;
3639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003640
Sunny Goyal2350bc92014-10-14 16:42:54 -07003641 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003642 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3643 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3644 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3645
Adam Cohend9198822011-11-22 16:42:47 -08003646 if (intent == null) {
3647 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3648 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3649 return null;
3650 }
3651
Joe Onorato0589f0f2010-02-08 13:44:00 -08003652 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003653 boolean customIcon = false;
3654 ShortcutIconResource iconResource = null;
3655
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003656 if (bitmap instanceof Bitmap) {
3657 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003658 customIcon = true;
3659 } else {
3660 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003661 if (extra instanceof ShortcutIconResource) {
3662 iconResource = (ShortcutIconResource) extra;
3663 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003664 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003665 }
3666 }
3667
Michael Jurkac9d95c52011-08-29 14:03:34 -07003668 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003669
Kenny Guyed131872014-04-30 03:02:21 +01003670 // Only support intents for current user for now. Intents sent from other
3671 // users wouldn't get here without intent forwarding anyway.
3672 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003673 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003674 icon = mIconCache.getDefaultIcon(info.user);
3675 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003676 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003677 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003678
Winson Chung82b016c2015-05-08 17:00:10 -07003679 info.title = Utilities.trim(name);
3680 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003681 info.intent = intent;
3682 info.customIcon = customIcon;
3683 info.iconResource = iconResource;
3684
3685 return info;
3686 }
3687
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003689 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 * or make a new one.
3691 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003692 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 // See if a placeholder was created for us already
3694 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003695 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003697 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 folders.put(id, folderInfo);
3699 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003700 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003701 }
3702
Joe Onoratobe386092009-11-17 17:32:16 -08003703
Sunny Goyal651077b2014-06-30 14:15:31 -07003704 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3705 return (provider != null) && (provider.provider != null)
3706 && (provider.provider.getPackageName() != null);
3707 }
3708
Joe Onoratobe386092009-11-17 17:32:16 -08003709 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003710 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003711 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3712 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3713 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3714 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003715 if (mLoaderTask != null) {
3716 mLoaderTask.dumpState();
3717 } else {
3718 Log.d(TAG, "mLoaderTask=null");
3719 }
Joe Onoratobe386092009-11-17 17:32:16 -08003720 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003721
3722 public Callbacks getCallback() {
3723 return mCallbacks != null ? mCallbacks.get() : null;
3724 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003725
3726 /**
3727 * @return {@link FolderInfo} if its already loaded.
3728 */
3729 public FolderInfo findFolderById(Long folderId) {
3730 synchronized (sBgLock) {
3731 return sBgFolders.get(folderId);
3732 }
3733 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003734
3735 /**
3736 * @return the looper for the worker thread which can be used to start background tasks.
3737 */
3738 public static Looper getWorkerLooper() {
3739 return sWorkerThread.getLooper();
3740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741}