blob: b5922c6a38c9032fdc1e2b65db8dbb8bfb61d62b [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 Goyale5bb7052015-07-27 14:36:07 -070058import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070059import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000060import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070061import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070062import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080065
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.lang.ref.WeakReference;
67import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070068import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070070import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070071import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.Collections;
73import java.util.Comparator;
74import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070075import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070076import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070078import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070079import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081/**
82 * Maintains in-memory state of the Launcher. It is expected that there should be only one
83 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070084 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 */
Kenny Guyed131872014-04-30 03:02:21 +010086public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010087 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080088 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040089 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070090 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040091
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070093
Dan Sandlerd5024042014-01-09 15:01:33 -050094 public static final int LOADER_FLAG_NONE = 0;
95 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
96 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
97
Joe Onorato36115782010-06-17 13:28:48 -040098 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050099 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800100
Adam Cohen091440a2015-03-18 14:16:05 -0700101 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800102 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final LauncherAppState mApp;
105 @Thunk final Object mLock = new Object();
106 @Thunk DeferredHandler mHandler = new DeferredHandler();
107 @Thunk LoaderTask mLoaderTask;
108 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700109 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
142 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700152 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700164 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 // sBgWorkspaceScreens is the ordered set of workspace screens.
167 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
168
Adam Cohen59400422014-03-05 18:07:04 -0800169 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000170 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800171
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700172 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700173 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
174 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700175
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176 // </ only access in worker thread >
177
Adam Cohen091440a2015-03-18 14:16:05 -0700178 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk final LauncherAppsCompat mLauncherApps;
181 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700184 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public int getCurrentWorkspaceScreen();
186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700191 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800192 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700195 public void bindAppsAdded(ArrayList<Long> newScreens,
196 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700197 ArrayList<ItemInfo> addAnimated,
198 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
201 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700204 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700205 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700206 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700207 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700208 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700210 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chung64359a52013-07-08 17:17:08 -0700213 public interface ItemInfoFilter {
214 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
215 }
216
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800218 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219
Winson Chungee055712013-07-30 14:46:24 -0700220 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700221 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400222 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
223 // resource string.
224 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
225 ProviderInfo providerInfo =
226 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
227 ProviderInfo redirectProvider =
228 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400231 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 if (mOldContentProviderExists) {
234 Log.d(TAG, "Old launcher provider exists.");
235 } else {
236 Log.d(TAG, "Old launcher provider does not exist.");
237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700241 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Kenny Guyed131872014-04-30 03:02:21 +0100244 mLauncherApps = LauncherAppsCompat.getInstance(context);
245 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 }
247
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700248 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
249 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 if (sWorkerThread.getThreadId() == Process.myTid()) {
252 // If we are on the worker thread, post onto the main handler
253 mHandler.post(r);
254 } else {
255 r.run();
256 }
257 }
258
259 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
260 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700261 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700262 if (sWorkerThread.getThreadId() == Process.myTid()) {
263 r.run();
264 } else {
265 // If we are not on the worker thread, then post to the worker handler
266 sWorker.post(r);
267 }
268 }
269
Winson Chunge43a1e72014-01-15 10:33:02 -0800270 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
271 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800272 }
273
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 public void setPackageState(final PackageInstallInfo installInfo) {
275 Runnable updateRunnable = new Runnable() {
276
277 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500278 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700279 synchronized (sBgLock) {
280 final HashSet<ItemInfo> updates = new HashSet<>();
281
282 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
283 // Ignore install success events as they are handled by Package add events.
284 return;
285 }
286
Sunny Goyale2df0622015-04-24 11:27:00 -0700287 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700288 if (info instanceof ShortcutInfo) {
289 ShortcutInfo si = (ShortcutInfo) info;
290 ComponentName cn = si.getTargetComponent();
291 if (si.isPromise() && (cn != null)
292 && installInfo.packageName.equals(cn.getPackageName())) {
293 si.setInstallProgress(installInfo.progress);
294
295 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
296 // Mark this info as broken.
297 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
298 }
299 updates.add(si);
300 }
301 }
302 }
303
304 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
305 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
306 widget.installProgress = installInfo.progress;
307 updates.add(widget);
308 }
309 }
310
311 if (!updates.isEmpty()) {
312 // Push changes to the callback.
313 Runnable r = new Runnable() {
314 public void run() {
315 Callbacks callbacks = getCallback();
316 if (callbacks != null) {
317 callbacks.bindRestoreItemsChange(updates);
318 }
319 }
320 };
321 mHandler.post(r);
322 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500323 }
324 }
325 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500327 }
328
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 /**
330 * Updates the icons and label of all pending icons for the provided package name.
331 */
332 public void updateSessionDisplayInfo(final String packageName) {
333 Runnable updateRunnable = new Runnable() {
334
335 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700336 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 synchronized (sBgLock) {
338 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
339 final UserHandleCompat user = UserHandleCompat.myUserHandle();
340
Sunny Goyale2df0622015-04-24 11:27:00 -0700341 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 if (info instanceof ShortcutInfo) {
343 ShortcutInfo si = (ShortcutInfo) info;
344 ComponentName cn = si.getTargetComponent();
345 if (si.isPromise() && (cn != null)
346 && packageName.equals(cn.getPackageName())) {
347 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
348 // For auto install apps update the icon as well as label.
349 mIconCache.getTitleAndIcon(si,
350 si.promisedIntent, user,
351 si.shouldUseLowResIcon());
352 } else {
353 // Only update the icon for restored apps.
354 si.updateIcon(mIconCache);
355 }
356 updates.add(si);
357 }
358 }
359 }
360
361 if (!updates.isEmpty()) {
362 // Push changes to the callback.
363 Runnable r = new Runnable() {
364 public void run() {
365 Callbacks callbacks = getCallback();
366 if (callbacks != null) {
367 callbacks.bindShortcutsChanged(updates,
368 new ArrayList<ShortcutInfo>(), user);
369 }
370 }
371 };
372 mHandler.post(r);
373 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700374 }
375 }
376 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700377 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700378 }
379
Adam Cohen76a47a12014-02-05 11:47:43 -0800380 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800381 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800382
383 if (allAppsApps == null) {
384 throw new RuntimeException("allAppsApps must not be null");
385 }
386 if (allAppsApps.isEmpty()) {
387 return;
388 }
389
390 // Process the newly added applications and add them to the database first
391 Runnable r = new Runnable() {
392 public void run() {
393 runOnMainThread(new Runnable() {
394 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800395 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800396 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500397 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 }
399 }
400 });
401 }
402 };
403 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700404 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800405
Sunny Goyala9116722015-04-29 13:55:58 -0700406 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800407 int[] xy, int spanX, int spanY) {
408 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
410 final int xCount = (int) profile.numColumns;
411 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 boolean[][] occupied = new boolean[xCount][yCount];
413 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700414 for (ItemInfo r : occupiedPos) {
415 int right = r.cellX + r.spanX;
416 int bottom = r.cellY + r.spanY;
417 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
418 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 occupied[x][y] = true;
420 }
421 }
422 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800423 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700424 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800425 }
426
427 /**
428 * Find a position on the screen for the given size or adds a new screen.
429 * @return screenId and the coordinates for the item.
430 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700431 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 ArrayList<Long> workspaceScreens,
434 ArrayList<Long> addedWorkspaceScreensFinal,
435 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700436 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437
Sunny Goyala9116722015-04-29 13:55:58 -0700438 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700439 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700440 synchronized (sBgLock) {
441 for (ItemInfo info : sBgItemsIdMap) {
442 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
443 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
444 if (items == null) {
445 items = new ArrayList<>();
446 screenItems.put(info.screenId, items);
447 }
448 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
452
453 // Find appropriate space for the item.
454 long screenId = 0;
455 int[] cordinates = new int[2];
456 boolean found = false;
457
458 int screenCount = workspaceScreens.size();
459 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700460 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 if (preferredScreenIndex < screenCount) {
462 screenId = workspaceScreens.get(preferredScreenIndex);
463 found = findNextAvailableIconSpaceInScreen(
464 screenItems.get(screenId), cordinates, spanX, spanY);
465 }
466
467 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700468 // Search on any of the screens starting from the first screen.
469 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800470 screenId = workspaceScreens.get(screen);
471 if (findNextAvailableIconSpaceInScreen(
472 screenItems.get(screenId), cordinates, spanX, spanY)) {
473 // We found a space for it
474 found = true;
475 break;
476 }
477 }
478 }
479
480 if (!found) {
481 // Still no position found. Add a new screen to the end.
482 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
483
484 // Save the screen id for binding in the workspace
485 workspaceScreens.add(screenId);
486 addedWorkspaceScreensFinal.add(screenId);
487
488 // If we still can't find an empty space, then God help us all!!!
489 if (!findNextAvailableIconSpaceInScreen(
490 screenItems.get(screenId), cordinates, spanX, spanY)) {
491 throw new RuntimeException("Can't find space to add the item");
492 }
493 }
494 return Pair.create(screenId, cordinates);
495 }
496
497 /**
498 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700500 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700501 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800503 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700504 return;
Winson Chung997a9232013-07-24 15:33:46 -0700505 }
Winson Chung64359a52013-07-08 17:17:08 -0700506 // Process the newly added applications and add them to the database first
507 Runnable r = new Runnable() {
508 public void run() {
509 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
510 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
511
Winson Chung76828c82013-08-19 15:43:29 -0700512 // Get the list of workspace screens. We need to append to this list and
513 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
514 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800515 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700516 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700518 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800519 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700520 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700522 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 }
Winson Chung76828c82013-08-19 15:43:29 -0700524
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700526 Pair<Long, int[]> coords = findSpaceForItem(context,
527 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 1, 1);
529 long screenId = coords.first;
530 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700531
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 ItemInfo itemInfo;
533 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
534 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700536 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700537 } else {
538 throw new RuntimeException("Unexpected info type");
539 }
Winson Chung94d67682013-09-25 16:29:40 -0700540
Winson Chung64359a52013-07-08 17:17:08 -0700541 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700543 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700544 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700545 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700547 }
548 }
549
Winson Chung76828c82013-08-19 15:43:29 -0700550 // Update the workspace screens
551 updateWorkspaceScreenOrder(context, workspaceScreens);
552
Adam Cohen76a47a12014-02-05 11:47:43 -0800553 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700554 runOnMainThread(new Runnable() {
555 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800556 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700557 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700558 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
559 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700560 if (!addedShortcutsFinal.isEmpty()) {
561 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
562 long lastScreenId = info.screenId;
563 for (ItemInfo i : addedShortcutsFinal) {
564 if (i.screenId == lastScreenId) {
565 addAnimated.add(i);
566 } else {
567 addNotAnimated.add(i);
568 }
Winson Chung997a9232013-07-24 15:33:46 -0700569 }
570 }
Winson Chungd64d1762013-08-20 14:37:16 -0700571 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800572 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700573 }
Winson Chung64359a52013-07-08 17:17:08 -0700574 }
Winson Chung997a9232013-07-24 15:33:46 -0700575 });
576 }
Winson Chung64359a52013-07-08 17:17:08 -0700577 }
578 };
579 runOnWorkerThread(r);
580 }
581
Sunny Goyald33860f2015-04-23 16:02:20 -0700582 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700583 if (sWorkerThread.getThreadId() == Process.myTid()) {
584 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
585 "main thread");
586 }
587
588 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400589 synchronized (mDeferredBindRunnables) {
590 mDeferredBindRunnables.clear();
591 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700592
593 // Remove any queued UI runnables
594 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700595 // Unbind all the workspace items
596 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700597 }
598
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700600 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700601 // Ensure that we don't use the same workspace items data structure on the main thread
602 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700603 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700604 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700605 tmpItems.addAll(sBgWorkspaceItems);
606 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 }
608 Runnable r = new Runnable() {
609 @Override
610 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700611 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612 item.unbind();
613 }
614 }
615 };
616 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700617 }
618
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 /**
620 * Adds an item to the DB if it was not created previously, or move it to a new
621 * <container, screen, cellX, cellY>
622 */
623 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700624 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 if (item.container == ItemInfo.NO_ID) {
626 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700627 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 } else {
629 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
632 }
633
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700634 static void checkItemInfoLocked(
635 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
636 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
637 if (modelItem != null && item != modelItem) {
638 // check all the data is consistent
639 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
640 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
641 ShortcutInfo shortcut = (ShortcutInfo) item;
642 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
643 modelShortcut.intent.filterEquals(shortcut.intent) &&
644 modelShortcut.id == shortcut.id &&
645 modelShortcut.itemType == shortcut.itemType &&
646 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700648 modelShortcut.cellX == shortcut.cellX &&
649 modelShortcut.cellY == shortcut.cellY &&
650 modelShortcut.spanX == shortcut.spanX &&
651 modelShortcut.spanY == shortcut.spanY &&
652 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
653 (modelShortcut.dropPos != null &&
654 shortcut.dropPos != null &&
655 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
656 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
657 // For all intents and purposes, this is the same object
658 return;
659 }
660 }
661
662 // the modelItem needs to match up perfectly with item if our model is
663 // to be consistent with the database-- for now, just require
664 // modelItem == item or the equality check above
665 String msg = "item: " + ((item != null) ? item.toString() : "null") +
666 "modelItem: " +
667 ((modelItem != null) ? modelItem.toString() : "null") +
668 "Error: ItemInfo passed to checkItemInfo doesn't match original";
669 RuntimeException e = new RuntimeException(msg);
670 if (stackTrace != null) {
671 e.setStackTrace(stackTrace);
672 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800673 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 }
675 }
676
Michael Jurka816474f2012-06-25 14:49:02 -0700677 static void checkItemInfo(final ItemInfo item) {
678 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
679 final long itemId = item.id;
680 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 public void run() {
682 synchronized (sBgLock) {
683 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700684 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700685 }
686 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700687 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700688 }
689
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
691 final ItemInfo item, final String callingFunction) {
692 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700693 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694 final ContentResolver cr = context.getContentResolver();
695
Adam Cohen487f7dd2012-06-28 18:12:10 -0700696 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700697 Runnable r = new Runnable() {
698 public void run() {
699 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 updateItemArrays(item, itemId, stackTrace);
701 }
702 };
703 runOnWorkerThread(r);
704 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
707 final ArrayList<ItemInfo> items, final String callingFunction) {
708 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700709
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
711 Runnable r = new Runnable() {
712 public void run() {
713 ArrayList<ContentProviderOperation> ops =
714 new ArrayList<ContentProviderOperation>();
715 int count = items.size();
716 for (int i = 0; i < count; i++) {
717 ItemInfo item = items.get(i);
718 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700719 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700720 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
723 updateItemArrays(item, itemId, stackTrace);
724
725 }
726 try {
727 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
728 } catch (Exception e) {
729 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 }
731 }
732 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700733 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700734 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700735
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
737 // Lock on mBgLock *after* the db operation
738 synchronized (sBgLock) {
739 checkItemInfoLocked(itemId, item, stackTrace);
740
741 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
742 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
743 // Item is in a folder, make sure this folder exists
744 if (!sBgFolders.containsKey(item.container)) {
745 // An items container is being set to a that of an item which is not in
746 // the list of Folders.
747 String msg = "item: " + item + " container being set to: " +
748 item.container + ", not in the list of folders";
749 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 }
751 }
752
753 // Items are added/removed from the corresponding FolderInfo elsewhere, such
754 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
755 // that are on the desktop, as appropriate
756 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800757 if (modelItem != null &&
758 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
759 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700760 switch (modelItem.itemType) {
761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
763 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
764 if (!sBgWorkspaceItems.contains(modelItem)) {
765 sBgWorkspaceItems.add(modelItem);
766 }
767 break;
768 default:
769 break;
770 }
771 } else {
772 sBgWorkspaceItems.remove(modelItem);
773 }
774 }
775 }
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700779 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700780 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 item.cellX = cellX;
784 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700785
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 // We store hotseat items in canonical form which is this orientation invariant position
787 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700791 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
795 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700797 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
798 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800799 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801
Michael Jurkac9d95c52011-08-29 14:03:34 -0700802 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700803 }
804
805 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
807 * cellX, cellY have already been updated on the ItemInfos.
808 */
809 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
810 final long container, final int screen) {
811
812 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
813 int count = items.size();
814
815 for (int i = 0; i < count; i++) {
816 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 item.container = container;
818
819 // We store hotseat items in canonical form which is this orientation invariant position
820 // in the hotseat
821 if (context instanceof Launcher && screen < 0 &&
822 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 item.cellY);
825 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700827 }
828
829 final ContentValues values = new ContentValues();
830 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
831 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
832 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800833 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835
836 contentValues.add(values);
837 }
838 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
839 }
840
841 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700842 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800843 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700844 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700846 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800847 item.cellX = cellX;
848 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 item.spanX = spanX;
850 item.spanY = spanY;
851
852 // We store hotseat items in canonical form which is this orientation invariant position
853 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 }
Adam Cohend4844c32011-02-18 19:25:06 -0800860
Adam Cohend4844c32011-02-18 19:25:06 -0800861 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800862 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
864 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800865 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
867 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800869
Michael Jurka816474f2012-06-25 14:49:02 -0700870 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872
873 /**
874 * Update an item to the database in a specified container.
875 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700876 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100878 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800880 }
881
Sunny Goyal756a28a2015-04-23 17:07:55 -0700882 private void assertWorkspaceLoaded() {
Sunny Goyal639e9062015-08-19 19:17:06 -0700883 if (LauncherAppState.isDogfoodBuild()) {
884 synchronized (mLock) {
885 if (!mHasLoaderCompletedOnce ||
886 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
887 throw new RuntimeException("Trying to add shortcut while loader is running");
888 }
889 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700890 }
891 }
892
Adam Cohend4844c32011-02-18 19:25:06 -0800893 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700894 * Returns true if the shortcuts already exists on the workspace. This must be called after
895 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700897 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
898 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700899 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 if (intent.getComponent() != null) {
901 // If component is not null, an intent with null package will produce
902 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700903 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700905 intentWithPkg = intent.toUri(0);
906 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
909 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700910 }
911 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 intentWithPkg = intent.toUri(0);
913 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915
916 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700917 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700918 if (item instanceof ShortcutInfo) {
919 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700920 Intent targetIntent = info.promisedIntent == null
921 ? info.intent : info.promisedIntent;
922 if (targetIntent != null && info.user.equals(user)) {
923 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700924 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
925 return true;
926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 }
928 }
929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700932 }
933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 /**
935 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
936 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700937 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 final ContentResolver cr = context.getContentResolver();
939 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
940 "_id=? and (itemType=? or itemType=?)",
941 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700942 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700943
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 try {
945 if (c.moveToFirst()) {
946 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
947 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
948 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
949 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
950 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
951 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700952 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 FolderInfo folderInfo = null;
955 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700956 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
957 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700959 }
960
Sunny Goyala508e4f2015-05-21 09:33:57 -0700961 // Do not trim the folder label, as is was set by the user.
962 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 folderInfo.id = id;
964 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700966 folderInfo.cellX = c.getInt(cellXIndex);
967 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700968 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969
970 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 } finally {
973 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700974 }
975
976 return null;
977 }
978
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 /**
980 * Add an item to the database in a specified container. Sets the container, screen, cellX and
981 * cellY fields of the item. Also assigns an ID to the item.
982 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700983 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700984 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 item.cellX = cellX;
987 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700988 // We store hotseat items in canonical form which is this orientation invariant position
989 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
997 final ContentValues values = new ContentValues();
998 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100999 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
Michael Jurka414300a2013-08-27 15:42:35 +02001001 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001003
Jason Monk8e19cf22014-03-20 15:06:57 -04001004 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001005 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001007 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001010 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001011 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 sBgItemsIdMap.put(item.id, item);
1013 switch (item.itemType) {
1014 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1015 sBgFolders.put(item.id, (FolderInfo) item);
1016 // Fall through
1017 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1018 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1019 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1020 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1021 sBgWorkspaceItems.add(item);
1022 } else {
1023 if (!sBgFolders.containsKey(item.container)) {
1024 // Adding an item to a folder that doesn't exist.
1025 String msg = "adding item: " + item + " to a folder that " +
1026 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001027 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001029 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 break;
1031 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1032 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1033 break;
1034 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 }
1036 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001037 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001039 }
1040
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001042 * Creates a new unique child id, for a given cell span across all layouts.
1043 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001044 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001045 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001047 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001048 }
1049
Sunny Goyal34942622014-08-29 17:20:55 -07001050 private static ArrayList<ItemInfo> getItemsByPackageName(
1051 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001052 ItemInfoFilter filter = new ItemInfoFilter() {
1053 @Override
1054 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1055 return cn.getPackageName().equals(pn) && info.user.equals(user);
1056 }
1057 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001058 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001059 }
1060
1061 /**
1062 * Removes all the items from the database corresponding to the specified package.
1063 */
1064 static void deletePackageFromDatabase(Context context, final String pn,
1065 final UserHandleCompat user) {
1066 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001067 }
1068
1069 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001070 * Removes the specified item from the database
1071 * @param context
1072 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001074 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001075 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1076 items.add(item);
1077 deleteItemsFromDatabase(context, items);
1078 }
1079
1080 /**
1081 * Removes the specified items from the database
1082 * @param context
1083 * @param item
1084 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001085 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001087 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001088 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001089 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001090 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001091 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001092
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001093 // Lock on mBgLock *after* the db operation
1094 synchronized (sBgLock) {
1095 switch (item.itemType) {
1096 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1097 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001098 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001099 if (info.container == item.id) {
1100 // We are deleting a folder which still contains items that
1101 // think they are contained by that folder.
1102 String msg = "deleting a folder (" + item + ") which still " +
1103 "contains items (" + info + ")";
1104 Log.e(TAG, msg);
1105 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001107 sBgWorkspaceItems.remove(item);
1108 break;
1109 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1110 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1111 sBgWorkspaceItems.remove(item);
1112 break;
1113 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1114 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1115 break;
1116 }
1117 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001119 }
1120 }
Michael Jurka83df1882011-08-31 20:59:26 -07001121 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 }
1124
1125 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001126 * Update the order of the workspace screens in the database. The array list contains
1127 * a list of screen ids in the order that they should appear.
1128 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001129 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001130 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001131 final ContentResolver cr = context.getContentResolver();
1132 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1133
1134 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001135 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 while (iter.hasNext()) {
1137 long id = iter.next();
1138 if (id < 0) {
1139 iter.remove();
1140 }
1141 }
1142
1143 Runnable r = new Runnable() {
1144 @Override
1145 public void run() {
Yura085c8532014-02-11 15:15:29 +00001146 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001147 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001148 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001149 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 for (int i = 0; i < count; i++) {
1151 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001152 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1154 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001155 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001156 }
Yura085c8532014-02-11 15:15:29 +00001157
1158 try {
1159 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1160 } catch (Exception ex) {
1161 throw new RuntimeException(ex);
1162 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001163
Winson Chungba9c37f2013-08-30 14:11:37 -07001164 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001165 sBgWorkspaceScreens.clear();
1166 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001167 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 }
1169 };
1170 runOnWorkerThread(r);
1171 }
1172
1173 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174 * Remove the contents of the specified folder from the database
1175 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001176 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 final ContentResolver cr = context.getContentResolver();
1178
Michael Jurkac9d95c52011-08-29 14:03:34 -07001179 Runnable r = new Runnable() {
1180 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001181 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001183 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 sBgItemsIdMap.remove(info.id);
1185 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 sBgWorkspaceItems.remove(info);
1187 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001188
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001189 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001190 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001192 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 for (ItemInfo childInfo : info.contents) {
1194 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001196 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001197 }
1198 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
1201
1202 /**
1203 * Set this as the current Launcher activity object for the loader.
1204 */
1205 public void initialize(Callbacks callbacks) {
1206 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001207 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1208 // workspace to prevent leaking Launcher activities on orientation change.
1209 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 mCallbacks = new WeakReference<Callbacks>(callbacks);
1211 }
1212 }
1213
Kenny Guyed131872014-04-30 03:02:21 +01001214 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001215 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001216 int op = PackageUpdatedTask.OP_UPDATE;
1217 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1218 user));
1219 }
1220
1221 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001222 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001223 int op = PackageUpdatedTask.OP_REMOVE;
1224 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1225 user));
1226 }
1227
1228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001230 int op = PackageUpdatedTask.OP_ADD;
1231 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1232 user));
1233 }
1234
1235 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001236 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001237 boolean replacing) {
1238 if (!replacing) {
1239 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1240 user));
1241 if (mAppsCanBeOnRemoveableStorage) {
1242 // Only rebind if we support removable storage. It catches the
1243 // case where
1244 // apps on the external sd card need to be reloaded
1245 startLoaderFromBackground();
1246 }
1247 } else {
1248 // If we are replacing then just update the packages in the list
1249 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1250 packageNames, user));
1251 }
1252 }
1253
1254 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001255 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001256 boolean replacing) {
1257 if (!replacing) {
1258 enqueuePackageUpdated(new PackageUpdatedTask(
1259 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1260 user));
1261 }
Kenny Guyed131872014-04-30 03:02:21 +01001262 }
1263
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001264 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1266 * ACTION_PACKAGE_CHANGED.
1267 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001268 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001269 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001270 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001271
Joe Onorato36115782010-06-17 13:28:48 -04001272 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001273 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001274 // If we have changed locale we need to clear out the labels in all apps/workspace.
1275 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001276 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001277 Callbacks callbacks = getCallback();
1278 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001279 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001280 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001281 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1282 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001283 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001284 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001285 }
1286 }
1287
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001288 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001289 resetLoadedState(true, true);
1290
Reena Lee93f824a2011-09-23 17:20:28 -07001291 // Do this here because if the launcher activity is running it will be restarted.
1292 // If it's not running startLoaderFromBackground will merely tell it that it needs
1293 // to reload.
1294 startLoaderFromBackground();
1295 }
1296
Winson Chungf0c6ae02012-03-21 16:10:31 -07001297 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1298 synchronized (mLock) {
1299 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1300 // mWorkspaceLoaded to true later
1301 stopLoaderLocked();
1302 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1303 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1304 }
1305 }
1306
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001307 /**
1308 * When the launcher is in the background, it's possible for it to miss paired
1309 * configuration changes. So whenever we trigger the loader from the background
1310 * tell the launcher that it needs to re-run the loader when it comes back instead
1311 * of doing it now.
1312 */
1313 public void startLoaderFromBackground() {
1314 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001315 Callbacks callbacks = getCallback();
1316 if (callbacks != null) {
1317 // Only actually run the loader if they're not paused.
1318 if (!callbacks.setLoadOnResume()) {
1319 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001320 }
1321 }
1322 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001323 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001326
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001327 /**
1328 * If there is already a loader task running, tell it to stop.
1329 */
1330 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001331 LoaderTask oldTask = mLoaderTask;
1332 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001333 oldTask.stopLocked();
1334 }
Reena Lee93f824a2011-09-23 17:20:28 -07001335 }
1336
Adam Cohen1a85c582014-09-30 09:48:49 -07001337 public boolean isCurrentCallbacks(Callbacks callbacks) {
1338 return (mCallbacks != null && mCallbacks.get() == callbacks);
1339 }
1340
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001341 public void startLoader(int synchronousBindPage) {
1342 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001343 }
1344
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001345 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001346 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1347 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001348 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001349 // Clear any deferred bind-runnables from the synchronized load process
1350 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001351 synchronized (mDeferredBindRunnables) {
1352 mDeferredBindRunnables.clear();
1353 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354
Joe Onorato36115782010-06-17 13:28:48 -04001355 // Don't bother to start the thread if we know it's not going to do anything
1356 if (mCallbacks != null && mCallbacks.get() != null) {
1357 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001358 stopLoaderLocked();
1359 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001360 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001361 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1363 } else {
1364 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1365 sWorker.post(mLoaderTask);
1366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
1369 }
1370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 void bindRemainingSynchronousPages() {
1372 // Post the remaining side pages to be loaded
1373 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001374 Runnable[] deferredBindRunnables = null;
1375 synchronized (mDeferredBindRunnables) {
1376 deferredBindRunnables = mDeferredBindRunnables.toArray(
1377 new Runnable[mDeferredBindRunnables.size()]);
1378 mDeferredBindRunnables.clear();
1379 }
1380 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001381 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 }
1384 }
1385
Joe Onorato36115782010-06-17 13:28:48 -04001386 public void stopLoader() {
1387 synchronized (mLock) {
1388 if (mLoaderTask != null) {
1389 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 }
1391 }
Joe Onorato36115782010-06-17 13:28:48 -04001392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001394 /**
1395 * Loads the workspace screen ids in an ordered list.
1396 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001397 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001398 final ContentResolver contentResolver = context.getContentResolver();
1399 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001400
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001401 // Get screens ordered by rank.
1402 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1403 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1404 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001405 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001406 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001407 while (sc.moveToNext()) {
1408 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001409 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001410 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001411 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1412 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001413 }
1414 }
1415 } finally {
1416 sc.close();
1417 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001418 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001419 }
1420
Michael Jurkac57b7a82011-08-09 22:02:20 -07001421 public boolean isAllAppsLoaded() {
1422 return mAllAppsLoaded;
1423 }
1424
Joe Onorato36115782010-06-17 13:28:48 -04001425 /**
1426 * Runnable for the thread that loads the contents of the launcher:
1427 * - workspace icons
1428 * - widgets
1429 * - all apps icons
1430 */
1431 private class LoaderTask implements Runnable {
1432 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001433 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001434 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001435 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001436 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001437
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001438 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001439 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001440 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 }
1442
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001443 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001444 mIsLoadingAndBindingWorkspace = true;
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001447 if (DEBUG_LOADERS) {
1448 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001450
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001451 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001452 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001453 synchronized (LoaderTask.this) {
1454 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001455 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001456 }
1457 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 }
1459 }
1460
Joe Onorato36115782010-06-17 13:28:48 -04001461 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001462 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001463 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001464
Joe Onorato36115782010-06-17 13:28:48 -04001465 private void waitForIdle() {
1466 // Wait until the either we're stopped or the other threads are done.
1467 // This way we don't start loading all apps until the workspace has settled
1468 // down.
1469 synchronized (LoaderTask.this) {
1470 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001471
Joe Onorato36115782010-06-17 13:28:48 -04001472 mHandler.postIdle(new Runnable() {
1473 public void run() {
1474 synchronized (LoaderTask.this) {
1475 mLoadAndBindStepFinished = true;
1476 if (DEBUG_LOADERS) {
1477 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001478 }
Joe Onorato36115782010-06-17 13:28:48 -04001479 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001480 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001481 }
Joe Onorato36115782010-06-17 13:28:48 -04001482 });
1483
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001484 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001485 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001486 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1487 // wait no longer than 1sec at a time
1488 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001489 } catch (InterruptedException ex) {
1490 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001491 }
1492 }
Joe Onorato36115782010-06-17 13:28:48 -04001493 if (DEBUG_LOADERS) {
1494 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001495 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001496 + "ms for previous step to finish binding");
1497 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001498 }
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001500
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001501 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001502 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001503 // Ensure that we have a valid page index to load synchronously
1504 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1505 "valid page index");
1506 }
1507 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1508 // Ensure that we don't try and bind a specified page when the pages have not been
1509 // loaded already (we should load everything asynchronously in that case)
1510 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1511 }
1512 synchronized (mLock) {
1513 if (mIsLoaderTaskRunning) {
1514 // Ensure that we are never running the background loading at this point since
1515 // we also touch the background collections
1516 throw new RuntimeException("Error! Background loading is already running");
1517 }
1518 }
1519
1520 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1521 // data structures, we can't allow any other thread to touch that data, but because
1522 // this call is synchronous, we can get away with not locking).
1523
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001524 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001525 // operations from the previous activity. We need to ensure that all queued operations
1526 // are executed before any synchronous binding work is done.
1527 mHandler.flush();
1528
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 // Divide the set of loaded items into those that we are binding synchronously, and
1530 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001531 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1533 // arise from that.
1534 onlyBindAllApps();
1535 }
1536
Joe Onorato36115782010-06-17 13:28:48 -04001537 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001538 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001539 if (mStopped) {
1540 return;
1541 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 mIsLoaderTaskRunning = true;
1543 }
Joe Onorato36115782010-06-17 13:28:48 -04001544 // Optimize for end-user experience: if the Launcher is up and // running with the
1545 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1546 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001547 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001548 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001549 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001550
Joe Onorato36115782010-06-17 13:28:48 -04001551 if (mStopped) {
1552 break keep_running;
1553 }
1554
Joe Onorato36115782010-06-17 13:28:48 -04001555 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001556
1557 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001558 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1559 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561
Joe Onorato36115782010-06-17 13:28:48 -04001562 // Clear out this reference, otherwise we end up holding it until all of the
1563 // callback runnables are done.
1564 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565
Joe Onorato36115782010-06-17 13:28:48 -04001566 synchronized (mLock) {
1567 // If we are still the last one to be scheduled, remove ourselves.
1568 if (mLoaderTask == this) {
1569 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001572 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001573 }
Joe Onorato36115782010-06-17 13:28:48 -04001574 }
1575
1576 public void stopLocked() {
1577 synchronized (LoaderTask.this) {
1578 mStopped = true;
1579 this.notify();
1580 }
1581 }
1582
1583 /**
1584 * Gets the callbacks object. If we've been stopped, or if the launcher object
1585 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1586 * object that was around when the deferred message was scheduled, and if there's
1587 * a new Callbacks object around then also return null. This will save us from
1588 * calling onto it with data that will be ignored.
1589 */
1590 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1591 synchronized (mLock) {
1592 if (mStopped) {
1593 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001594 }
Joe Onorato36115782010-06-17 13:28:48 -04001595
1596 if (mCallbacks == null) {
1597 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599
1600 final Callbacks callbacks = mCallbacks.get();
1601 if (callbacks != oldCallbacks) {
1602 return null;
1603 }
1604 if (callbacks == null) {
1605 Log.w(TAG, "no mCallbacks");
1606 return null;
1607 }
1608
1609 return callbacks;
1610 }
1611 }
1612
1613 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001614 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1615 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001616 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001617 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001618 final int countX = profile.numColumns;
1619 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001620
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001622 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001623 // Return early if we detect that an item is under the hotseat button
1624 if (mCallbacks == null ||
1625 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001626 Log.e(TAG, "Error loading shortcut into hotseat " + item
1627 + " into position (" + item.screenId + ":" + item.cellX + ","
1628 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001629 return false;
1630 }
1631
Dan Sandler295ae182013-12-10 16:05:47 -05001632 final ItemInfo[][] hotseatItems =
1633 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1634
Adam Cohen2e6da152015-05-06 11:42:25 -07001635 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001636 Log.e(TAG, "Error loading shortcut " + item
1637 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001638 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001639 + ")");
1640 return false;
1641 }
1642
Dan Sandler295ae182013-12-10 16:05:47 -05001643 if (hotseatItems != null) {
1644 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 Log.e(TAG, "Error loading shortcut into hotseat " + item
1646 + " into position (" + item.screenId + ":" + item.cellX + ","
1647 + item.cellY + ") occupied by "
1648 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1649 [(int) item.screenId][0]);
1650 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001651 } else {
1652 hotseatItems[(int) item.screenId][0] = item;
1653 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001654 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001655 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001656 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001657 items[(int) item.screenId][0] = item;
1658 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001659 return true;
1660 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001661 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1662 if (!workspaceScreens.contains((Long) item.screenId)) {
1663 // The item has an invalid screen id.
1664 return false;
1665 }
1666 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001667 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001668 return true;
1669 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001670
Adam Cohendcd297f2013-06-18 13:13:40 -07001671 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001672 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001673 occupied.put(item.screenId, items);
1674 }
1675
Dan Sandler295ae182013-12-10 16:05:47 -05001676 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001677 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1678 item.cellX < 0 || item.cellY < 0 ||
1679 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1680 Log.e(TAG, "Error loading shortcut " + item
1681 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1682 + item.cellX + "," + item.cellY
1683 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1684 return false;
1685 }
1686
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001687 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001688 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1689 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001690 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001691 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001693 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001694 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001695 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001696 return false;
1697 }
1698 }
1699 }
1700 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1701 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001702 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001703 }
1704 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001705
Joe Onorato36115782010-06-17 13:28:48 -04001706 return true;
1707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708
Winson Chungba9c37f2013-08-30 14:11:37 -07001709 /** Clears all the sBg data structures */
1710 private void clearSBgDataStructures() {
1711 synchronized (sBgLock) {
1712 sBgWorkspaceItems.clear();
1713 sBgAppWidgets.clear();
1714 sBgFolders.clear();
1715 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001716 sBgWorkspaceScreens.clear();
1717 }
1718 }
1719
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001720 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001721 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722
Joe Onorato36115782010-06-17 13:28:48 -04001723 final Context context = mContext;
1724 final ContentResolver contentResolver = context.getContentResolver();
1725 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001726 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001727 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1728 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001729 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001730
Winson Chung892c74d2013-08-22 16:15:50 -07001731 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001732 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001733 int countX = profile.numColumns;
1734 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001735
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001736 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001737 long migrationStartTime = System.currentTimeMillis();
1738 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001739 try {
1740 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1741 // Clear the flags before starting the task, so that we do not run the task
1742 // again, in case there was an uncaught error.
1743 MigrateFromRestoreTask.clearFlags(mContext);
1744 task.execute();
1745 } catch (Exception e) {
1746 Log.e(TAG, "Error during grid migration", e);
1747
1748 // Clear workspace.
1749 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1750 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001751 Log.v(TAG, "Workspace migration completed in "
1752 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001753 }
1754
Dan Sandlerd5024042014-01-09 15:01:33 -05001755 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1756 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1757 LauncherAppState.getLauncherProvider().deleteDatabase();
1758 }
1759
1760 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1761 // append the user's Launcher2 shortcuts
1762 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1763 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1764 } else {
1765 // Make sure the default workspace is loaded
1766 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001767 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001768 }
Adam Cohene25af792013-06-06 23:08:25 -07001769
Winson Chung2abf94d2012-07-18 18:16:38 -07001770 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001771 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001772 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001773 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001774 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001775
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001777 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001778 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001779 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001780 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001781
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001782 // +1 for the hotseat (it can be larger than the workspace)
1783 // Load workspace in reverse order to ensure that latest items are loaded first (and
1784 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001785 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 try {
1788 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1789 final int intentIndex = c.getColumnIndexOrThrow
1790 (LauncherSettings.Favorites.INTENT);
1791 final int titleIndex = c.getColumnIndexOrThrow
1792 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 final int containerIndex = c.getColumnIndexOrThrow(
1794 LauncherSettings.Favorites.CONTAINER);
1795 final int itemTypeIndex = c.getColumnIndexOrThrow(
1796 LauncherSettings.Favorites.ITEM_TYPE);
1797 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001799 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 final int screenIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.SCREEN);
1803 final int cellXIndex = c.getColumnIndexOrThrow
1804 (LauncherSettings.Favorites.CELLX);
1805 final int cellYIndex = c.getColumnIndexOrThrow
1806 (LauncherSettings.Favorites.CELLY);
1807 final int spanXIndex = c.getColumnIndexOrThrow
1808 (LauncherSettings.Favorites.SPANX);
1809 final int spanYIndex = c.getColumnIndexOrThrow(
1810 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001811 final int rankIndex = c.getColumnIndexOrThrow(
1812 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001813 final int restoredIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001815 final int profileIdIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001817 final int optionsIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001819 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001820
Sunny Goyal7f834d22015-04-21 10:10:23 -07001821 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1822 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1823 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1824 }
1825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 ShortcutInfo info;
1827 String intentDescription;
1828 LauncherAppWidgetInfo appWidgetInfo;
1829 int container;
1830 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001831 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001833 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 while (!mStopped && c.moveToNext()) {
1836 try {
1837 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001838 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001839 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001840 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 switch (itemType) {
1843 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1844 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001845 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001847 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001848 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001849 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001850 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001851 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001852 if (user == null) {
1853 // User has been deleted remove the item.
1854 itemsToRemove.add(id);
1855 continue;
1856 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 try {
1858 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001859 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001860 if (cn != null && cn.getPackageName() != null) {
1861 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1862 cn.getPackageName(), user);
1863 boolean validComponent = validPkg &&
1864 launcherApps.isActivityEnabledForProfile(cn, user);
1865
1866 if (validComponent) {
1867 if (restored) {
1868 // no special handling necessary for this item
1869 restoredRows.add(id);
1870 restored = false;
1871 }
1872 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001873 intent = null;
1874 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1875 // We allow auto install apps to have their intent
1876 // updated after an install.
1877 intent = manager.getLaunchIntentForPackage(
1878 cn.getPackageName());
1879 if (intent != null) {
1880 ContentValues values = new ContentValues();
1881 values.put(LauncherSettings.Favorites.INTENT,
1882 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001883 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001884 }
1885 }
1886
1887 if (intent == null) {
1888 // The app is installed but the component is no
1889 // longer available.
1890 Launcher.addDumpLog(TAG,
1891 "Invalid component removed: " + cn, true);
1892 itemsToRemove.add(id);
1893 continue;
1894 } else {
1895 // no special handling necessary for this item
1896 restoredRows.add(id);
1897 restored = false;
1898 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001899 } else if (restored) {
1900 // Package is not yet available but might be
1901 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001902 Launcher.addDumpLog(TAG,
1903 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001904
1905 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1906 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001907 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001908 // App restore has started. Update the flag
1909 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1910 ContentValues values = new ContentValues();
1911 values.put(LauncherSettings.Favorites.RESTORED,
1912 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 updateItem(id, values);
1914 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1915 // This is a common app. Try to replace this.
1916 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1917 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1918 if (parser.findDefaultApp()) {
1919 // Default app found. Replace it.
1920 intent = parser.parsedIntent;
1921 cn = intent.getComponent();
1922 ContentValues values = parser.parsedValues;
1923 values.put(LauncherSettings.Favorites.RESTORED, 0);
1924 updateItem(id, values);
1925 restored = false;
1926 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001927
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001928 } else if (REMOVE_UNRESTORED_ICONS) {
1929 Launcher.addDumpLog(TAG,
1930 "Unrestored package removed: " + cn, true);
1931 itemsToRemove.add(id);
1932 continue;
1933 }
Sunny Goyal94485362014-09-18 16:13:58 -07001934 } else if (REMOVE_UNRESTORED_ICONS) {
1935 Launcher.addDumpLog(TAG,
1936 "Unrestored package removed: " + cn, true);
1937 itemsToRemove.add(id);
1938 continue;
1939 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001940 } else if (launcherApps.isAppEnabled(
1941 manager, cn.getPackageName(),
1942 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1943 // Package is present but not available.
1944 allowMissingTarget = true;
1945 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1946 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001947 // SdCard is not ready yet. Package might get available,
1948 // once it is ready.
1949 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1950 + " (check again later)", true);
1951 HashSet<String> pkgs = sPendingPackages.get(user);
1952 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001953 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001954 sPendingPackages.put(user, pkgs);
1955 }
1956 pkgs.add(cn.getPackageName());
1957 allowMissingTarget = true;
1958 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001959
1960 } else {
1961 // Do not wait for external media load anymore.
1962 // Log the invalid package, and remove it
1963 Launcher.addDumpLog(TAG,
1964 "Invalid package removed: " + cn, true);
1965 itemsToRemove.add(id);
1966 continue;
Winson Chungee055712013-07-30 14:46:24 -07001967 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001968 } else if (cn == null) {
1969 // For shortcuts with no component, keep them as they are
1970 restoredRows.add(id);
1971 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001972 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001974 Launcher.addDumpLog(TAG,
1975 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001976 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 continue;
1978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001979
Sunny Goyal34b65272015-03-11 16:56:52 -07001980 boolean useLowResIcon = container >= 0 &&
1981 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1982
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001983 if (itemReplaced) {
1984 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001985 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001986 cursorIconInfo.iconIndex, titleIndex,
1987 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001988 } else {
1989 // Don't replace items for other profiles.
1990 itemsToRemove.add(id);
1991 continue;
1992 }
1993 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001994 if (user.equals(UserHandleCompat.myUserHandle())) {
1995 Launcher.addDumpLog(TAG,
1996 "constructing info for partially restored package",
1997 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001998 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001999 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002000 intent = getRestoredItemIntent(c, context, intent);
2001 } else {
2002 // Don't restore items for other profiles.
2003 itemsToRemove.add(id);
2004 continue;
2005 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002006 } else if (itemType ==
2007 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002008 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002009 cursorIconInfo.iconIndex, titleIndex,
2010 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002012 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002013
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 // App shortcuts that used to be automatically added to Launcher
2015 // didn't always have the correct intent flags set, so do that
2016 // here
2017 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002018 intent.getCategories() != null &&
2019 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002020 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 intent.addFlags(
2022 Intent.FLAG_ACTIVITY_NEW_TASK |
2023 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2024 }
Michael Jurka96879562012-03-22 05:54:33 -07002025 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002026
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002028 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002031 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 info.cellX = c.getInt(cellXIndex);
2033 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002034 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002035 info.spanX = 1;
2036 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002037 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002038 if (info.promisedIntent != null) {
2039 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2040 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002041 info.isDisabled = disabledState;
2042 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2043 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2044 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002045
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002047 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002048 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 break;
2050 }
2051
Sunny Goyal756adbc2015-04-16 15:20:51 -07002052 if (restored) {
2053 ComponentName cn = info.getTargetComponent();
2054 if (cn != null) {
2055 Integer progress = installingPkgs.get(cn.getPackageName());
2056 if (progress != null) {
2057 info.setInstallProgress(progress);
2058 } else {
2059 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2060 }
2061 }
2062 }
2063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 switch (container) {
2065 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2066 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2067 sBgWorkspaceItems.add(info);
2068 break;
2069 default:
2070 // Item is in a user folder
2071 FolderInfo folderInfo =
2072 findOrMakeFolder(sBgFolders, container);
2073 folderInfo.add(info);
2074 break;
2075 }
2076 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002077 } else {
2078 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 }
2080 break;
2081
2082 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2083 id = c.getLong(idIndex);
2084 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2085
Sunny Goyala508e4f2015-05-21 09:33:57 -07002086 // Do not trim the folder label, as is was set by the user.
2087 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002090 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 folderInfo.cellX = c.getInt(cellXIndex);
2092 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002093 folderInfo.spanX = 1;
2094 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002095 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096
Daniel Sandler8802e962010-05-26 16:28:16 -04002097 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002098 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002099 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002100 break;
2101 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002102
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2105 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2106 sBgWorkspaceItems.add(folderInfo);
2107 break;
Joe Onorato36115782010-06-17 13:28:48 -04002108 }
Joe Onorato17a89222011-02-08 17:26:11 -08002109
Chris Wrenf4d08112014-01-16 18:13:56 -05002110 if (restored) {
2111 // no special handling required for restored folders
2112 restoredRows.add(id);
2113 }
2114
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2116 sBgFolders.put(folderInfo.id, folderInfo);
2117 break;
2118
2119 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002120 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002122 boolean customWidget = itemType ==
2123 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2124
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002126 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002127 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002128 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002129 user = allUsers.get(serialNumber);
2130 if (user == null) {
2131 itemsToRemove.add(id);
2132 continue;
2133 }
2134
Sunny Goyalff572272014-07-23 13:58:07 -07002135 final ComponentName component =
2136 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002137
Sunny Goyal651077b2014-06-30 14:15:31 -07002138 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002139 final boolean isIdValid = (restoreStatus &
2140 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002141 final boolean wasProviderReady = (restoreStatus &
2142 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002143
Adam Cohen59400422014-03-05 18:07:04 -08002144 final LauncherAppWidgetProviderInfo provider =
2145 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002146 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002147 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002148
2149 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002150 if (!isSafeMode && !customWidget &&
2151 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002152 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002153 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002154
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002156 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 itemsToRemove.add(id);
2158 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002159 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002160 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2161 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002162
Sunny Goyal53f96722015-07-13 19:54:53 -07002163 // The provider is available. So the widget is either
2164 // available or not available. We do not need to track
2165 // any future restore updates.
2166 int status = restoreStatus &
2167 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002168 if (!wasProviderReady) {
2169 // If provider was not previously ready, update the
2170 // status and UI flag.
2171
2172 // Id would be valid only if the widget restore broadcast was received.
2173 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002174 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002175 } else {
2176 status &= ~LauncherAppWidgetInfo
2177 .FLAG_PROVIDER_NOT_READY;
2178 }
2179 }
2180 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 } else {
2182 Log.v(TAG, "Widget restore pending id=" + id
2183 + " appWidgetId=" + appWidgetId
2184 + " status =" + restoreStatus);
2185 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002186 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002187 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002188 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002189
2190 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2191 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002192 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002193 // App restore has started. Update the flag
2194 appWidgetInfo.restoreStatus |=
2195 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002196 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002197 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002198 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002199 itemsToRemove.add(id);
2200 continue;
2201 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002202
2203 appWidgetInfo.installProgress =
2204 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002205 }
Sunny Goyalff572272014-07-23 13:58:07 -07002206
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 appWidgetInfo.cellX = c.getInt(cellXIndex);
2210 appWidgetInfo.cellY = c.getInt(cellYIndex);
2211 appWidgetInfo.spanX = c.getInt(spanXIndex);
2212 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002213 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002214
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2216 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2217 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002218 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2219 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 continue;
2221 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002223 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002225 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002226 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 break;
2228 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002229
Adam Cohen59400422014-03-05 18:07:04 -08002230 if (!customWidget) {
2231 String providerName =
2232 appWidgetInfo.providerName.flattenToString();
2233 if (!providerName.equals(savedProvider) ||
2234 (appWidgetInfo.restoreStatus != restoreStatus)) {
2235 ContentValues values = new ContentValues();
2236 values.put(
2237 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2238 providerName);
2239 values.put(LauncherSettings.Favorites.RESTORED,
2240 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002241 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002242 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002243 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2245 sBgAppWidgets.add(appWidgetInfo);
2246 }
Joe Onorato36115782010-06-17 13:28:48 -04002247 break;
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002250 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002251 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 }
2253 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002254 if (c != null) {
2255 c.close();
2256 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 }
2258
Winson Chungba9c37f2013-08-30 14:11:37 -07002259 // Break early if we've stopped loading
2260 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002261 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002262 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002263 }
2264
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002267 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2268 Utilities.createDbSelectionQuery(
2269 LauncherSettings.Favorites._ID, itemsToRemove), null);
2270 if (DEBUG_LOADERS) {
2271 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2272 LauncherSettings.Favorites._ID, itemsToRemove));
2273 }
2274
2275 // Remove any empty folder
2276 for (long folderId : LauncherAppState.getLauncherProvider()
2277 .deleteEmptyFolders()) {
2278 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2279 sBgFolders.remove(folderId);
2280 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002281 }
2282 }
2283
Sunny Goyal317698b2015-07-29 11:45:41 -07002284 // Sort all the folder items and make sure the first 3 items are high resolution.
2285 for (FolderInfo folder : sBgFolders) {
2286 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2287 int pos = 0;
2288 for (ShortcutInfo info : folder.contents) {
2289 if (info.usingLowResIcon) {
2290 info.updateIcon(mIconCache, false);
2291 }
2292 pos ++;
2293 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2294 break;
2295 }
2296 }
2297 }
2298
Chris Wrenf4d08112014-01-16 18:13:56 -05002299 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002300 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002301 ContentValues values = new ContentValues();
2302 values.put(LauncherSettings.Favorites.RESTORED, 0);
2303 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2304 Utilities.createDbSelectionQuery(
2305 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002306 }
2307
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002308 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2309 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002310 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002311 null, sWorker);
2312 }
2313
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002314 // Remove any empty screens
2315 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002316 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002317 long screenId = item.screenId;
2318 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2319 unusedScreens.contains(screenId)) {
2320 unusedScreens.remove(screenId);
2321 }
2322 }
2323
2324 // If there are any empty screens remove them, and update.
2325 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002326 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 }
2329
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 if (DEBUG_LOADERS) {
2331 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2332 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002334 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002336
Sunny Goyale2df0622015-04-24 11:27:00 -07002337 for (int i = 0; i < nScreens; i++) {
2338 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002339 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 line += " | ";
2341 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002342 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002343 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002344 if (x < screen.length && y < screen[x].length) {
2345 line += (screen[x][y] != null) ? "#" : ".";
2346 } else {
2347 line += "!";
2348 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 }
Joe Onorato36115782010-06-17 13:28:48 -04002350 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Adam Cohene25af792013-06-06 23:08:25 -07002355 }
2356
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002357 /**
2358 * Partially updates the item without any notification. Must be called on the worker thread.
2359 */
2360 private void updateItem(long itemId, ContentValues update) {
2361 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002362 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002363 update,
2364 BaseColumns._ID + "= ?",
2365 new String[]{Long.toString(itemId)});
2366 }
2367
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 /** Filters the set of items who are directly or indirectly (via another container) on the
2369 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002370 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 ArrayList<ItemInfo> allWorkspaceItems,
2372 ArrayList<ItemInfo> currentScreenItems,
2373 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002374 // Purge any null ItemInfos
2375 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2376 while (iter.hasNext()) {
2377 ItemInfo i = iter.next();
2378 if (i == null) {
2379 iter.remove();
2380 }
2381 }
2382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 // Order the set of items by their containers first, this allows use to walk through the
2384 // list sequentially, build up a list of containers that are in the specified screen,
2385 // as well as all items in those containers.
2386 Set<Long> itemsOnScreen = new HashSet<Long>();
2387 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2388 @Override
2389 public int compare(ItemInfo lhs, ItemInfo rhs) {
2390 return (int) (lhs.container - rhs.container);
2391 }
2392 });
2393 for (ItemInfo info : allWorkspaceItems) {
2394 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002395 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 currentScreenItems.add(info);
2397 itemsOnScreen.add(info.id);
2398 } else {
2399 otherScreenItems.add(info);
2400 }
2401 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2402 currentScreenItems.add(info);
2403 itemsOnScreen.add(info.id);
2404 } else {
2405 if (itemsOnScreen.contains(info.container)) {
2406 currentScreenItems.add(info);
2407 itemsOnScreen.add(info.id);
2408 } else {
2409 otherScreenItems.add(info);
2410 }
2411 }
2412 }
2413 }
2414
2415 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002416 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 ArrayList<LauncherAppWidgetInfo> appWidgets,
2418 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2419 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420
2421 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002422 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002424 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 currentScreenWidgets.add(widget);
2426 } else {
2427 otherScreenWidgets.add(widget);
2428 }
2429 }
2430 }
2431
2432 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002433 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002434 LongArrayMap<ItemInfo> itemsIdMap,
2435 LongArrayMap<FolderInfo> folders,
2436 LongArrayMap<FolderInfo> currentScreenFolders,
2437 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438
Sunny Goyale2df0622015-04-24 11:27:00 -07002439 int total = folders.size();
2440 for (int i = 0; i < total; i++) {
2441 long id = folders.keyAt(i);
2442 FolderInfo folder = folders.valueAt(i);
2443
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002445 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002447 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 currentScreenFolders.put(id, folder);
2449 } else {
2450 otherScreenFolders.put(id, folder);
2451 }
2452 }
2453 }
2454
2455 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2456 * right) */
2457 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002458 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002459 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 // XXX: review this
2461 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002462 @Override
2463 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002464 int cellCountX = (int) profile.numColumns;
2465 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002466 int screenOffset = cellCountX * cellCountY;
2467 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002469 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002471 rhs.cellY * cellCountX + rhs.cellX);
2472 return (int) (lr - rr);
2473 }
2474 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002476
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2478 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002479 final Runnable r = new Runnable() {
2480 @Override
2481 public void run() {
2482 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2483 if (callbacks != null) {
2484 callbacks.bindScreens(orderedScreens);
2485 }
2486 }
2487 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002488 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002489 }
2490
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2492 final ArrayList<ItemInfo> workspaceItems,
2493 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002494 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 final boolean postOnMainThread = (deferredBindRunnables != null);
2498
2499 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002500 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002502 final int start = i;
2503 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 final Runnable r = new Runnable() {
2505 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002506 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002507 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002509 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2510 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 }
2512 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 };
2514 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002515 synchronized (deferredBindRunnables) {
2516 deferredBindRunnables.add(r);
2517 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002519 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 }
Joe Onorato36115782010-06-17 13:28:48 -04002521 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522
2523 // Bind the folders
2524 if (!folders.isEmpty()) {
2525 final Runnable r = new Runnable() {
2526 public void run() {
2527 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2528 if (callbacks != null) {
2529 callbacks.bindFolders(folders);
2530 }
2531 }
2532 };
2533 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002534 synchronized (deferredBindRunnables) {
2535 deferredBindRunnables.add(r);
2536 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002538 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002539 }
2540 }
2541
2542 // Bind the widgets, one at a time
2543 N = appWidgets.size();
2544 for (int i = 0; i < N; i++) {
2545 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2546 final Runnable r = new Runnable() {
2547 public void run() {
2548 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2549 if (callbacks != null) {
2550 callbacks.bindAppWidget(widget);
2551 }
2552 }
2553 };
2554 if (postOnMainThread) {
2555 deferredBindRunnables.add(r);
2556 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002557 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 }
2559 }
2560 }
2561
2562 /**
2563 * Binds all loaded data to actual views on the main thread.
2564 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002565 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566 final long t = SystemClock.uptimeMillis();
2567 Runnable r;
2568
2569 // Don't use these two variables in any of the callback runnables.
2570 // Otherwise we hold a reference to them.
2571 final Callbacks oldCallbacks = mCallbacks.get();
2572 if (oldCallbacks == null) {
2573 // This launcher has exited and nobody bothered to tell us. Just bail.
2574 Log.w(TAG, "LoaderTask running with no launcher");
2575 return;
2576 }
2577
Winson Chung9b9fb962013-11-15 15:39:34 -08002578 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002579 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2580 ArrayList<LauncherAppWidgetInfo> appWidgets =
2581 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002582 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002583
2584 final LongArrayMap<FolderInfo> folders;
2585 final LongArrayMap<ItemInfo> itemsIdMap;
2586
Winson Chung2abf94d2012-07-18 18:16:38 -07002587 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002588 workspaceItems.addAll(sBgWorkspaceItems);
2589 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002590 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002591
2592 folders = sBgFolders.clone();
2593 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 }
2595
Derek Prothro7aff3992013-12-10 14:00:37 -05002596 final boolean isLoadingSynchronously =
2597 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002598 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002599 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002600 if (currScreen >= orderedScreenIds.size()) {
2601 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002602 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002603 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002604 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002605 final long currentScreenId = currentScreen < 0
2606 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002607
2608 // Load all the items that are on the current page first (and in the process, unbind
2609 // all the existing workspace items before we call startBinding() below.
2610 unbindWorkspaceItemsOnMainThread();
2611
2612 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002613 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2614 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2615 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2616 new ArrayList<LauncherAppWidgetInfo>();
2617 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2618 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002619 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2620 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621
Winson Chung9b9fb962013-11-15 15:39:34 -08002622 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002624 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002626 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 otherFolders);
2628 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2629 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2630
2631 // Tell the workspace that we're about to start binding items
2632 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002633 public void run() {
2634 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2635 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002636 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002637 }
2638 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002639 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002640 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641
Adam Cohendcd297f2013-06-18 13:13:40 -07002642 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2643
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002644 // Load items on the current page
2645 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2646 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002647 if (isLoadingSynchronously) {
2648 r = new Runnable() {
2649 public void run() {
2650 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002651 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002652 callbacks.onPageBoundSynchronously(currentScreen);
2653 }
2654 }
2655 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002656 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002657 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002658
Winson Chung4a2afa32012-07-19 14:53:05 -07002659 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2660 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002661 synchronized (mDeferredBindRunnables) {
2662 mDeferredBindRunnables.clear();
2663 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002665 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666
2667 // Tell the workspace that we're done 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) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002672 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002673 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002674
Sunny Goyal639e9062015-08-19 19:17:06 -07002675 mIsLoadingAndBindingWorkspace = false;
2676
2677 // Run all the bind complete runnables after workspace is bound.
2678 if (!mBindCompleteRunnables.isEmpty()) {
2679 synchronized (mBindCompleteRunnables) {
2680 for (final Runnable r : mBindCompleteRunnables) {
2681 runOnWorkerThread(r);
2682 }
2683 mBindCompleteRunnables.clear();
2684 }
2685 }
2686
Winson Chung98e030b2012-05-07 16:01:11 -07002687 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002688 if (DEBUG_LOADERS) {
2689 Log.d(TAG, "bound workspace in "
2690 + (SystemClock.uptimeMillis()-t) + "ms");
2691 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002692
Joe Onorato36115782010-06-17 13:28:48 -04002693 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002695 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002696 synchronized (mDeferredBindRunnables) {
2697 mDeferredBindRunnables.add(r);
2698 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002699 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002700 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002701 }
Joe Onorato36115782010-06-17 13:28:48 -04002702 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002703
Joe Onorato36115782010-06-17 13:28:48 -04002704 private void loadAndBindAllApps() {
2705 if (DEBUG_LOADERS) {
2706 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2707 }
2708 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002709 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002710 synchronized (LoaderTask.this) {
2711 if (mStopped) {
2712 return;
2713 }
2714 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002715 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002716 synchronized (LoaderTask.this) {
2717 if (mStopped) {
2718 return;
2719 }
2720 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002721 }
Joe Onorato36115782010-06-17 13:28:48 -04002722 } else {
2723 onlyBindAllApps();
2724 }
2725 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002726
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002727 private void updateIconCache() {
2728 // Ignore packages which have a promise icon.
2729 HashSet<String> packagesToIgnore = new HashSet<>();
2730 synchronized (sBgLock) {
2731 for (ItemInfo info : sBgItemsIdMap) {
2732 if (info instanceof ShortcutInfo) {
2733 ShortcutInfo si = (ShortcutInfo) info;
2734 if (si.isPromise() && si.getTargetComponent() != null) {
2735 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2736 }
2737 } else if (info instanceof LauncherAppWidgetInfo) {
2738 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2739 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2740 packagesToIgnore.add(lawi.providerName.getPackageName());
2741 }
2742 }
2743 }
2744 }
2745 mIconCache.updateDbIcons(packagesToIgnore);
2746 }
2747
Joe Onorato36115782010-06-17 13:28:48 -04002748 private void onlyBindAllApps() {
2749 final Callbacks oldCallbacks = mCallbacks.get();
2750 if (oldCallbacks == null) {
2751 // This launcher has exited and nobody bothered to tell us. Just bail.
2752 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2753 return;
2754 }
2755
2756 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002757 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002758 final ArrayList<AppInfo> list
2759 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002760 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002761 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002762 public void run() {
2763 final long t = SystemClock.uptimeMillis();
2764 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2765 if (callbacks != null) {
2766 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002767 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002768 }
2769 if (DEBUG_LOADERS) {
2770 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2771 + (SystemClock.uptimeMillis()-t) + "ms");
2772 }
2773 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002774 };
2775 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002776 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002777 r.run();
2778 } else {
2779 mHandler.post(r);
2780 }
Joe Onorato36115782010-06-17 13:28:48 -04002781 }
2782
Winson Chung64359a52013-07-08 17:17:08 -07002783 private void loadAllApps() {
2784 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002785
Joe Onorato36115782010-06-17 13:28:48 -04002786 final Callbacks oldCallbacks = mCallbacks.get();
2787 if (oldCallbacks == null) {
2788 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002789 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002790 return;
2791 }
2792
Kenny Guyed131872014-04-30 03:02:21 +01002793 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2794
Winson Chung64359a52013-07-08 17:17:08 -07002795 // Clear the list of apps
2796 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002797 for (UserHandleCompat user : profiles) {
2798 // Query for the set of apps
2799 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002800 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002801 if (DEBUG_LOADERS) {
2802 Log.d(TAG, "getActivityList took "
2803 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2804 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2805 }
2806 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002807 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002808 if (apps == null || apps.isEmpty()) {
2809 return;
2810 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002811
Kenny Guyed131872014-04-30 03:02:21 +01002812 // Create the ApplicationInfos
2813 for (int i = 0; i < apps.size(); i++) {
2814 LauncherActivityInfoCompat app = apps.get(i);
2815 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002816 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002817 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002818
Sunny Goyal756a28a2015-04-23 17:07:55 -07002819 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2820 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002821 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002822
2823 @Override
2824 public void run() {
2825 heuristic.processUserApps(apps);
2826 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002827 };
2828 runOnMainThread(new Runnable() {
2829
2830 @Override
2831 public void run() {
2832 // Check isLoadingWorkspace on the UI thread, as it is updated on
2833 // the UI thread.
2834 if (mIsLoadingAndBindingWorkspace) {
2835 synchronized (mBindCompleteRunnables) {
2836 mBindCompleteRunnables.add(r);
2837 }
2838 } else {
2839 runOnWorkerThread(r);
2840 }
2841 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002842 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002843 }
Winson Chung64359a52013-07-08 17:17:08 -07002844 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002845 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002846 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2847 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002848
2849 // Post callback on main thread
2850 mHandler.post(new Runnable() {
2851 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002852
Winson Chung64359a52013-07-08 17:17:08 -07002853 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002854 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002855 if (callbacks != null) {
2856 callbacks.bindAllApplications(added);
2857 if (DEBUG_LOADERS) {
2858 Log.d(TAG, "bound " + added.size() + " apps in "
2859 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2860 }
2861 } else {
2862 Log.i(TAG, "not binding apps: no Launcher activity");
2863 }
2864 }
2865 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002866 // Cleanup any data stored for a deleted user.
2867 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002868
Sunny Goyal2d648b02015-08-11 13:56:28 -07002869 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002870 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002871 Log.d(TAG, "Icons processed in "
2872 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002873 }
2874 }
2875
2876 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002877 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002878 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002879 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2880 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2881 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2882 }
Joe Onorato36115782010-06-17 13:28:48 -04002883 }
2884 }
2885
Sunny Goyal75b0f552015-05-20 21:57:06 -07002886 /**
2887 * Called when the icons for packages have been updated in the icon cache.
2888 */
2889 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2890 final Callbacks callbacks = getCallback();
2891 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2892 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2893
2894 // If any package icon has changed (app was updated while launcher was dead),
2895 // update the corresponding shortcuts.
2896 synchronized (sBgLock) {
2897 for (ItemInfo info : sBgItemsIdMap) {
2898 if (info instanceof ShortcutInfo && user.equals(info.user)
2899 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2900 ShortcutInfo si = (ShortcutInfo) info;
2901 ComponentName cn = si.getTargetComponent();
2902 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2903 si.updateIcon(mIconCache);
2904 updatedShortcuts.add(si);
2905 }
2906 }
2907 }
2908 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2909 }
2910
2911 if (!updatedShortcuts.isEmpty()) {
2912 final UserHandleCompat userFinal = user;
2913 mHandler.post(new Runnable() {
2914
2915 public void run() {
2916 Callbacks cb = getCallback();
2917 if (cb != null && callbacks == cb) {
2918 cb.bindShortcutsChanged(updatedShortcuts,
2919 new ArrayList<ShortcutInfo>(), userFinal);
2920 }
2921 }
2922 });
2923 }
2924
2925 if (!updatedApps.isEmpty()) {
2926 mHandler.post(new Runnable() {
2927
2928 public void run() {
2929 Callbacks cb = getCallback();
2930 if (cb != null && callbacks == cb) {
2931 cb.bindAppsUpdated(updatedApps);
2932 }
2933 }
2934 });
2935 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002936
2937 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002938 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002939 }
2940
Joe Onorato36115782010-06-17 13:28:48 -04002941 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002942 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002943 }
2944
Adam Cohen091440a2015-03-18 14:16:05 -07002945 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002946
2947 @Override
2948 public void onReceive(Context context, Intent intent) {
2949 synchronized (sBgLock) {
2950 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2951 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002952 final PackageManager manager = context.getPackageManager();
2953 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2954 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002955 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2956 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002957 packagesRemoved.clear();
2958 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 for (String pkg : entry.getValue()) {
2960 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002961 boolean packageOnSdcard = launcherApps.isAppEnabled(
2962 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2963 if (packageOnSdcard) {
2964 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2965 packagesUnavailable.add(pkg);
2966 } else {
2967 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2968 packagesRemoved.add(pkg);
2969 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002970 }
2971 }
2972 if (!packagesRemoved.isEmpty()) {
2973 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2974 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2975 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002976 if (!packagesUnavailable.isEmpty()) {
2977 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2978 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2979 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002980 }
Sunny Goyal34942622014-08-29 17:20:55 -07002981 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002982 }
2983 }
2984 }
2985
Joe Onorato36115782010-06-17 13:28:48 -04002986 private class PackageUpdatedTask implements Runnable {
2987 int mOp;
2988 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002989 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002990
2991 public static final int OP_NONE = 0;
2992 public static final int OP_ADD = 1;
2993 public static final int OP_UPDATE = 2;
2994 public static final int OP_REMOVE = 3; // uninstlled
2995 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2996
2997
Kenny Guyed131872014-04-30 03:02:21 +01002998 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002999 mOp = op;
3000 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003001 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003002 }
3003
3004 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003005 if (!mHasLoaderCompletedOnce) {
3006 // Loader has not yet run.
3007 return;
3008 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003009 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003010
3011 final String[] packages = mPackages;
3012 final int N = packages.length;
3013 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003014 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003015 for (int i=0; i<N; i++) {
3016 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003017 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003018 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003019 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003020
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003021 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3022 if (heuristic != null) {
3023 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024 }
Joe Onorato36115782010-06-17 13:28:48 -04003025 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003026 }
Joe Onorato36115782010-06-17 13:28:48 -04003027 case OP_UPDATE:
3028 for (int i=0; i<N; i++) {
3029 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003030 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003031 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003032 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003033 }
3034 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003035 case OP_REMOVE: {
3036 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3037 if (heuristic != null) {
3038 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003039 }
Joe Onorato36115782010-06-17 13:28:48 -04003040 for (int i=0; i<N; i++) {
3041 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003042 mIconCache.removeIconsForPkg(packages[i], mUser);
3043 }
3044 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003045 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003046 case OP_UNAVAILABLE:
3047 for (int i=0; i<N; i++) {
3048 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3049 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003050 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003051 }
3052 break;
3053 }
3054
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003055 ArrayList<AppInfo> added = null;
3056 ArrayList<AppInfo> modified = null;
3057 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003058
Adam Cohen487f7dd2012-06-28 18:12:10 -07003059 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003060 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003061 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003062 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003063 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003065 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003066 }
Winson Chung5d55f332012-07-16 20:45:03 -07003067 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003068 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003069 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003070 }
3071
Sunny Goyale0f58d72014-11-10 18:05:31 -08003072 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003073 if (callbacks == null) {
3074 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3075 return;
3076 }
3077
Sunny Goyal4390ace2014-10-13 11:33:11 -07003078 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3079 new HashMap<ComponentName, AppInfo>();
3080
Joe Onorato36115782010-06-17 13:28:48 -04003081 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003082 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 for (AppInfo ai : added) {
3084 addedOrUpdatedApps.put(ai.componentName, ai);
3085 }
Joe Onorato36115782010-06-17 13:28:48 -04003086 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003087
Joe Onorato36115782010-06-17 13:28:48 -04003088 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003089 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 for (AppInfo ai : modified) {
3091 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003092 }
3093
Joe Onorato36115782010-06-17 13:28:48 -04003094 mHandler.post(new Runnable() {
3095 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003096 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003097 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003098 callbacks.bindAppsUpdated(modifiedFinal);
3099 }
3100 }
3101 });
3102 }
Winson Chung83892cc2013-05-01 16:53:33 -07003103
Sunny Goyal4390ace2014-10-13 11:33:11 -07003104 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003105 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003106 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3107 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3108 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3109
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003110 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003111 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003112 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003113 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3114 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 boolean infoUpdated = false;
3116 boolean shortcutUpdated = false;
3117
3118 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003119 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003121 Bitmap icon = Utilities.createIconBitmap(
3122 si.iconResource.packageName,
3123 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003124 if (icon != null) {
3125 si.setIcon(icon);
3126 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003127 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 }
3129 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130
3131 ComponentName cn = si.getTargetComponent();
3132 if (cn != null && packageSet.contains(cn.getPackageName())) {
3133 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3134
3135 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3137 // Auto install icon
3138 PackageManager pm = context.getPackageManager();
3139 ResolveInfo matched = pm.resolveActivity(
3140 new Intent(Intent.ACTION_MAIN)
3141 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3142 PackageManager.MATCH_DEFAULT_ONLY);
3143 if (matched == null) {
3144 // Try to find the best match activity.
3145 Intent intent = pm.getLaunchIntentForPackage(
3146 cn.getPackageName());
3147 if (intent != null) {
3148 cn = intent.getComponent();
3149 appInfo = addedOrUpdatedApps.get(cn);
3150 }
3151
3152 if ((intent == null) || (appInfo == null)) {
3153 removedShortcuts.add(si);
3154 continue;
3155 }
3156 si.promisedIntent = intent;
3157 }
3158 }
3159
3160 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003161 if (appInfo != null) {
3162 si.flags = appInfo.flags;
3163 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164
Sunny Goyal756adbc2015-04-16 15:20:51 -07003165 si.intent = si.promisedIntent;
3166 si.promisedIntent = null;
3167 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 infoUpdated = true;
3169 si.updateIcon(mIconCache);
3170 }
3171
3172 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3173 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3174 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003175 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176 si.contentDescription = appInfo.contentDescription;
3177 infoUpdated = true;
3178 }
3179
3180 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3181 // Since package was just updated, the target must be available now.
3182 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3183 shortcutUpdated = true;
3184 }
3185 }
3186
3187 if (infoUpdated || shortcutUpdated) {
3188 updatedShortcuts.add(si);
3189 }
3190 if (infoUpdated) {
3191 updateItemInDatabase(context, si);
3192 }
3193 } else if (info instanceof LauncherAppWidgetInfo) {
3194 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3195 if (mUser.equals(widgetInfo.user)
3196 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3197 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003198 widgetInfo.restoreStatus &=
3199 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3200 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003201
3202 // adding this flag ensures that launcher shows 'click to setup'
3203 // if the widget has a config activity. In case there is no config
3204 // activity, it will be marked as 'restored' during bind.
3205 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3206
Sunny Goyal4390ace2014-10-13 11:33:11 -07003207 widgets.add(widgetInfo);
3208 updateItemInDatabase(context, widgetInfo);
3209 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003210 }
3211 }
3212 }
3213
Sunny Goyal4390ace2014-10-13 11:33:11 -07003214 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3215 mHandler.post(new Runnable() {
3216
3217 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003218 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 if (callbacks == cb && cb != null) {
3220 callbacks.bindShortcutsChanged(
3221 updatedShortcuts, removedShortcuts, mUser);
3222 }
3223 }
3224 });
3225 if (!removedShortcuts.isEmpty()) {
3226 deleteItemsFromDatabase(context, removedShortcuts);
3227 }
3228 }
3229 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003230 mHandler.post(new Runnable() {
3231 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003232 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003233 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003234 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003235 }
3236 }
3237 });
3238 }
3239 }
3240
Winson Chungdf95eb12013-10-16 14:57:07 -07003241 final ArrayList<String> removedPackageNames =
3242 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003243 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003244 // Mark all packages in the broadcast to be removed
3245 removedPackageNames.addAll(Arrays.asList(packages));
3246 } else if (mOp == OP_UPDATE) {
3247 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003249 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003251 }
3252 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003254
Winson Chungdf95eb12013-10-16 14:57:07 -07003255 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003256 final int removeReason;
3257 if (mOp == OP_UNAVAILABLE) {
3258 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3259 } else {
3260 // Remove all the components associated with this package
3261 for (String pn : removedPackageNames) {
3262 deletePackageFromDatabase(context, pn, mUser);
3263 }
3264 // Remove all the specific components
3265 for (AppInfo a : removedApps) {
3266 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3267 deleteItemsFromDatabase(context, infos);
3268 }
3269 removeReason = 0;
3270 }
3271
Winson Chungdf95eb12013-10-16 14:57:07 -07003272 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003273 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003274 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003275 mHandler.post(new Runnable() {
3276 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003277 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003278 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003279 callbacks.bindComponentsRemoved(
3280 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003281 }
3282 }
3283 });
Joe Onoratobe386092009-11-17 17:32:16 -08003284 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003285
Sunny Goyal2d648b02015-08-11 13:56:28 -07003286 // Update widgets
3287 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3288 // Always refresh for a package event on secondary user
3289 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3290
3291 // Refresh widget list, if the package already had a widget.
3292 synchronized (sBgLock) {
3293 if (sBgWidgetProviders != null) {
3294 HashSet<String> pkgSet = new HashSet<>();
3295 Collections.addAll(pkgSet, mPackages);
3296
3297 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3298 needToRefresh |= key.user.equals(mUser) &&
3299 pkgSet.contains(key.componentName.getPackageName());
3300 }
3301 }
3302 }
3303
3304 if (!needToRefresh && mOp != OP_REMOVE) {
3305 // Refresh widget list, if there is any newly added widget
3306 PackageManager pm = context.getPackageManager();
3307 for (String pkg : mPackages) {
3308 needToRefresh |= !pm.queryBroadcastReceivers(
3309 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
3310 .setPackage(pkg), 0).isEmpty();
3311 }
3312 }
3313
3314 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3315 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003316
Adam Cohen4caf2982013-08-20 18:54:31 -07003317 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003318 mHandler.post(new Runnable() {
3319 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003320 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003321 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003322 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003323 }
3324 }
3325 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326 }
3327 }
3328
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003329 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3330 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003331 ArrayList<LauncherAppWidgetProviderInfo> results =
3332 new ArrayList<LauncherAppWidgetProviderInfo>();
3333 try {
3334 synchronized (sBgLock) {
3335 if (sBgWidgetProviders == null || refresh) {
3336 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3337 = new HashMap<>();
3338 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3339 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003340
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003341 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3342 for (AppWidgetProviderInfo pInfo : widgets) {
3343 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3344 UserHandleCompat user = wm.getUser(info);
3345 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3346 }
Robin Lee26ace122015-03-16 19:41:43 +00003347
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003348 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3349 for (CustomAppWidget widget : customWidgets) {
3350 info = new LauncherAppWidgetProviderInfo(context, widget);
3351 UserHandleCompat user = wm.getUser(info);
3352 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3353 }
3354 // Replace the global list at the very end, so that if there is an exception,
3355 // previously loaded provider list is used.
3356 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003357 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003358 results.addAll(sBgWidgetProviders.values());
3359 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003360 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003361 } catch (Exception e) {
3362 if (e.getCause() instanceof TransactionTooLargeException) {
3363 // the returned value may be incomplete and will not be refreshed until the next
3364 // time Launcher starts.
3365 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3366 // onResume is called to refresh the widget provider list.
3367 synchronized (sBgLock) {
3368 if (sBgWidgetProviders != null) {
3369 results.addAll(sBgWidgetProviders.values());
3370 }
3371 return results;
3372 }
3373 } else {
3374 throw e;
3375 }
Adam Cohen59400422014-03-05 18:07:04 -08003376 }
3377 }
3378
Robin Lee26ace122015-03-16 19:41:43 +00003379 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3380 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003381 synchronized (sBgLock) {
3382 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003383 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003384 }
Robin Lee26ace122015-03-16 19:41:43 +00003385 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003386 }
3387 }
3388
Sunny Goyal2d648b02015-08-11 13:56:28 -07003389 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003390
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003391 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003392 @Override
3393 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003394 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003395 final WidgetsModel model = mBgWidgetsModel.clone();
3396
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003397 mHandler.post(new Runnable() {
3398 @Override
3399 public void run() {
3400 Callbacks cb = getCallback();
3401 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003402 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003403 }
3404 }
3405 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003406 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003407 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3408 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003409 }
3410 });
3411 }
3412
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003413 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003414 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003415 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003416 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003417 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003418 @Thunk void updateWidgetsModel(boolean refresh) {
3419 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003420 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003421 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003422 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3423 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003424 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003425 }
3426
Adam Cohen091440a2015-03-18 14:16:05 -07003427 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003428 UserHandleCompat user) {
3429 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3430 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003431 }
Adam Cohen556f6132014-01-15 15:18:08 -08003432
Kenny Guyed131872014-04-30 03:02:21 +01003433 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3434 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003435 if (cn == null) {
3436 return false;
3437 }
Kenny Guyed131872014-04-30 03:02:21 +01003438 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3439 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003440 return false;
3441 }
Kenny Guyed131872014-04-30 03:02:21 +01003442 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003443 }
3444
Adam Cohena28b78e2014-05-20 17:03:04 -07003445 public static boolean isValidPackage(Context context, String packageName,
3446 UserHandleCompat user) {
3447 if (packageName == null) {
3448 return false;
3449 }
3450 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3451 return launcherApps.isPackageEnabledForProfile(packageName, user);
3452 }
3453
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003455 * Make an ShortcutInfo object for a restored application or shortcut item that points
3456 * to a package that is not yet installed on the system.
3457 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003458 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003459 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003460 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003461 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003462
3463 Bitmap icon = iconInfo.loadIcon(c, info, context);
3464 // the fallback icon
3465 if (icon == null) {
3466 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3467 } else {
3468 info.setIcon(icon);
3469 }
Sunny Goyal34942622014-08-29 17:20:55 -07003470
3471 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003472 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003473 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003474 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003475 }
Sunny Goyal34942622014-08-29 17:20:55 -07003476 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3477 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003478 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003479 }
Sunny Goyal34942622014-08-29 17:20:55 -07003480 } else {
3481 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3482 }
3483
Winson Chung82b016c2015-05-08 17:00:10 -07003484 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003485 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003486 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003487 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003488 return info;
3489 }
3490
3491 /**
3492 * Make an Intent object for a restored application or shortcut item that points
3493 * to the market page for the item.
3494 */
Adam Cohen091440a2015-03-18 14:16:05 -07003495 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003496 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003497 return getMarketIntent(componentName.getPackageName());
3498 }
3499
3500 static Intent getMarketIntent(String packageName) {
3501 return new Intent(Intent.ACTION_VIEW)
3502 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003503 .scheme("market")
3504 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003505 .appendQueryParameter("id", packageName)
3506 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003507 }
3508
3509 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003510 * Make an ShortcutInfo object for a shortcut that is an application.
3511 *
3512 * If c is not null, then it will be used to fill in missing data like the title and icon.
3513 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003514 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003515 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003516 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003517 if (user == null) {
3518 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003519 return null;
3520 }
3521
Kenny Guyed131872014-04-30 03:02:21 +01003522 ComponentName componentName = intent.getComponent();
3523 if (componentName == null) {
3524 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3525 return null;
3526 }
3527
3528 Intent newIntent = new Intent(intent.getAction(), null);
3529 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3530 newIntent.setComponent(componentName);
3531 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003532 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003533 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3534 return null;
3535 }
3536
3537 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003538 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003539 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3540 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3541 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003542 }
3543
Joe Onorato56d82912010-03-07 14:32:10 -05003544 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003545 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003546 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003547 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003548
Joe Onorato56d82912010-03-07 14:32:10 -05003549 // fall back to the class name of the activity
3550 if (info.title == null) {
3551 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003552 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003553
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003555 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003556 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003557 if (lai != null) {
3558 info.flags = AppInfo.initFlags(lai);
3559 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003562
Sunny Goyale2df0622015-04-24 11:27:00 -07003563 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003564 ItemInfoFilter f) {
3565 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3566 for (ItemInfo i : infos) {
3567 if (i instanceof ShortcutInfo) {
3568 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003569 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003570 if (cn != null && f.filterItem(null, info, cn)) {
3571 filtered.add(info);
3572 }
3573 } else if (i instanceof FolderInfo) {
3574 FolderInfo info = (FolderInfo) i;
3575 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003576 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003577 if (cn != null && f.filterItem(info, s, cn)) {
3578 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003579 }
3580 }
Winson Chung64359a52013-07-08 17:17:08 -07003581 } else if (i instanceof LauncherAppWidgetInfo) {
3582 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3583 ComponentName cn = info.providerName;
3584 if (cn != null && f.filterItem(null, info, cn)) {
3585 filtered.add(info);
3586 }
Winson Chung8a435102012-08-30 17:16:53 -07003587 }
3588 }
Winson Chung64359a52013-07-08 17:17:08 -07003589 return new ArrayList<ItemInfo>(filtered);
3590 }
3591
Adam Cohen091440a2015-03-18 14:16:05 -07003592 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003593 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003594 ItemInfoFilter filter = new ItemInfoFilter() {
3595 @Override
3596 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003597 if (info.user == null) {
3598 return cn.equals(cname);
3599 } else {
3600 return cn.equals(cname) && info.user.equals(user);
3601 }
Winson Chung64359a52013-07-08 17:17:08 -07003602 }
3603 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003604 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003605 }
3606
Sunny Goyal1a745e82014-10-02 15:58:31 -07003607 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003608 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003609 */
Adam Cohen091440a2015-03-18 14:16:05 -07003610 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003611 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003612 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003613 // Non-app shortcuts are only supported for current user.
3614 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003616
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003617 // TODO: If there's an explicit component and we can't install that, delete it.
3618
Winson Chung82b016c2015-05-08 17:00:10 -07003619 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003620
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003621 Bitmap icon = iconInfo.loadIcon(c, info, context);
3622 // the fallback icon
3623 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003624 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003625 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003626 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003627 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 return info;
3629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003630
Sunny Goyal2350bc92014-10-14 16:42:54 -07003631 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003632 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3633 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3634 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3635
Adam Cohend9198822011-11-22 16:42:47 -08003636 if (intent == null) {
3637 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3638 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3639 return null;
3640 }
3641
Joe Onorato0589f0f2010-02-08 13:44:00 -08003642 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003643 boolean customIcon = false;
3644 ShortcutIconResource iconResource = null;
3645
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003646 if (bitmap instanceof Bitmap) {
3647 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003648 customIcon = true;
3649 } else {
3650 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003651 if (extra instanceof ShortcutIconResource) {
3652 iconResource = (ShortcutIconResource) extra;
3653 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003654 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003655 }
3656 }
3657
Michael Jurkac9d95c52011-08-29 14:03:34 -07003658 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003659
Kenny Guyed131872014-04-30 03:02:21 +01003660 // Only support intents for current user for now. Intents sent from other
3661 // users wouldn't get here without intent forwarding anyway.
3662 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003663 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003664 icon = mIconCache.getDefaultIcon(info.user);
3665 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003666 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003667 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003668
Winson Chung82b016c2015-05-08 17:00:10 -07003669 info.title = Utilities.trim(name);
3670 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003671 info.intent = intent;
3672 info.customIcon = customIcon;
3673 info.iconResource = iconResource;
3674
3675 return info;
3676 }
3677
Joe Onorato9c1289c2009-08-17 11:03:03 -04003678 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003679 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 * or make a new one.
3681 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003682 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 // See if a placeholder was created for us already
3684 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003685 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003687 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 folders.put(id, folderInfo);
3689 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003690 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003691 }
3692
Joe Onoratobe386092009-11-17 17:32:16 -08003693
Sunny Goyal651077b2014-06-30 14:15:31 -07003694 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3695 return (provider != null) && (provider.provider != null)
3696 && (provider.provider.getPackageName() != null);
3697 }
3698
Joe Onoratobe386092009-11-17 17:32:16 -08003699 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003700 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003701 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3702 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3703 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3704 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003705 if (mLoaderTask != null) {
3706 mLoaderTask.dumpState();
3707 } else {
3708 Log.d(TAG, "mLoaderTask=null");
3709 }
Joe Onoratobe386092009-11-17 17:32:16 -08003710 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003711
3712 public Callbacks getCallback() {
3713 return mCallbacks != null ? mCallbacks.get() : null;
3714 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003715
3716 /**
3717 * @return {@link FolderInfo} if its already loaded.
3718 */
3719 public FolderInfo findFolderById(Long folderId) {
3720 synchronized (sBgLock) {
3721 return sBgFolders.get(folderId);
3722 }
3723 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003724
3725 /**
3726 * @return the looper for the worker thread which can be used to start background tasks.
3727 */
3728 public static Looper getWorkerLooper() {
3729 return sWorkerThread.getLooper();
3730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731}