blob: 5f2db850eaf766a30e1973bc7bfa96c55b235c1a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Sunny Goyal3dc7bee2015-09-15 11:32:58 -070037import android.os.DeadObjectException;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070059import com.android.launcher3.config.ProviderConfig;
Sunny Goyale5bb7052015-07-27 14:36:07 -070060import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070066import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080067
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.lang.ref.WeakReference;
69import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070070import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070072import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070073import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.Collections;
75import java.util.Comparator;
76import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070078import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070080import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * Maintains in-memory state of the Launcher. It is expected that there should be only one
85 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070086 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 */
Kenny Guyed131872014-04-30 03:02:21 +010088public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010089 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040091 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070092 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070095
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700111 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Jason Monkbbe1e242014-05-16 17:37:34 -0400113 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700114
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700116 static {
117 sWorkerThread.start();
118 }
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 // We start off with everything not loaded. After that, we assume that
122 // our monitoring of the package manager provides all updates and we never
123 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk boolean mWorkspaceLoaded;
125 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700126
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700127 // When we are loading pages synchronously, we can't just post the binding of items on the side
128 // pages as this delays the rotation process. Instead, we wait for a callback from the first
129 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
130 // a normal load, we also clear this set of Runnables.
131 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700142 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
144 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146 // The lock that must be acquired before referencing any static bg data structures. Unlike
147 // other locks, this one can generally be held long-term because we never expect any of these
148 // static data structures to be referenced outside of the worker thread except on the first
149 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700150 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700154 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
157 // created by LauncherModel that are directly on the home screen (however, no widgets or
158 // shortcuts within folders).
159 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
162 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163 new ArrayList<LauncherAppWidgetInfo>();
164
Adam Cohen487f7dd2012-06-28 18:12:10 -0700165 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700166 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700167
Adam Cohendcd297f2013-06-18 13:13:40 -0700168 // sBgWorkspaceScreens is the ordered set of workspace screens.
169 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
170
Adam Cohen59400422014-03-05 18:07:04 -0800171 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000172 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800173
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700175 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
176 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700177
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178 // </ only access in worker thread >
179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Adam Cohen091440a2015-03-18 14:16:05 -0700182 @Thunk final LauncherAppsCompat mLauncherApps;
183 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700186 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public int getCurrentWorkspaceScreen();
188 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
190 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700191 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700193 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800194 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700197 public void bindAppsAdded(ArrayList<Long> newScreens,
198 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700199 ArrayList<ItemInfo> addAnimated,
200 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
203 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
204 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700205 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700208 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700209 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700212 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700243 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 mIconCache = iconCache;
245
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700263 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Winson Chunge43a1e72014-01-15 10:33:02 -0800272 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
273 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800274 }
275
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 public void setPackageState(final PackageInstallInfo installInfo) {
277 Runnable updateRunnable = new Runnable() {
278
279 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500280 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 synchronized (sBgLock) {
282 final HashSet<ItemInfo> updates = new HashSet<>();
283
284 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
285 // Ignore install success events as they are handled by Package add events.
286 return;
287 }
288
Sunny Goyale2df0622015-04-24 11:27:00 -0700289 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 if (info instanceof ShortcutInfo) {
291 ShortcutInfo si = (ShortcutInfo) info;
292 ComponentName cn = si.getTargetComponent();
293 if (si.isPromise() && (cn != null)
294 && installInfo.packageName.equals(cn.getPackageName())) {
295 si.setInstallProgress(installInfo.progress);
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
298 // Mark this info as broken.
299 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
300 }
301 updates.add(si);
302 }
303 }
304 }
305
306 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
307 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
308 widget.installProgress = installInfo.progress;
309 updates.add(widget);
310 }
311 }
312
313 if (!updates.isEmpty()) {
314 // Push changes to the callback.
315 Runnable r = new Runnable() {
316 public void run() {
317 Callbacks callbacks = getCallback();
318 if (callbacks != null) {
319 callbacks.bindRestoreItemsChange(updates);
320 }
321 }
322 };
323 mHandler.post(r);
324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326 }
327 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500329 }
330
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 /**
332 * Updates the icons and label of all pending icons for the provided package name.
333 */
334 public void updateSessionDisplayInfo(final String packageName) {
335 Runnable updateRunnable = new Runnable() {
336
337 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 synchronized (sBgLock) {
340 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
341 final UserHandleCompat user = UserHandleCompat.myUserHandle();
342
Sunny Goyale2df0622015-04-24 11:27:00 -0700343 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 if (info instanceof ShortcutInfo) {
345 ShortcutInfo si = (ShortcutInfo) info;
346 ComponentName cn = si.getTargetComponent();
347 if (si.isPromise() && (cn != null)
348 && packageName.equals(cn.getPackageName())) {
349 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
350 // For auto install apps update the icon as well as label.
351 mIconCache.getTitleAndIcon(si,
352 si.promisedIntent, user,
353 si.shouldUseLowResIcon());
354 } else {
355 // Only update the icon for restored apps.
356 si.updateIcon(mIconCache);
357 }
358 updates.add(si);
359 }
360 }
361 }
362
363 if (!updates.isEmpty()) {
364 // Push changes to the callback.
365 Runnable r = new Runnable() {
366 public void run() {
367 Callbacks callbacks = getCallback();
368 if (callbacks != null) {
369 callbacks.bindShortcutsChanged(updates,
370 new ArrayList<ShortcutInfo>(), user);
371 }
372 }
373 };
374 mHandler.post(r);
375 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377 }
378 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700379 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700380 }
381
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384
385 if (allAppsApps == null) {
386 throw new RuntimeException("allAppsApps must not be null");
387 }
388 if (allAppsApps.isEmpty()) {
389 return;
390 }
391
392 // Process the newly added applications and add them to the database first
393 Runnable r = new Runnable() {
394 public void run() {
395 runOnMainThread(new Runnable() {
396 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 }
401 }
402 });
403 }
404 };
405 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700406 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800407
Sunny Goyala9116722015-04-29 13:55:58 -0700408 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 int[] xy, int spanX, int spanY) {
410 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
412 final int xCount = (int) profile.numColumns;
413 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 boolean[][] occupied = new boolean[xCount][yCount];
415 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700416 for (ItemInfo r : occupiedPos) {
417 int right = r.cellX + r.spanX;
418 int bottom = r.cellY + r.spanY;
419 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
420 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 occupied[x][y] = true;
422 }
423 }
424 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800425 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700426 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
428
429 /**
430 * Find a position on the screen for the given size or adds a new screen.
431 * @return screenId and the coordinates for the item.
432 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700433 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 ArrayList<Long> workspaceScreens,
436 ArrayList<Long> addedWorkspaceScreensFinal,
437 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700438 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439
Sunny Goyala9116722015-04-29 13:55:58 -0700440 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700441 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700442 synchronized (sBgLock) {
443 for (ItemInfo info : sBgItemsIdMap) {
444 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
445 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
446 if (items == null) {
447 items = new ArrayList<>();
448 screenItems.put(info.screenId, items);
449 }
450 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 }
454
455 // Find appropriate space for the item.
456 long screenId = 0;
457 int[] cordinates = new int[2];
458 boolean found = false;
459
460 int screenCount = workspaceScreens.size();
461 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700462 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 if (preferredScreenIndex < screenCount) {
464 screenId = workspaceScreens.get(preferredScreenIndex);
465 found = findNextAvailableIconSpaceInScreen(
466 screenItems.get(screenId), cordinates, spanX, spanY);
467 }
468
469 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700470 // Search on any of the screens starting from the first screen.
471 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 screenId = workspaceScreens.get(screen);
473 if (findNextAvailableIconSpaceInScreen(
474 screenItems.get(screenId), cordinates, spanX, spanY)) {
475 // We found a space for it
476 found = true;
477 break;
478 }
479 }
480 }
481
482 if (!found) {
483 // Still no position found. Add a new screen to the end.
484 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
485
486 // Save the screen id for binding in the workspace
487 workspaceScreens.add(screenId);
488 addedWorkspaceScreensFinal.add(screenId);
489
490 // If we still can't find an empty space, then God help us all!!!
491 if (!findNextAvailableIconSpaceInScreen(
492 screenItems.get(screenId), cordinates, spanX, spanY)) {
493 throw new RuntimeException("Can't find space to add the item");
494 }
495 }
496 return Pair.create(screenId, cordinates);
497 }
498
499 /**
500 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700502 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700503 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800505 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700506 return;
Winson Chung997a9232013-07-24 15:33:46 -0700507 }
Winson Chung64359a52013-07-08 17:17:08 -0700508 // Process the newly added applications and add them to the database first
509 Runnable r = new Runnable() {
510 public void run() {
511 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
512 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
513
Winson Chung76828c82013-08-19 15:43:29 -0700514 // Get the list of workspace screens. We need to append to this list and
515 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
516 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800517 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700518 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800519 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700520 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700522 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700524 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 }
Winson Chung76828c82013-08-19 15:43:29 -0700526
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700528 Pair<Long, int[]> coords = findSpaceForItem(context,
529 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 1, 1);
531 long screenId = coords.first;
532 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700533
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700534 ItemInfo itemInfo;
535 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
536 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700538 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700539 } else {
540 throw new RuntimeException("Unexpected info type");
541 }
Winson Chung94d67682013-09-25 16:29:40 -0700542
Winson Chung64359a52013-07-08 17:17:08 -0700543 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700545 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700546 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700547 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700549 }
550 }
551
Winson Chung76828c82013-08-19 15:43:29 -0700552 // Update the workspace screens
553 updateWorkspaceScreenOrder(context, workspaceScreens);
554
Adam Cohen76a47a12014-02-05 11:47:43 -0800555 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700556 runOnMainThread(new Runnable() {
557 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800558 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700559 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700560 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
561 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700562 if (!addedShortcutsFinal.isEmpty()) {
563 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
564 long lastScreenId = info.screenId;
565 for (ItemInfo i : addedShortcutsFinal) {
566 if (i.screenId == lastScreenId) {
567 addAnimated.add(i);
568 } else {
569 addNotAnimated.add(i);
570 }
Winson Chung997a9232013-07-24 15:33:46 -0700571 }
572 }
Winson Chungd64d1762013-08-20 14:37:16 -0700573 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800574 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700575 }
Winson Chung64359a52013-07-08 17:17:08 -0700576 }
Winson Chung997a9232013-07-24 15:33:46 -0700577 });
578 }
Winson Chung64359a52013-07-08 17:17:08 -0700579 }
580 };
581 runOnWorkerThread(r);
582 }
583
Sunny Goyald33860f2015-04-23 16:02:20 -0700584 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700585 if (sWorkerThread.getThreadId() == Process.myTid()) {
586 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
587 "main thread");
588 }
589
590 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400591 synchronized (mDeferredBindRunnables) {
592 mDeferredBindRunnables.clear();
593 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700594
595 // Remove any queued UI runnables
596 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700597 // Unbind all the workspace items
598 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700599 }
600
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700601 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700602 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700603 // Ensure that we don't use the same workspace items data structure on the main thread
604 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700605 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700606 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700607 tmpItems.addAll(sBgWorkspaceItems);
608 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700609 }
610 Runnable r = new Runnable() {
611 @Override
612 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700613 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 item.unbind();
615 }
616 }
617 };
618 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700619 }
620
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 /**
622 * Adds an item to the DB if it was not created previously, or move it to a new
623 * <container, screen, cellX, cellY>
624 */
625 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700626 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 if (item.container == ItemInfo.NO_ID) {
628 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700629 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 } else {
631 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
634 }
635
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700636 static void checkItemInfoLocked(
637 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
638 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
639 if (modelItem != null && item != modelItem) {
640 // check all the data is consistent
641 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
642 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
643 ShortcutInfo shortcut = (ShortcutInfo) item;
644 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
645 modelShortcut.intent.filterEquals(shortcut.intent) &&
646 modelShortcut.id == shortcut.id &&
647 modelShortcut.itemType == shortcut.itemType &&
648 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700650 modelShortcut.cellX == shortcut.cellX &&
651 modelShortcut.cellY == shortcut.cellY &&
652 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700653 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700654 // For all intents and purposes, this is the same object
655 return;
656 }
657 }
658
659 // the modelItem needs to match up perfectly with item if our model is
660 // to be consistent with the database-- for now, just require
661 // modelItem == item or the equality check above
662 String msg = "item: " + ((item != null) ? item.toString() : "null") +
663 "modelItem: " +
664 ((modelItem != null) ? modelItem.toString() : "null") +
665 "Error: ItemInfo passed to checkItemInfo doesn't match original";
666 RuntimeException e = new RuntimeException(msg);
667 if (stackTrace != null) {
668 e.setStackTrace(stackTrace);
669 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800670 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700671 }
672 }
673
Michael Jurka816474f2012-06-25 14:49:02 -0700674 static void checkItemInfo(final ItemInfo item) {
675 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
676 final long itemId = item.id;
677 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700678 public void run() {
679 synchronized (sBgLock) {
680 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700681 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700682 }
683 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700684 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700685 }
686
Michael Jurkac9d95c52011-08-29 14:03:34 -0700687 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
688 final ItemInfo item, final String callingFunction) {
689 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700690 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700691 final ContentResolver cr = context.getContentResolver();
692
Adam Cohen487f7dd2012-06-28 18:12:10 -0700693 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694 Runnable r = new Runnable() {
695 public void run() {
696 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700697 updateItemArrays(item, itemId, stackTrace);
698 }
699 };
700 runOnWorkerThread(r);
701 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700702
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
704 final ArrayList<ItemInfo> items, final String callingFunction) {
705 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700706
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
708 Runnable r = new Runnable() {
709 public void run() {
710 ArrayList<ContentProviderOperation> ops =
711 new ArrayList<ContentProviderOperation>();
712 int count = items.size();
713 for (int i = 0; i < count; i++) {
714 ItemInfo item = items.get(i);
715 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700716 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700717 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700718
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
720 updateItemArrays(item, itemId, stackTrace);
721
722 }
723 try {
724 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
725 } catch (Exception e) {
726 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727 }
728 }
729 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700730 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700731 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700732
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700733 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
734 // Lock on mBgLock *after* the db operation
735 synchronized (sBgLock) {
736 checkItemInfoLocked(itemId, item, stackTrace);
737
738 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
739 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
740 // Item is in a folder, make sure this folder exists
741 if (!sBgFolders.containsKey(item.container)) {
742 // An items container is being set to a that of an item which is not in
743 // the list of Folders.
744 String msg = "item: " + item + " container being set to: " +
745 item.container + ", not in the list of folders";
746 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 }
748 }
749
750 // Items are added/removed from the corresponding FolderInfo elsewhere, such
751 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
752 // that are on the desktop, as appropriate
753 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800754 if (modelItem != null &&
755 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
756 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757 switch (modelItem.itemType) {
758 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
759 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
760 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
761 if (!sBgWorkspaceItems.contains(modelItem)) {
762 sBgWorkspaceItems.add(modelItem);
763 }
764 break;
765 default:
766 break;
767 }
768 } else {
769 sBgWorkspaceItems.remove(modelItem);
770 }
771 }
772 }
773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700776 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700777 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 item.cellX = cellX;
781 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700782
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 // We store hotseat items in canonical form which is this orientation invariant position
784 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700788 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
792 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700794 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
795 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800796 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798
Michael Jurkac9d95c52011-08-29 14:03:34 -0700799 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700800 }
801
802 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700803 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
804 * cellX, cellY have already been updated on the ItemInfos.
805 */
806 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
807 final long container, final int screen) {
808
809 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
810 int count = items.size();
811
812 for (int i = 0; i < count; i++) {
813 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 item.container = container;
815
816 // We store hotseat items in canonical form which is this orientation invariant position
817 // in the hotseat
818 if (context instanceof Launcher && screen < 0 &&
819 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700820 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 item.cellY);
822 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 }
825
826 final ContentValues values = new ContentValues();
827 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
828 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
829 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800830 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832
833 contentValues.add(values);
834 }
835 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
836 }
837
838 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700839 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800840 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700841 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700843 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800844 item.cellX = cellX;
845 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700846 item.spanX = spanX;
847 item.spanY = spanY;
848
849 // We store hotseat items in canonical form which is this orientation invariant position
850 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700854 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 }
Adam Cohend4844c32011-02-18 19:25:06 -0800857
Adam Cohend4844c32011-02-18 19:25:06 -0800858 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800859 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
861 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800862 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
864 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800866
Michael Jurka816474f2012-06-25 14:49:02 -0700867 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700868 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869
870 /**
871 * Update an item to the database in a specified container.
872 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700873 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700874 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100875 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700876 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800877 }
878
Sunny Goyal756a28a2015-04-23 17:07:55 -0700879 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700880 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700881 synchronized (mLock) {
882 if (!mHasLoaderCompletedOnce ||
883 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
884 throw new RuntimeException("Trying to add shortcut while loader is running");
885 }
886 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700887 }
888 }
889
Adam Cohend4844c32011-02-18 19:25:06 -0800890 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700891 * Returns true if the shortcuts already exists on the workspace. This must be called after
892 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700894 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
895 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700896 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700897 if (intent.getComponent() != null) {
898 // If component is not null, an intent with null package will produce
899 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700900 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700901 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700902 intentWithPkg = intent.toUri(0);
903 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700905 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
906 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 }
908 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 intentWithPkg = intent.toUri(0);
910 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700912
913 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700914 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915 if (item instanceof ShortcutInfo) {
916 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700917 Intent targetIntent = info.promisedIntent == null
918 ? info.intent : info.promisedIntent;
919 if (targetIntent != null && info.user.equals(user)) {
920 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700921 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
922 return true;
923 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700924 }
925 }
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700928 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700929 }
930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 /**
932 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
933 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700934 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935 final ContentResolver cr = context.getContentResolver();
936 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
937 "_id=? and (itemType=? or itemType=?)",
938 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700939 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700940
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 try {
942 if (c.moveToFirst()) {
943 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
944 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
945 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
946 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
947 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
948 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700949 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 FolderInfo folderInfo = null;
952 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700953 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
954 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700956 }
957
Sunny Goyala508e4f2015-05-21 09:33:57 -0700958 // Do not trim the folder label, as is was set by the user.
959 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 folderInfo.id = id;
961 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700963 folderInfo.cellX = c.getInt(cellXIndex);
964 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700965 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966
967 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 } finally {
970 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700971 }
972
973 return null;
974 }
975
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 /**
977 * Add an item to the database in a specified container. Sets the container, screen, cellX and
978 * cellY fields of the item. Also assigns an ID to the item.
979 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700980 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700981 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 item.cellX = cellX;
984 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 // We store hotseat items in canonical form which is this orientation invariant position
986 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700987 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700988 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700990 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993
994 final ContentValues values = new ContentValues();
995 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100996 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
Michael Jurka414300a2013-08-27 15:42:35 +0200998 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001000
Jason Monk8e19cf22014-03-20 15:06:57 -04001001 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001002 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001003 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001004 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001006 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001007 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001008 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 sBgItemsIdMap.put(item.id, item);
1010 switch (item.itemType) {
1011 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1012 sBgFolders.put(item.id, (FolderInfo) item);
1013 // Fall through
1014 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1015 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1016 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1017 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1018 sBgWorkspaceItems.add(item);
1019 } else {
1020 if (!sBgFolders.containsKey(item.container)) {
1021 // Adding an item to a folder that doesn't exist.
1022 String msg = "adding item: " + item + " to a folder that " +
1023 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001024 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001026 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 break;
1028 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1029 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1030 break;
1031 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001032 }
1033 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001034 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001036 }
1037
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001039 * Creates a new unique child id, for a given cell span across all layouts.
1040 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001041 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001042 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001044 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001045 }
1046
Sunny Goyal34942622014-08-29 17:20:55 -07001047 private static ArrayList<ItemInfo> getItemsByPackageName(
1048 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001049 ItemInfoFilter filter = new ItemInfoFilter() {
1050 @Override
1051 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1052 return cn.getPackageName().equals(pn) && info.user.equals(user);
1053 }
1054 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001055 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001056 }
1057
1058 /**
1059 * Removes all the items from the database corresponding to the specified package.
1060 */
1061 static void deletePackageFromDatabase(Context context, final String pn,
1062 final UserHandleCompat user) {
1063 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001064 }
1065
1066 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001067 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001069 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001070 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1071 items.add(item);
1072 deleteItemsFromDatabase(context, items);
1073 }
1074
1075 /**
1076 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001078 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001080 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001081 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001082 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001083 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001084 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001085
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001086 // Lock on mBgLock *after* the db operation
1087 synchronized (sBgLock) {
1088 switch (item.itemType) {
1089 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1090 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001091 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001092 if (info.container == item.id) {
1093 // We are deleting a folder which still contains items that
1094 // think they are contained by that folder.
1095 String msg = "deleting a folder (" + item + ") which still " +
1096 "contains items (" + info + ")";
1097 Log.e(TAG, msg);
1098 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 sBgWorkspaceItems.remove(item);
1101 break;
1102 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1103 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1104 sBgWorkspaceItems.remove(item);
1105 break;
1106 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1107 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1108 break;
1109 }
1110 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001112 }
1113 }
Michael Jurka83df1882011-08-31 20:59:26 -07001114 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 }
1117
1118 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001119 * Update the order of the workspace screens in the database. The array list contains
1120 * a list of screen ids in the order that they should appear.
1121 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001122 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001123 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 final ContentResolver cr = context.getContentResolver();
1125 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1126
1127 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001128 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001129 while (iter.hasNext()) {
1130 long id = iter.next();
1131 if (id < 0) {
1132 iter.remove();
1133 }
1134 }
1135
1136 Runnable r = new Runnable() {
1137 @Override
1138 public void run() {
Yura085c8532014-02-11 15:15:29 +00001139 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001140 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001141 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001142 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 for (int i = 0; i < count; i++) {
1144 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001145 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1147 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001148 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 }
Yura085c8532014-02-11 15:15:29 +00001150
1151 try {
1152 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1153 } catch (Exception ex) {
1154 throw new RuntimeException(ex);
1155 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001156
Winson Chungba9c37f2013-08-30 14:11:37 -07001157 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001158 sBgWorkspaceScreens.clear();
1159 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001160 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 }
1162 };
1163 runOnWorkerThread(r);
1164 }
1165
1166 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001167 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 */
Winsonc0b52fe2015-09-09 16:38:15 -07001169 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 final ContentResolver cr = context.getContentResolver();
1171
Michael Jurkac9d95c52011-08-29 14:03:34 -07001172 Runnable r = new Runnable() {
1173 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001174 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001175 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001176 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001177 sBgItemsIdMap.remove(info.id);
1178 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 sBgWorkspaceItems.remove(info);
1180 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001181
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001182 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001183 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001185 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 for (ItemInfo childInfo : info.contents) {
1187 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001189 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001190 }
1191 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 }
1194
1195 /**
1196 * Set this as the current Launcher activity object for the loader.
1197 */
1198 public void initialize(Callbacks callbacks) {
1199 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001200 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1201 // workspace to prevent leaking Launcher activities on orientation change.
1202 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 mCallbacks = new WeakReference<Callbacks>(callbacks);
1204 }
1205 }
1206
Kenny Guyed131872014-04-30 03:02:21 +01001207 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001208 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001209 int op = PackageUpdatedTask.OP_UPDATE;
1210 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1211 user));
1212 }
1213
1214 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001215 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001216 int op = PackageUpdatedTask.OP_REMOVE;
1217 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1218 user));
1219 }
1220
1221 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001222 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001223 int op = PackageUpdatedTask.OP_ADD;
1224 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1225 user));
1226 }
1227
1228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001230 boolean replacing) {
1231 if (!replacing) {
1232 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1233 user));
1234 if (mAppsCanBeOnRemoveableStorage) {
1235 // Only rebind if we support removable storage. It catches the
1236 // case where
1237 // apps on the external sd card need to be reloaded
1238 startLoaderFromBackground();
1239 }
1240 } else {
1241 // If we are replacing then just update the packages in the list
1242 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1243 packageNames, user));
1244 }
1245 }
1246
1247 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001248 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001249 boolean replacing) {
1250 if (!replacing) {
1251 enqueuePackageUpdated(new PackageUpdatedTask(
1252 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1253 user));
1254 }
Kenny Guyed131872014-04-30 03:02:21 +01001255 }
1256
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001257 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1259 * ACTION_PACKAGE_CHANGED.
1260 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001261 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001262 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001263 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001264
Joe Onorato36115782010-06-17 13:28:48 -04001265 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001266 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001267 // If we have changed locale we need to clear out the labels in all apps/workspace.
1268 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001269 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001270 Callbacks callbacks = getCallback();
1271 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001272 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001273 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001274 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1275 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001276 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001277 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001278 }
1279 }
1280
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001281 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001282 resetLoadedState(true, true);
1283
Reena Lee93f824a2011-09-23 17:20:28 -07001284 // Do this here because if the launcher activity is running it will be restarted.
1285 // If it's not running startLoaderFromBackground will merely tell it that it needs
1286 // to reload.
1287 startLoaderFromBackground();
1288 }
1289
Winson Chungf0c6ae02012-03-21 16:10:31 -07001290 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1291 synchronized (mLock) {
1292 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1293 // mWorkspaceLoaded to true later
1294 stopLoaderLocked();
1295 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1296 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1297 }
1298 }
1299
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001300 /**
1301 * When the launcher is in the background, it's possible for it to miss paired
1302 * configuration changes. So whenever we trigger the loader from the background
1303 * tell the launcher that it needs to re-run the loader when it comes back instead
1304 * of doing it now.
1305 */
1306 public void startLoaderFromBackground() {
1307 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001308 Callbacks callbacks = getCallback();
1309 if (callbacks != null) {
1310 // Only actually run the loader if they're not paused.
1311 if (!callbacks.setLoadOnResume()) {
1312 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001313 }
1314 }
1315 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001316 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001317 }
Joe Onorato36115782010-06-17 13:28:48 -04001318 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001319
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001320 /**
1321 * If there is already a loader task running, tell it to stop.
1322 */
1323 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001324 LoaderTask oldTask = mLoaderTask;
1325 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001326 oldTask.stopLocked();
1327 }
Reena Lee93f824a2011-09-23 17:20:28 -07001328 }
1329
Adam Cohen1a85c582014-09-30 09:48:49 -07001330 public boolean isCurrentCallbacks(Callbacks callbacks) {
1331 return (mCallbacks != null && mCallbacks.get() == callbacks);
1332 }
1333
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001334 public void startLoader(int synchronousBindPage) {
1335 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001336 }
1337
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001338 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001339 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1340 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001341 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 // Clear any deferred bind-runnables from the synchronized load process
1343 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001344 synchronized (mDeferredBindRunnables) {
1345 mDeferredBindRunnables.clear();
1346 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347
Joe Onorato36115782010-06-17 13:28:48 -04001348 // Don't bother to start the thread if we know it's not going to do anything
1349 if (mCallbacks != null && mCallbacks.get() != null) {
1350 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001351 stopLoaderLocked();
1352 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001353 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001354 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1356 } else {
1357 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1358 sWorker.post(mLoaderTask);
1359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 }
1362 }
1363
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 void bindRemainingSynchronousPages() {
1365 // Post the remaining side pages to be loaded
1366 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001367 Runnable[] deferredBindRunnables = null;
1368 synchronized (mDeferredBindRunnables) {
1369 deferredBindRunnables = mDeferredBindRunnables.toArray(
1370 new Runnable[mDeferredBindRunnables.size()]);
1371 mDeferredBindRunnables.clear();
1372 }
1373 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001374 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 }
1377 }
1378
Joe Onorato36115782010-06-17 13:28:48 -04001379 public void stopLoader() {
1380 synchronized (mLock) {
1381 if (mLoaderTask != null) {
1382 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 }
1384 }
Joe Onorato36115782010-06-17 13:28:48 -04001385 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001387 /**
1388 * Loads the workspace screen ids in an ordered list.
1389 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001390 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001391 final ContentResolver contentResolver = context.getContentResolver();
1392 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001393
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001394 // Get screens ordered by rank.
1395 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1396 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1397 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001398 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001399 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001400 while (sc.moveToNext()) {
1401 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001402 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001403 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001404 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1405 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001406 }
1407 }
1408 } finally {
1409 sc.close();
1410 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001411 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001412 }
1413
Michael Jurkac57b7a82011-08-09 22:02:20 -07001414 public boolean isAllAppsLoaded() {
1415 return mAllAppsLoaded;
1416 }
1417
Joe Onorato36115782010-06-17 13:28:48 -04001418 /**
1419 * Runnable for the thread that loads the contents of the launcher:
1420 * - workspace icons
1421 * - widgets
1422 * - all apps icons
1423 */
1424 private class LoaderTask implements Runnable {
1425 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001426 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001427 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001428 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001429 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001431 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001432 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001433 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 }
1435
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001436 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001437 mIsLoadingAndBindingWorkspace = true;
1438
Joe Onorato36115782010-06-17 13:28:48 -04001439 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001440 if (DEBUG_LOADERS) {
1441 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001443
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001444 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001445 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001446 synchronized (LoaderTask.this) {
1447 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001448 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001449 }
1450 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 }
1452 }
1453
Joe Onorato36115782010-06-17 13:28:48 -04001454 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001455 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001456 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001457
Joe Onorato36115782010-06-17 13:28:48 -04001458 private void waitForIdle() {
1459 // Wait until the either we're stopped or the other threads are done.
1460 // This way we don't start loading all apps until the workspace has settled
1461 // down.
1462 synchronized (LoaderTask.this) {
1463 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001464
Joe Onorato36115782010-06-17 13:28:48 -04001465 mHandler.postIdle(new Runnable() {
1466 public void run() {
1467 synchronized (LoaderTask.this) {
1468 mLoadAndBindStepFinished = true;
1469 if (DEBUG_LOADERS) {
1470 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001471 }
Joe Onorato36115782010-06-17 13:28:48 -04001472 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001473 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 });
1476
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001477 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001478 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001479 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1480 // wait no longer than 1sec at a time
1481 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001482 } catch (InterruptedException ex) {
1483 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001484 }
1485 }
Joe Onorato36115782010-06-17 13:28:48 -04001486 if (DEBUG_LOADERS) {
1487 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001488 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001489 + "ms for previous step to finish binding");
1490 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001493
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001494 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001495 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001496 // Ensure that we have a valid page index to load synchronously
1497 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1498 "valid page index");
1499 }
1500 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1501 // Ensure that we don't try and bind a specified page when the pages have not been
1502 // loaded already (we should load everything asynchronously in that case)
1503 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1504 }
1505 synchronized (mLock) {
1506 if (mIsLoaderTaskRunning) {
1507 // Ensure that we are never running the background loading at this point since
1508 // we also touch the background collections
1509 throw new RuntimeException("Error! Background loading is already running");
1510 }
1511 }
1512
1513 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1514 // data structures, we can't allow any other thread to touch that data, but because
1515 // this call is synchronous, we can get away with not locking).
1516
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001517 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001518 // operations from the previous activity. We need to ensure that all queued operations
1519 // are executed before any synchronous binding work is done.
1520 mHandler.flush();
1521
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001522 // Divide the set of loaded items into those that we are binding synchronously, and
1523 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001524 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1526 // arise from that.
1527 onlyBindAllApps();
1528 }
1529
Joe Onorato36115782010-06-17 13:28:48 -04001530 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001532 if (mStopped) {
1533 return;
1534 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001535 mIsLoaderTaskRunning = true;
1536 }
Joe Onorato36115782010-06-17 13:28:48 -04001537 // Optimize for end-user experience: if the Launcher is up and // running with the
1538 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1539 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001540 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001541 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001542 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001543
Joe Onorato36115782010-06-17 13:28:48 -04001544 if (mStopped) {
1545 break keep_running;
1546 }
1547
Joe Onorato36115782010-06-17 13:28:48 -04001548 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001549
1550 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001551 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1552 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554
Joe Onorato36115782010-06-17 13:28:48 -04001555 // Clear out this reference, otherwise we end up holding it until all of the
1556 // callback runnables are done.
1557 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558
Joe Onorato36115782010-06-17 13:28:48 -04001559 synchronized (mLock) {
1560 // If we are still the last one to be scheduled, remove ourselves.
1561 if (mLoaderTask == this) {
1562 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001563 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001564 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001565 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001566 }
Joe Onorato36115782010-06-17 13:28:48 -04001567 }
1568
1569 public void stopLocked() {
1570 synchronized (LoaderTask.this) {
1571 mStopped = true;
1572 this.notify();
1573 }
1574 }
1575
1576 /**
1577 * Gets the callbacks object. If we've been stopped, or if the launcher object
1578 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1579 * object that was around when the deferred message was scheduled, and if there's
1580 * a new Callbacks object around then also return null. This will save us from
1581 * calling onto it with data that will be ignored.
1582 */
1583 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1584 synchronized (mLock) {
1585 if (mStopped) {
1586 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001587 }
Joe Onorato36115782010-06-17 13:28:48 -04001588
1589 if (mCallbacks == null) {
1590 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001591 }
Joe Onorato36115782010-06-17 13:28:48 -04001592
1593 final Callbacks callbacks = mCallbacks.get();
1594 if (callbacks != oldCallbacks) {
1595 return null;
1596 }
1597 if (callbacks == null) {
1598 Log.w(TAG, "no mCallbacks");
1599 return null;
1600 }
1601
1602 return callbacks;
1603 }
1604 }
1605
1606 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001607 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1608 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001609 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001610 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001611 final int countX = profile.numColumns;
1612 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001613
Adam Cohendcd297f2013-06-18 13:13:40 -07001614 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001615 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001616 // Return early if we detect that an item is under the hotseat button
1617 if (mCallbacks == null ||
1618 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001619 Log.e(TAG, "Error loading shortcut into hotseat " + item
1620 + " into position (" + item.screenId + ":" + item.cellX + ","
1621 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001622 return false;
1623 }
1624
Dan Sandler295ae182013-12-10 16:05:47 -05001625 final ItemInfo[][] hotseatItems =
1626 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1627
Adam Cohen2e6da152015-05-06 11:42:25 -07001628 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001629 Log.e(TAG, "Error loading shortcut " + item
1630 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001631 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001632 + ")");
1633 return false;
1634 }
1635
Dan Sandler295ae182013-12-10 16:05:47 -05001636 if (hotseatItems != null) {
1637 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001638 Log.e(TAG, "Error loading shortcut into hotseat " + item
1639 + " into position (" + item.screenId + ":" + item.cellX + ","
1640 + item.cellY + ") occupied by "
1641 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1642 [(int) item.screenId][0]);
1643 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001644 } else {
1645 hotseatItems[(int) item.screenId][0] = item;
1646 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001647 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001648 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001649 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001650 items[(int) item.screenId][0] = item;
1651 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001652 return true;
1653 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001654 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1655 if (!workspaceScreens.contains((Long) item.screenId)) {
1656 // The item has an invalid screen id.
1657 return false;
1658 }
1659 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001660 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001661 return true;
1662 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001663
Adam Cohendcd297f2013-06-18 13:13:40 -07001664 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001665 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001666 occupied.put(item.screenId, items);
1667 }
1668
Dan Sandler295ae182013-12-10 16:05:47 -05001669 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001670 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1671 item.cellX < 0 || item.cellY < 0 ||
1672 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1673 Log.e(TAG, "Error loading shortcut " + item
1674 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1675 + item.cellX + "," + item.cellY
1676 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1677 return false;
1678 }
1679
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001680 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001681 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1682 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001683 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001684 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001685 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001686 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001687 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001688 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001689 return false;
1690 }
1691 }
1692 }
1693 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1694 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001695 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001696 }
1697 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001698
Joe Onorato36115782010-06-17 13:28:48 -04001699 return true;
1700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701
Winson Chungba9c37f2013-08-30 14:11:37 -07001702 /** Clears all the sBg data structures */
1703 private void clearSBgDataStructures() {
1704 synchronized (sBgLock) {
1705 sBgWorkspaceItems.clear();
1706 sBgAppWidgets.clear();
1707 sBgFolders.clear();
1708 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001709 sBgWorkspaceScreens.clear();
1710 }
1711 }
1712
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001713 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001714 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Joe Onorato36115782010-06-17 13:28:48 -04001716 final Context context = mContext;
1717 final ContentResolver contentResolver = context.getContentResolver();
1718 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001719 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001720 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001721 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001722
Winson Chung892c74d2013-08-22 16:15:50 -07001723 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001724 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001725 int countX = profile.numColumns;
1726 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001727
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001728 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001729 long migrationStartTime = System.currentTimeMillis();
1730 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001731 try {
1732 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1733 // Clear the flags before starting the task, so that we do not run the task
1734 // again, in case there was an uncaught error.
1735 MigrateFromRestoreTask.clearFlags(mContext);
1736 task.execute();
1737 } catch (Exception e) {
1738 Log.e(TAG, "Error during grid migration", e);
1739
1740 // Clear workspace.
1741 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1742 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001743 Log.v(TAG, "Workspace migration completed in "
1744 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001745 }
1746
Dan Sandlerd5024042014-01-09 15:01:33 -05001747 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1748 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1749 LauncherAppState.getLauncherProvider().deleteDatabase();
1750 }
1751
1752 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1753 // append the user's Launcher2 shortcuts
1754 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1755 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1756 } else {
1757 // Make sure the default workspace is loaded
1758 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001759 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001760 }
Adam Cohene25af792013-06-06 23:08:25 -07001761
Winson Chung2abf94d2012-07-18 18:16:38 -07001762 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001763 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001764 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001765 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001766 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001767
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001769 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001770 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001771 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001772 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 // +1 for the hotseat (it can be larger than the workspace)
1775 // Load workspace in reverse order to ensure that latest items are loaded first (and
1776 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001777 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 try {
1780 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1781 final int intentIndex = c.getColumnIndexOrThrow
1782 (LauncherSettings.Favorites.INTENT);
1783 final int titleIndex = c.getColumnIndexOrThrow
1784 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 final int containerIndex = c.getColumnIndexOrThrow(
1786 LauncherSettings.Favorites.CONTAINER);
1787 final int itemTypeIndex = c.getColumnIndexOrThrow(
1788 LauncherSettings.Favorites.ITEM_TYPE);
1789 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1790 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001791 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1792 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 final int screenIndex = c.getColumnIndexOrThrow(
1794 LauncherSettings.Favorites.SCREEN);
1795 final int cellXIndex = c.getColumnIndexOrThrow
1796 (LauncherSettings.Favorites.CELLX);
1797 final int cellYIndex = c.getColumnIndexOrThrow
1798 (LauncherSettings.Favorites.CELLY);
1799 final int spanXIndex = c.getColumnIndexOrThrow
1800 (LauncherSettings.Favorites.SPANX);
1801 final int spanYIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001803 final int rankIndex = c.getColumnIndexOrThrow(
1804 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001805 final int restoredIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001807 final int profileIdIndex = c.getColumnIndexOrThrow(
1808 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001809 final int optionsIndex = c.getColumnIndexOrThrow(
1810 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001811 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812
Sunny Goyal7f834d22015-04-21 10:10:23 -07001813 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1814 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1815 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1816 }
1817
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 ShortcutInfo info;
1819 String intentDescription;
1820 LauncherAppWidgetInfo appWidgetInfo;
1821 int container;
1822 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001823 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001825 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 while (!mStopped && c.moveToNext()) {
1828 try {
1829 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001830 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001831 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001832 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 switch (itemType) {
1835 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1836 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001837 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001839 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001840 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001841 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001842 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001843 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001844 if (user == null) {
1845 // User has been deleted remove the item.
1846 itemsToRemove.add(id);
1847 continue;
1848 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 try {
1850 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001851 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001852 if (cn != null && cn.getPackageName() != null) {
1853 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1854 cn.getPackageName(), user);
1855 boolean validComponent = validPkg &&
1856 launcherApps.isActivityEnabledForProfile(cn, user);
1857
1858 if (validComponent) {
1859 if (restored) {
1860 // no special handling necessary for this item
1861 restoredRows.add(id);
1862 restored = false;
1863 }
1864 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001865 intent = null;
1866 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1867 // We allow auto install apps to have their intent
1868 // updated after an install.
1869 intent = manager.getLaunchIntentForPackage(
1870 cn.getPackageName());
1871 if (intent != null) {
1872 ContentValues values = new ContentValues();
1873 values.put(LauncherSettings.Favorites.INTENT,
1874 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001875 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001876 }
1877 }
1878
1879 if (intent == null) {
1880 // The app is installed but the component is no
1881 // longer available.
1882 Launcher.addDumpLog(TAG,
1883 "Invalid component removed: " + cn, true);
1884 itemsToRemove.add(id);
1885 continue;
1886 } else {
1887 // no special handling necessary for this item
1888 restoredRows.add(id);
1889 restored = false;
1890 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001891 } else if (restored) {
1892 // Package is not yet available but might be
1893 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001894 Launcher.addDumpLog(TAG,
1895 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001896
1897 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1898 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001899 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001900 // App restore has started. Update the flag
1901 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1902 ContentValues values = new ContentValues();
1903 values.put(LauncherSettings.Favorites.RESTORED,
1904 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001905 updateItem(id, values);
1906 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1907 // This is a common app. Try to replace this.
1908 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1909 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1910 if (parser.findDefaultApp()) {
1911 // Default app found. Replace it.
1912 intent = parser.parsedIntent;
1913 cn = intent.getComponent();
1914 ContentValues values = parser.parsedValues;
1915 values.put(LauncherSettings.Favorites.RESTORED, 0);
1916 updateItem(id, values);
1917 restored = false;
1918 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001919
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001920 } else if (REMOVE_UNRESTORED_ICONS) {
1921 Launcher.addDumpLog(TAG,
1922 "Unrestored package removed: " + cn, true);
1923 itemsToRemove.add(id);
1924 continue;
1925 }
Sunny Goyal94485362014-09-18 16:13:58 -07001926 } else if (REMOVE_UNRESTORED_ICONS) {
1927 Launcher.addDumpLog(TAG,
1928 "Unrestored package removed: " + cn, true);
1929 itemsToRemove.add(id);
1930 continue;
1931 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001932 } else if (launcherApps.isAppEnabled(
1933 manager, cn.getPackageName(),
1934 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1935 // Package is present but not available.
1936 allowMissingTarget = true;
1937 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1938 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001939 // SdCard is not ready yet. Package might get available,
1940 // once it is ready.
1941 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1942 + " (check again later)", true);
1943 HashSet<String> pkgs = sPendingPackages.get(user);
1944 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001945 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001946 sPendingPackages.put(user, pkgs);
1947 }
1948 pkgs.add(cn.getPackageName());
1949 allowMissingTarget = true;
1950 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001951
1952 } else {
1953 // Do not wait for external media load anymore.
1954 // Log the invalid package, and remove it
1955 Launcher.addDumpLog(TAG,
1956 "Invalid package removed: " + cn, true);
1957 itemsToRemove.add(id);
1958 continue;
Winson Chungee055712013-07-30 14:46:24 -07001959 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001960 } else if (cn == null) {
1961 // For shortcuts with no component, keep them as they are
1962 restoredRows.add(id);
1963 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001964 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001966 Launcher.addDumpLog(TAG,
1967 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001968 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001969 continue;
1970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971
Sunny Goyal34b65272015-03-11 16:56:52 -07001972 boolean useLowResIcon = container >= 0 &&
1973 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1974
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001975 if (itemReplaced) {
1976 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001977 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001978 cursorIconInfo.iconIndex, titleIndex,
1979 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001980 } else {
1981 // Don't replace items for other profiles.
1982 itemsToRemove.add(id);
1983 continue;
1984 }
1985 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001986 if (user.equals(UserHandleCompat.myUserHandle())) {
1987 Launcher.addDumpLog(TAG,
1988 "constructing info for partially restored package",
1989 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001990 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001991 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001992 intent = getRestoredItemIntent(c, context, intent);
1993 } else {
1994 // Don't restore items for other profiles.
1995 itemsToRemove.add(id);
1996 continue;
1997 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001998 } else if (itemType ==
1999 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002000 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002001 cursorIconInfo.iconIndex, titleIndex,
2002 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002004 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 // App shortcuts that used to be automatically added to Launcher
2007 // didn't always have the correct intent flags set, so do that
2008 // here
2009 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002010 intent.getCategories() != null &&
2011 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002012 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 intent.addFlags(
2014 Intent.FLAG_ACTIVITY_NEW_TASK |
2015 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2016 }
Michael Jurka96879562012-03-22 05:54:33 -07002017 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002020 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002023 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 info.cellX = c.getInt(cellXIndex);
2025 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002026 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002027 info.spanX = 1;
2028 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002029 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002030 if (info.promisedIntent != null) {
2031 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2032 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002033 info.isDisabled = disabledState;
2034 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2035 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2036 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002039 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002040 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 break;
2042 }
2043
Sunny Goyal756adbc2015-04-16 15:20:51 -07002044 if (restored) {
2045 ComponentName cn = info.getTargetComponent();
2046 if (cn != null) {
2047 Integer progress = installingPkgs.get(cn.getPackageName());
2048 if (progress != null) {
2049 info.setInstallProgress(progress);
2050 } else {
2051 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2052 }
2053 }
2054 }
2055
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 switch (container) {
2057 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2058 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2059 sBgWorkspaceItems.add(info);
2060 break;
2061 default:
2062 // Item is in a user folder
2063 FolderInfo folderInfo =
2064 findOrMakeFolder(sBgFolders, container);
2065 folderInfo.add(info);
2066 break;
2067 }
2068 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002069 } else {
2070 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 }
2072 break;
2073
2074 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2075 id = c.getLong(idIndex);
2076 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2077
Sunny Goyala508e4f2015-05-21 09:33:57 -07002078 // Do not trim the folder label, as is was set by the user.
2079 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002082 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 folderInfo.cellX = c.getInt(cellXIndex);
2084 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002085 folderInfo.spanX = 1;
2086 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002087 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088
Daniel Sandler8802e962010-05-26 16:28:16 -04002089 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002090 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002091 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002092 break;
2093 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002094
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2097 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2098 sBgWorkspaceItems.add(folderInfo);
2099 break;
Joe Onorato36115782010-06-17 13:28:48 -04002100 }
Joe Onorato17a89222011-02-08 17:26:11 -08002101
Chris Wrenf4d08112014-01-16 18:13:56 -05002102 if (restored) {
2103 // no special handling required for restored folders
2104 restoredRows.add(id);
2105 }
2106
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2108 sBgFolders.put(folderInfo.id, folderInfo);
2109 break;
2110
2111 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002112 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002114 boolean customWidget = itemType ==
2115 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2116
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002118 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002119 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002120 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002121 user = allUsers.get(serialNumber);
2122 if (user == null) {
2123 itemsToRemove.add(id);
2124 continue;
2125 }
2126
Sunny Goyalff572272014-07-23 13:58:07 -07002127 final ComponentName component =
2128 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002129
Sunny Goyal651077b2014-06-30 14:15:31 -07002130 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002131 final boolean isIdValid = (restoreStatus &
2132 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002133 final boolean wasProviderReady = (restoreStatus &
2134 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002135
Adam Cohen59400422014-03-05 18:07:04 -08002136 final LauncherAppWidgetProviderInfo provider =
2137 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002138 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002139 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002140
2141 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002142 if (!isSafeMode && !customWidget &&
2143 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002144 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002145 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002146
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002148 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 itemsToRemove.add(id);
2150 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002151 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002152 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2153 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002154
Sunny Goyal53f96722015-07-13 19:54:53 -07002155 // The provider is available. So the widget is either
2156 // available or not available. We do not need to track
2157 // any future restore updates.
2158 int status = restoreStatus &
2159 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002160 if (!wasProviderReady) {
2161 // If provider was not previously ready, update the
2162 // status and UI flag.
2163
2164 // Id would be valid only if the widget restore broadcast was received.
2165 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002166 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002167 } else {
2168 status &= ~LauncherAppWidgetInfo
2169 .FLAG_PROVIDER_NOT_READY;
2170 }
2171 }
2172 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002173 } else {
2174 Log.v(TAG, "Widget restore pending id=" + id
2175 + " appWidgetId=" + appWidgetId
2176 + " status =" + restoreStatus);
2177 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002178 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002179 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002180 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002181
2182 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2183 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002184 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002185 // App restore has started. Update the flag
2186 appWidgetInfo.restoreStatus |=
2187 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002188 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002189 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002190 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002191 itemsToRemove.add(id);
2192 continue;
2193 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002194
2195 appWidgetInfo.installProgress =
2196 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002197 }
Sunny Goyalff572272014-07-23 13:58:07 -07002198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 appWidgetInfo.cellX = c.getInt(cellXIndex);
2202 appWidgetInfo.cellY = c.getInt(cellYIndex);
2203 appWidgetInfo.spanX = c.getInt(spanXIndex);
2204 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002205 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002206
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2208 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2209 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002210 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2211 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 continue;
2213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002215 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002217 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002218 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 break;
2220 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002221
Adam Cohen59400422014-03-05 18:07:04 -08002222 if (!customWidget) {
2223 String providerName =
2224 appWidgetInfo.providerName.flattenToString();
2225 if (!providerName.equals(savedProvider) ||
2226 (appWidgetInfo.restoreStatus != restoreStatus)) {
2227 ContentValues values = new ContentValues();
2228 values.put(
2229 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2230 providerName);
2231 values.put(LauncherSettings.Favorites.RESTORED,
2232 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002233 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002234 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002235 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2237 sBgAppWidgets.add(appWidgetInfo);
2238 }
Joe Onorato36115782010-06-17 13:28:48 -04002239 break;
2240 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002242 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002243 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 }
2245 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002246 if (c != null) {
2247 c.close();
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 }
2250
Winson Chungba9c37f2013-08-30 14:11:37 -07002251 // Break early if we've stopped loading
2252 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002253 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002254 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002255 }
2256
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002259 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2260 Utilities.createDbSelectionQuery(
2261 LauncherSettings.Favorites._ID, itemsToRemove), null);
2262 if (DEBUG_LOADERS) {
2263 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2264 LauncherSettings.Favorites._ID, itemsToRemove));
2265 }
2266
2267 // Remove any empty folder
2268 for (long folderId : LauncherAppState.getLauncherProvider()
2269 .deleteEmptyFolders()) {
2270 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2271 sBgFolders.remove(folderId);
2272 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002273 }
2274 }
2275
Sunny Goyal317698b2015-07-29 11:45:41 -07002276 // Sort all the folder items and make sure the first 3 items are high resolution.
2277 for (FolderInfo folder : sBgFolders) {
2278 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2279 int pos = 0;
2280 for (ShortcutInfo info : folder.contents) {
2281 if (info.usingLowResIcon) {
2282 info.updateIcon(mIconCache, false);
2283 }
2284 pos ++;
2285 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2286 break;
2287 }
2288 }
2289 }
2290
Chris Wrenf4d08112014-01-16 18:13:56 -05002291 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002292 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002293 ContentValues values = new ContentValues();
2294 values.put(LauncherSettings.Favorites.RESTORED, 0);
2295 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2296 Utilities.createDbSelectionQuery(
2297 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002298 }
2299
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002300 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2301 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002302 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002303 null, sWorker);
2304 }
2305
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002306 // Remove any empty screens
2307 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002308 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002309 long screenId = item.screenId;
2310 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2311 unusedScreens.contains(screenId)) {
2312 unusedScreens.remove(screenId);
2313 }
2314 }
2315
2316 // If there are any empty screens remove them, and update.
2317 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002318 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 }
2321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 if (DEBUG_LOADERS) {
2323 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2324 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002326 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002328
Sunny Goyale2df0622015-04-24 11:27:00 -07002329 for (int i = 0; i < nScreens; i++) {
2330 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002331 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 line += " | ";
2333 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002334 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002335 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002336 if (x < screen.length && y < screen[x].length) {
2337 line += (screen[x][y] != null) ? "#" : ".";
2338 } else {
2339 line += "!";
2340 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 }
Joe Onorato36115782010-06-17 13:28:48 -04002342 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002345 }
Joe Onorato36115782010-06-17 13:28:48 -04002346 }
Adam Cohene25af792013-06-06 23:08:25 -07002347 }
2348
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002349 /**
2350 * Partially updates the item without any notification. Must be called on the worker thread.
2351 */
2352 private void updateItem(long itemId, ContentValues update) {
2353 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002354 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002355 update,
2356 BaseColumns._ID + "= ?",
2357 new String[]{Long.toString(itemId)});
2358 }
2359
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 /** Filters the set of items who are directly or indirectly (via another container) on the
2361 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002362 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 ArrayList<ItemInfo> allWorkspaceItems,
2364 ArrayList<ItemInfo> currentScreenItems,
2365 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002366 // Purge any null ItemInfos
2367 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2368 while (iter.hasNext()) {
2369 ItemInfo i = iter.next();
2370 if (i == null) {
2371 iter.remove();
2372 }
2373 }
2374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 // Order the set of items by their containers first, this allows use to walk through the
2376 // list sequentially, build up a list of containers that are in the specified screen,
2377 // as well as all items in those containers.
2378 Set<Long> itemsOnScreen = new HashSet<Long>();
2379 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2380 @Override
2381 public int compare(ItemInfo lhs, ItemInfo rhs) {
2382 return (int) (lhs.container - rhs.container);
2383 }
2384 });
2385 for (ItemInfo info : allWorkspaceItems) {
2386 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002387 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 currentScreenItems.add(info);
2389 itemsOnScreen.add(info.id);
2390 } else {
2391 otherScreenItems.add(info);
2392 }
2393 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2394 currentScreenItems.add(info);
2395 itemsOnScreen.add(info.id);
2396 } else {
2397 if (itemsOnScreen.contains(info.container)) {
2398 currentScreenItems.add(info);
2399 itemsOnScreen.add(info.id);
2400 } else {
2401 otherScreenItems.add(info);
2402 }
2403 }
2404 }
2405 }
2406
2407 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002408 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 ArrayList<LauncherAppWidgetInfo> appWidgets,
2410 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2411 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412
2413 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002414 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002416 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 currentScreenWidgets.add(widget);
2418 } else {
2419 otherScreenWidgets.add(widget);
2420 }
2421 }
2422 }
2423
2424 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002425 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002426 LongArrayMap<ItemInfo> itemsIdMap,
2427 LongArrayMap<FolderInfo> folders,
2428 LongArrayMap<FolderInfo> currentScreenFolders,
2429 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430
Sunny Goyale2df0622015-04-24 11:27:00 -07002431 int total = folders.size();
2432 for (int i = 0; i < total; i++) {
2433 long id = folders.keyAt(i);
2434 FolderInfo folder = folders.valueAt(i);
2435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002437 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002439 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440 currentScreenFolders.put(id, folder);
2441 } else {
2442 otherScreenFolders.put(id, folder);
2443 }
2444 }
2445 }
2446
2447 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2448 * right) */
2449 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002450 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002451 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002452 final int screenCols = profile.numColumns;
2453 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002455 @Override
2456 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002457 if (lhs.container == rhs.container) {
2458 // Within containers, order by their spatial position in that container
2459 switch ((int) lhs.container) {
2460 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2461 long lr = (lhs.screenId * screenCellCount +
2462 lhs.cellY * screenCols + lhs.cellX);
2463 long rr = (rhs.screenId * screenCellCount +
2464 rhs.cellY * screenCols + rhs.cellX);
2465 return (int) (lr - rr);
2466 }
2467 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2468 // We currently use the screen id as the rank
2469 return (int) (lhs.screenId - rhs.screenId);
2470 }
2471 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002472 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002473 throw new RuntimeException("Unexpected container type when " +
2474 "sorting workspace items.");
2475 }
2476 return 0;
2477 }
2478 } else {
2479 // Between containers, order by hotseat, desktop
2480 return (int) (lhs.container - rhs.container);
2481 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002482 }
2483 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002485
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2487 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002488 final Runnable r = new Runnable() {
2489 @Override
2490 public void run() {
2491 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2492 if (callbacks != null) {
2493 callbacks.bindScreens(orderedScreens);
2494 }
2495 }
2496 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002497 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002498 }
2499
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2501 final ArrayList<ItemInfo> workspaceItems,
2502 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002503 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002505
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 final boolean postOnMainThread = (deferredBindRunnables != null);
2507
2508 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002509 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002511 final int start = i;
2512 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 final Runnable r = new Runnable() {
2514 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002516 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002517 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002518 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2519 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 }
2521 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522 };
2523 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002524 synchronized (deferredBindRunnables) {
2525 deferredBindRunnables.add(r);
2526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002528 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529 }
Joe Onorato36115782010-06-17 13:28:48 -04002530 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531
2532 // Bind the folders
2533 if (!folders.isEmpty()) {
2534 final Runnable r = new Runnable() {
2535 public void run() {
2536 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2537 if (callbacks != null) {
2538 callbacks.bindFolders(folders);
2539 }
2540 }
2541 };
2542 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002543 synchronized (deferredBindRunnables) {
2544 deferredBindRunnables.add(r);
2545 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002547 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 }
2549 }
2550
2551 // Bind the widgets, one at a time
2552 N = appWidgets.size();
2553 for (int i = 0; i < N; i++) {
2554 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2555 final Runnable r = new Runnable() {
2556 public void run() {
2557 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2558 if (callbacks != null) {
2559 callbacks.bindAppWidget(widget);
2560 }
2561 }
2562 };
2563 if (postOnMainThread) {
2564 deferredBindRunnables.add(r);
2565 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002566 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 }
2568 }
2569 }
2570
2571 /**
2572 * Binds all loaded data to actual views on the main thread.
2573 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002574 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 final long t = SystemClock.uptimeMillis();
2576 Runnable r;
2577
2578 // Don't use these two variables in any of the callback runnables.
2579 // Otherwise we hold a reference to them.
2580 final Callbacks oldCallbacks = mCallbacks.get();
2581 if (oldCallbacks == null) {
2582 // This launcher has exited and nobody bothered to tell us. Just bail.
2583 Log.w(TAG, "LoaderTask running with no launcher");
2584 return;
2585 }
2586
Winson Chung9b9fb962013-11-15 15:39:34 -08002587 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002588 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2589 ArrayList<LauncherAppWidgetInfo> appWidgets =
2590 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002591 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002592
2593 final LongArrayMap<FolderInfo> folders;
2594 final LongArrayMap<ItemInfo> itemsIdMap;
2595
Winson Chung2abf94d2012-07-18 18:16:38 -07002596 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002597 workspaceItems.addAll(sBgWorkspaceItems);
2598 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002599 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002600
2601 folders = sBgFolders.clone();
2602 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603 }
2604
Derek Prothro7aff3992013-12-10 14:00:37 -05002605 final boolean isLoadingSynchronously =
2606 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002607 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002608 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002609 if (currScreen >= orderedScreenIds.size()) {
2610 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002611 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002612 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002613 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002614 final long currentScreenId = currentScreen < 0
2615 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002616
2617 // Load all the items that are on the current page first (and in the process, unbind
2618 // all the existing workspace items before we call startBinding() below.
2619 unbindWorkspaceItemsOnMainThread();
2620
2621 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2623 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2624 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2625 new ArrayList<LauncherAppWidgetInfo>();
2626 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2627 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002628 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2629 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630
Winson Chung9b9fb962013-11-15 15:39:34 -08002631 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002633 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002634 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002635 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002636 otherFolders);
2637 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2638 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2639
2640 // Tell the workspace that we're about to start binding items
2641 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002642 public void run() {
2643 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2644 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002646 }
2647 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002649 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002650
Adam Cohendcd297f2013-06-18 13:13:40 -07002651 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2652
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002653 // Load items on the current page
2654 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2655 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002656 if (isLoadingSynchronously) {
2657 r = new Runnable() {
2658 public void run() {
2659 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002660 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002661 callbacks.onPageBoundSynchronously(currentScreen);
2662 }
2663 }
2664 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002665 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002666 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002667
Winson Chung4a2afa32012-07-19 14:53:05 -07002668 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2669 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002670 synchronized (mDeferredBindRunnables) {
2671 mDeferredBindRunnables.clear();
2672 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002673 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002674 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002675
2676 // Tell the workspace that we're done binding items
2677 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002678 public void run() {
2679 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2680 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002681 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002682 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002683
Sunny Goyal639e9062015-08-19 19:17:06 -07002684 mIsLoadingAndBindingWorkspace = false;
2685
2686 // Run all the bind complete runnables after workspace is bound.
2687 if (!mBindCompleteRunnables.isEmpty()) {
2688 synchronized (mBindCompleteRunnables) {
2689 for (final Runnable r : mBindCompleteRunnables) {
2690 runOnWorkerThread(r);
2691 }
2692 mBindCompleteRunnables.clear();
2693 }
2694 }
2695
Winson Chung98e030b2012-05-07 16:01:11 -07002696 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002697 if (DEBUG_LOADERS) {
2698 Log.d(TAG, "bound workspace in "
2699 + (SystemClock.uptimeMillis()-t) + "ms");
2700 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002701
Joe Onorato36115782010-06-17 13:28:48 -04002702 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002703 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002704 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002705 synchronized (mDeferredBindRunnables) {
2706 mDeferredBindRunnables.add(r);
2707 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002708 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002709 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002710 }
Joe Onorato36115782010-06-17 13:28:48 -04002711 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002712
Joe Onorato36115782010-06-17 13:28:48 -04002713 private void loadAndBindAllApps() {
2714 if (DEBUG_LOADERS) {
2715 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2716 }
2717 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002718 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002719 synchronized (LoaderTask.this) {
2720 if (mStopped) {
2721 return;
2722 }
2723 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002724 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002725 synchronized (LoaderTask.this) {
2726 if (mStopped) {
2727 return;
2728 }
2729 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002730 }
Joe Onorato36115782010-06-17 13:28:48 -04002731 } else {
2732 onlyBindAllApps();
2733 }
2734 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002735
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002736 private void updateIconCache() {
2737 // Ignore packages which have a promise icon.
2738 HashSet<String> packagesToIgnore = new HashSet<>();
2739 synchronized (sBgLock) {
2740 for (ItemInfo info : sBgItemsIdMap) {
2741 if (info instanceof ShortcutInfo) {
2742 ShortcutInfo si = (ShortcutInfo) info;
2743 if (si.isPromise() && si.getTargetComponent() != null) {
2744 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2745 }
2746 } else if (info instanceof LauncherAppWidgetInfo) {
2747 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2748 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2749 packagesToIgnore.add(lawi.providerName.getPackageName());
2750 }
2751 }
2752 }
2753 }
2754 mIconCache.updateDbIcons(packagesToIgnore);
2755 }
2756
Joe Onorato36115782010-06-17 13:28:48 -04002757 private void onlyBindAllApps() {
2758 final Callbacks oldCallbacks = mCallbacks.get();
2759 if (oldCallbacks == null) {
2760 // This launcher has exited and nobody bothered to tell us. Just bail.
2761 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2762 return;
2763 }
2764
2765 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002766 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002767 final ArrayList<AppInfo> list
2768 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002769 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002770 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002771 public void run() {
2772 final long t = SystemClock.uptimeMillis();
2773 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2774 if (callbacks != null) {
2775 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002776 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002777 }
2778 if (DEBUG_LOADERS) {
2779 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2780 + (SystemClock.uptimeMillis()-t) + "ms");
2781 }
2782 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002783 };
2784 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002785 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002786 r.run();
2787 } else {
2788 mHandler.post(r);
2789 }
Joe Onorato36115782010-06-17 13:28:48 -04002790 }
2791
Winson Chung64359a52013-07-08 17:17:08 -07002792 private void loadAllApps() {
2793 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002794
Joe Onorato36115782010-06-17 13:28:48 -04002795 final Callbacks oldCallbacks = mCallbacks.get();
2796 if (oldCallbacks == null) {
2797 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002798 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002799 return;
2800 }
2801
Kenny Guyed131872014-04-30 03:02:21 +01002802 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2803
Winson Chung64359a52013-07-08 17:17:08 -07002804 // Clear the list of apps
2805 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002806 for (UserHandleCompat user : profiles) {
2807 // Query for the set of apps
2808 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002809 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002810 if (DEBUG_LOADERS) {
2811 Log.d(TAG, "getActivityList took "
2812 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2813 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2814 }
2815 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002816 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002817 if (apps == null || apps.isEmpty()) {
2818 return;
2819 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002820
Kenny Guyed131872014-04-30 03:02:21 +01002821 // Create the ApplicationInfos
2822 for (int i = 0; i < apps.size(); i++) {
2823 LauncherActivityInfoCompat app = apps.get(i);
2824 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002825 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002826 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002827
Sunny Goyal756a28a2015-04-23 17:07:55 -07002828 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2829 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002830 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002831
2832 @Override
2833 public void run() {
2834 heuristic.processUserApps(apps);
2835 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002836 };
2837 runOnMainThread(new Runnable() {
2838
2839 @Override
2840 public void run() {
2841 // Check isLoadingWorkspace on the UI thread, as it is updated on
2842 // the UI thread.
2843 if (mIsLoadingAndBindingWorkspace) {
2844 synchronized (mBindCompleteRunnables) {
2845 mBindCompleteRunnables.add(r);
2846 }
2847 } else {
2848 runOnWorkerThread(r);
2849 }
2850 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002851 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002852 }
Winson Chung64359a52013-07-08 17:17:08 -07002853 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002854 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002855 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2856 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002857
2858 // Post callback on main thread
2859 mHandler.post(new Runnable() {
2860 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002861
Winson Chung64359a52013-07-08 17:17:08 -07002862 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002863 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002864 if (callbacks != null) {
2865 callbacks.bindAllApplications(added);
2866 if (DEBUG_LOADERS) {
2867 Log.d(TAG, "bound " + added.size() + " apps in "
2868 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2869 }
2870 } else {
2871 Log.i(TAG, "not binding apps: no Launcher activity");
2872 }
2873 }
2874 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002875 // Cleanup any data stored for a deleted user.
2876 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002877
Sunny Goyal2d648b02015-08-11 13:56:28 -07002878 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002879 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002880 Log.d(TAG, "Icons processed in "
2881 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002882 }
2883 }
2884
2885 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002886 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002887 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002888 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2889 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2890 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2891 }
Joe Onorato36115782010-06-17 13:28:48 -04002892 }
2893 }
2894
Sunny Goyal75b0f552015-05-20 21:57:06 -07002895 /**
2896 * Called when the icons for packages have been updated in the icon cache.
2897 */
2898 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2899 final Callbacks callbacks = getCallback();
2900 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2901 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2902
2903 // If any package icon has changed (app was updated while launcher was dead),
2904 // update the corresponding shortcuts.
2905 synchronized (sBgLock) {
2906 for (ItemInfo info : sBgItemsIdMap) {
2907 if (info instanceof ShortcutInfo && user.equals(info.user)
2908 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2909 ShortcutInfo si = (ShortcutInfo) info;
2910 ComponentName cn = si.getTargetComponent();
2911 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2912 si.updateIcon(mIconCache);
2913 updatedShortcuts.add(si);
2914 }
2915 }
2916 }
2917 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2918 }
2919
2920 if (!updatedShortcuts.isEmpty()) {
2921 final UserHandleCompat userFinal = user;
2922 mHandler.post(new Runnable() {
2923
2924 public void run() {
2925 Callbacks cb = getCallback();
2926 if (cb != null && callbacks == cb) {
2927 cb.bindShortcutsChanged(updatedShortcuts,
2928 new ArrayList<ShortcutInfo>(), userFinal);
2929 }
2930 }
2931 });
2932 }
2933
2934 if (!updatedApps.isEmpty()) {
2935 mHandler.post(new Runnable() {
2936
2937 public void run() {
2938 Callbacks cb = getCallback();
2939 if (cb != null && callbacks == cb) {
2940 cb.bindAppsUpdated(updatedApps);
2941 }
2942 }
2943 });
2944 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002945
2946 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002947 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002948 }
2949
Joe Onorato36115782010-06-17 13:28:48 -04002950 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002951 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002952 }
2953
Adam Cohen091440a2015-03-18 14:16:05 -07002954 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002955
2956 @Override
2957 public void onReceive(Context context, Intent intent) {
2958 synchronized (sBgLock) {
2959 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2960 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002961 final PackageManager manager = context.getPackageManager();
2962 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2963 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002964 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2965 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002966 packagesRemoved.clear();
2967 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002968 for (String pkg : entry.getValue()) {
2969 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002970 boolean packageOnSdcard = launcherApps.isAppEnabled(
2971 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2972 if (packageOnSdcard) {
2973 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2974 packagesUnavailable.add(pkg);
2975 } else {
2976 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2977 packagesRemoved.add(pkg);
2978 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002979 }
2980 }
2981 if (!packagesRemoved.isEmpty()) {
2982 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2983 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2984 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002985 if (!packagesUnavailable.isEmpty()) {
2986 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2987 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2988 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002989 }
Sunny Goyal34942622014-08-29 17:20:55 -07002990 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002991 }
2992 }
2993 }
2994
Joe Onorato36115782010-06-17 13:28:48 -04002995 private class PackageUpdatedTask implements Runnable {
2996 int mOp;
2997 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002998 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002999
3000 public static final int OP_NONE = 0;
3001 public static final int OP_ADD = 1;
3002 public static final int OP_UPDATE = 2;
3003 public static final int OP_REMOVE = 3; // uninstlled
3004 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3005
3006
Kenny Guyed131872014-04-30 03:02:21 +01003007 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003008 mOp = op;
3009 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003010 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003011 }
3012
3013 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003014 if (!mHasLoaderCompletedOnce) {
3015 // Loader has not yet run.
3016 return;
3017 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003018 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003019
3020 final String[] packages = mPackages;
3021 final int N = packages.length;
3022 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003023 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003024 for (int i=0; i<N; i++) {
3025 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003026 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003027 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003028 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003029
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3031 if (heuristic != null) {
3032 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003033 }
Joe Onorato36115782010-06-17 13:28:48 -04003034 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003035 }
Joe Onorato36115782010-06-17 13:28:48 -04003036 case OP_UPDATE:
3037 for (int i=0; i<N; i++) {
3038 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003039 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003040 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003041 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003042 }
3043 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003044 case OP_REMOVE: {
3045 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3046 if (heuristic != null) {
3047 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003048 }
Joe Onorato36115782010-06-17 13:28:48 -04003049 for (int i=0; i<N; i++) {
3050 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003051 mIconCache.removeIconsForPkg(packages[i], mUser);
3052 }
3053 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003054 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003055 case OP_UNAVAILABLE:
3056 for (int i=0; i<N; i++) {
3057 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3058 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003059 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003060 }
3061 break;
3062 }
3063
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 ArrayList<AppInfo> added = null;
3065 ArrayList<AppInfo> modified = null;
3066 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003067
Adam Cohen487f7dd2012-06-28 18:12:10 -07003068 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003069 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003070 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003071 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003072 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003073 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003074 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003075 }
Winson Chung5d55f332012-07-16 20:45:03 -07003076 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003077 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003078 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003079 }
3080
Sunny Goyale0f58d72014-11-10 18:05:31 -08003081 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003082 if (callbacks == null) {
3083 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3084 return;
3085 }
3086
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3088 new HashMap<ComponentName, AppInfo>();
3089
Joe Onorato36115782010-06-17 13:28:48 -04003090 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003091 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003092 for (AppInfo ai : added) {
3093 addedOrUpdatedApps.put(ai.componentName, ai);
3094 }
Joe Onorato36115782010-06-17 13:28:48 -04003095 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003096
Joe Onorato36115782010-06-17 13:28:48 -04003097 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003098 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 for (AppInfo ai : modified) {
3100 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003101 }
3102
Joe Onorato36115782010-06-17 13:28:48 -04003103 mHandler.post(new Runnable() {
3104 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003105 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003106 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003107 callbacks.bindAppsUpdated(modifiedFinal);
3108 }
3109 }
3110 });
3111 }
Winson Chung83892cc2013-05-01 16:53:33 -07003112
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3116 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3117 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3118
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003119 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003120 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003121 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003122 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3123 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 boolean infoUpdated = false;
3125 boolean shortcutUpdated = false;
3126
3127 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003129 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003130 Bitmap icon = Utilities.createIconBitmap(
3131 si.iconResource.packageName,
3132 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003133 if (icon != null) {
3134 si.setIcon(icon);
3135 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003137 }
3138 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139
3140 ComponentName cn = si.getTargetComponent();
3141 if (cn != null && packageSet.contains(cn.getPackageName())) {
3142 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3143
3144 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3146 // Auto install icon
3147 PackageManager pm = context.getPackageManager();
3148 ResolveInfo matched = pm.resolveActivity(
3149 new Intent(Intent.ACTION_MAIN)
3150 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3151 PackageManager.MATCH_DEFAULT_ONLY);
3152 if (matched == null) {
3153 // Try to find the best match activity.
3154 Intent intent = pm.getLaunchIntentForPackage(
3155 cn.getPackageName());
3156 if (intent != null) {
3157 cn = intent.getComponent();
3158 appInfo = addedOrUpdatedApps.get(cn);
3159 }
3160
3161 if ((intent == null) || (appInfo == null)) {
3162 removedShortcuts.add(si);
3163 continue;
3164 }
3165 si.promisedIntent = intent;
3166 }
3167 }
3168
3169 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003170 if (appInfo != null) {
3171 si.flags = appInfo.flags;
3172 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003173
Sunny Goyal756adbc2015-04-16 15:20:51 -07003174 si.intent = si.promisedIntent;
3175 si.promisedIntent = null;
3176 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 infoUpdated = true;
3178 si.updateIcon(mIconCache);
3179 }
3180
3181 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3182 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3183 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003184 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003185 si.contentDescription = appInfo.contentDescription;
3186 infoUpdated = true;
3187 }
3188
3189 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3190 // Since package was just updated, the target must be available now.
3191 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3192 shortcutUpdated = true;
3193 }
3194 }
3195
3196 if (infoUpdated || shortcutUpdated) {
3197 updatedShortcuts.add(si);
3198 }
3199 if (infoUpdated) {
3200 updateItemInDatabase(context, si);
3201 }
3202 } else if (info instanceof LauncherAppWidgetInfo) {
3203 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3204 if (mUser.equals(widgetInfo.user)
3205 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3206 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003207 widgetInfo.restoreStatus &=
3208 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3209 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003210
3211 // adding this flag ensures that launcher shows 'click to setup'
3212 // if the widget has a config activity. In case there is no config
3213 // activity, it will be marked as 'restored' during bind.
3214 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3215
Sunny Goyal4390ace2014-10-13 11:33:11 -07003216 widgets.add(widgetInfo);
3217 updateItemInDatabase(context, widgetInfo);
3218 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003219 }
3220 }
3221 }
3222
Sunny Goyal4390ace2014-10-13 11:33:11 -07003223 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3224 mHandler.post(new Runnable() {
3225
3226 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003227 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003228 if (callbacks == cb && cb != null) {
3229 callbacks.bindShortcutsChanged(
3230 updatedShortcuts, removedShortcuts, mUser);
3231 }
3232 }
3233 });
3234 if (!removedShortcuts.isEmpty()) {
3235 deleteItemsFromDatabase(context, removedShortcuts);
3236 }
3237 }
3238 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003239 mHandler.post(new Runnable() {
3240 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003241 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003242 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003243 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003244 }
3245 }
3246 });
3247 }
3248 }
3249
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 final ArrayList<String> removedPackageNames =
3251 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003252 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 // Mark all packages in the broadcast to be removed
3254 removedPackageNames.addAll(Arrays.asList(packages));
3255 } else if (mOp == OP_UPDATE) {
3256 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003258 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003259 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003260 }
3261 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003262 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003263
Winson Chungdf95eb12013-10-16 14:57:07 -07003264 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003265 final int removeReason;
3266 if (mOp == OP_UNAVAILABLE) {
3267 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3268 } else {
3269 // Remove all the components associated with this package
3270 for (String pn : removedPackageNames) {
3271 deletePackageFromDatabase(context, pn, mUser);
3272 }
3273 // Remove all the specific components
3274 for (AppInfo a : removedApps) {
3275 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3276 deleteItemsFromDatabase(context, infos);
3277 }
3278 removeReason = 0;
3279 }
3280
Winson Chungdf95eb12013-10-16 14:57:07 -07003281 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003282 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003283 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003284 mHandler.post(new Runnable() {
3285 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003286 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003287 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003288 callbacks.bindComponentsRemoved(
3289 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003290 }
3291 }
3292 });
Joe Onoratobe386092009-11-17 17:32:16 -08003293 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003294
Sunny Goyal2d648b02015-08-11 13:56:28 -07003295 // Update widgets
3296 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3297 // Always refresh for a package event on secondary user
3298 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3299
3300 // Refresh widget list, if the package already had a widget.
3301 synchronized (sBgLock) {
3302 if (sBgWidgetProviders != null) {
3303 HashSet<String> pkgSet = new HashSet<>();
3304 Collections.addAll(pkgSet, mPackages);
3305
3306 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3307 needToRefresh |= key.user.equals(mUser) &&
3308 pkgSet.contains(key.componentName.getPackageName());
3309 }
3310 }
3311 }
3312
3313 if (!needToRefresh && mOp != OP_REMOVE) {
3314 // Refresh widget list, if there is any newly added widget
3315 PackageManager pm = context.getPackageManager();
3316 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003317 try {
3318 needToRefresh |= !pm.queryBroadcastReceivers(
3319 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
3320 .setPackage(pkg), 0).isEmpty();
3321 } catch (RuntimeException e) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003322 if (LauncherAppState.isDogfoodBuild()) {
3323 throw e;
3324 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003325 // Ignore the crash. We can live with a state widget list.
3326 Log.e(TAG, "PM call failed for " + pkg, e);
3327 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003328 }
3329 }
3330
3331 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3332 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003333
Adam Cohen4caf2982013-08-20 18:54:31 -07003334 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003335 mHandler.post(new Runnable() {
3336 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003337 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003338 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003339 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003340 }
3341 }
3342 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 }
3344 }
3345
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003346 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3347 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003348 ArrayList<LauncherAppWidgetProviderInfo> results =
3349 new ArrayList<LauncherAppWidgetProviderInfo>();
3350 try {
3351 synchronized (sBgLock) {
3352 if (sBgWidgetProviders == null || refresh) {
3353 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3354 = new HashMap<>();
3355 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3356 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003357
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003358 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3359 for (AppWidgetProviderInfo pInfo : widgets) {
3360 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3361 UserHandleCompat user = wm.getUser(info);
3362 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3363 }
Robin Lee26ace122015-03-16 19:41:43 +00003364
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003365 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3366 for (CustomAppWidget widget : customWidgets) {
3367 info = new LauncherAppWidgetProviderInfo(context, widget);
3368 UserHandleCompat user = wm.getUser(info);
3369 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3370 }
3371 // Replace the global list at the very end, so that if there is an exception,
3372 // previously loaded provider list is used.
3373 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003374 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003375 results.addAll(sBgWidgetProviders.values());
3376 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003377 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003378 } catch (Exception e) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003379 if (!LauncherAppState.isDogfoodBuild() &&
3380 (e.getCause() instanceof TransactionTooLargeException ||
3381 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003382 // the returned value may be incomplete and will not be refreshed until the next
3383 // time Launcher starts.
3384 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3385 // onResume is called to refresh the widget provider list.
3386 synchronized (sBgLock) {
3387 if (sBgWidgetProviders != null) {
3388 results.addAll(sBgWidgetProviders.values());
3389 }
3390 return results;
3391 }
3392 } else {
3393 throw e;
3394 }
Adam Cohen59400422014-03-05 18:07:04 -08003395 }
3396 }
3397
Robin Lee26ace122015-03-16 19:41:43 +00003398 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3399 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003400 synchronized (sBgLock) {
3401 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003402 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003403 }
Robin Lee26ace122015-03-16 19:41:43 +00003404 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003405 }
3406 }
3407
Sunny Goyal2d648b02015-08-11 13:56:28 -07003408 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003409
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003410 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003411 @Override
3412 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003413 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003414 final WidgetsModel model = mBgWidgetsModel.clone();
3415
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003416 mHandler.post(new Runnable() {
3417 @Override
3418 public void run() {
3419 Callbacks cb = getCallback();
3420 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003421 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003422 }
3423 }
3424 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003425 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003426 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3427 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003428 }
3429 });
3430 }
3431
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003432 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003433 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003434 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003435 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003436 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003437 @Thunk void updateWidgetsModel(boolean refresh) {
3438 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003439 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003440 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003441 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3442 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003443 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003444 }
3445
Adam Cohen091440a2015-03-18 14:16:05 -07003446 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003447 UserHandleCompat user) {
3448 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3449 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003450 }
Adam Cohen556f6132014-01-15 15:18:08 -08003451
Kenny Guyed131872014-04-30 03:02:21 +01003452 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3453 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003454 if (cn == null) {
3455 return false;
3456 }
Kenny Guyed131872014-04-30 03:02:21 +01003457 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3458 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003459 return false;
3460 }
Kenny Guyed131872014-04-30 03:02:21 +01003461 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003462 }
3463
Adam Cohena28b78e2014-05-20 17:03:04 -07003464 public static boolean isValidPackage(Context context, String packageName,
3465 UserHandleCompat user) {
3466 if (packageName == null) {
3467 return false;
3468 }
3469 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3470 return launcherApps.isPackageEnabledForProfile(packageName, user);
3471 }
3472
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003474 * Make an ShortcutInfo object for a restored application or shortcut item that points
3475 * to a package that is not yet installed on the system.
3476 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003477 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003478 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003479 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003480 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003481
3482 Bitmap icon = iconInfo.loadIcon(c, info, context);
3483 // the fallback icon
3484 if (icon == null) {
3485 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3486 } else {
3487 info.setIcon(icon);
3488 }
Sunny Goyal34942622014-08-29 17:20:55 -07003489
3490 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003491 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003492 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003493 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003494 }
Sunny Goyal34942622014-08-29 17:20:55 -07003495 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3496 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003497 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003498 }
Sunny Goyal34942622014-08-29 17:20:55 -07003499 } else {
3500 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3501 }
3502
Winson Chung82b016c2015-05-08 17:00:10 -07003503 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003504 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003505 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003506 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003507 return info;
3508 }
3509
3510 /**
3511 * Make an Intent object for a restored application or shortcut item that points
3512 * to the market page for the item.
3513 */
Adam Cohen091440a2015-03-18 14:16:05 -07003514 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003515 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003516 return getMarketIntent(componentName.getPackageName());
3517 }
3518
3519 static Intent getMarketIntent(String packageName) {
3520 return new Intent(Intent.ACTION_VIEW)
3521 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003522 .scheme("market")
3523 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003524 .appendQueryParameter("id", packageName)
3525 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003526 }
3527
3528 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003529 * Make an ShortcutInfo object for a shortcut that is an application.
3530 *
3531 * If c is not null, then it will be used to fill in missing data like the title and icon.
3532 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003533 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003534 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003535 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003536 if (user == null) {
3537 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003538 return null;
3539 }
3540
Kenny Guyed131872014-04-30 03:02:21 +01003541 ComponentName componentName = intent.getComponent();
3542 if (componentName == null) {
3543 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3544 return null;
3545 }
3546
3547 Intent newIntent = new Intent(intent.getAction(), null);
3548 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3549 newIntent.setComponent(componentName);
3550 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003551 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003552 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3553 return null;
3554 }
3555
3556 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003557 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003558 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3559 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3560 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003561 }
3562
Joe Onorato56d82912010-03-07 14:32:10 -05003563 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003564 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003565 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003566 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003567
Joe Onorato56d82912010-03-07 14:32:10 -05003568 // fall back to the class name of the activity
3569 if (info.title == null) {
3570 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003571 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003574 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003575 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003576 if (lai != null) {
3577 info.flags = AppInfo.initFlags(lai);
3578 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003581
Sunny Goyale2df0622015-04-24 11:27:00 -07003582 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003583 ItemInfoFilter f) {
3584 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3585 for (ItemInfo i : infos) {
3586 if (i instanceof ShortcutInfo) {
3587 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003588 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003589 if (cn != null && f.filterItem(null, info, cn)) {
3590 filtered.add(info);
3591 }
3592 } else if (i instanceof FolderInfo) {
3593 FolderInfo info = (FolderInfo) i;
3594 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003595 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003596 if (cn != null && f.filterItem(info, s, cn)) {
3597 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003598 }
3599 }
Winson Chung64359a52013-07-08 17:17:08 -07003600 } else if (i instanceof LauncherAppWidgetInfo) {
3601 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3602 ComponentName cn = info.providerName;
3603 if (cn != null && f.filterItem(null, info, cn)) {
3604 filtered.add(info);
3605 }
Winson Chung8a435102012-08-30 17:16:53 -07003606 }
3607 }
Winson Chung64359a52013-07-08 17:17:08 -07003608 return new ArrayList<ItemInfo>(filtered);
3609 }
3610
Adam Cohen091440a2015-03-18 14:16:05 -07003611 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003612 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003613 ItemInfoFilter filter = new ItemInfoFilter() {
3614 @Override
3615 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003616 if (info.user == null) {
3617 return cn.equals(cname);
3618 } else {
3619 return cn.equals(cname) && info.user.equals(user);
3620 }
Winson Chung64359a52013-07-08 17:17:08 -07003621 }
3622 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003623 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003624 }
3625
Sunny Goyal1a745e82014-10-02 15:58:31 -07003626 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003627 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003628 */
Adam Cohen091440a2015-03-18 14:16:05 -07003629 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003630 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003631 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003632 // Non-app shortcuts are only supported for current user.
3633 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003635
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003636 // TODO: If there's an explicit component and we can't install that, delete it.
3637
Winson Chung82b016c2015-05-08 17:00:10 -07003638 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003639
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003640 Bitmap icon = iconInfo.loadIcon(c, info, context);
3641 // the fallback icon
3642 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003643 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003644 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003646 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 return info;
3648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649
Sunny Goyal2350bc92014-10-14 16:42:54 -07003650 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003651 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3652 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3653 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3654
Adam Cohend9198822011-11-22 16:42:47 -08003655 if (intent == null) {
3656 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3657 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3658 return null;
3659 }
3660
Joe Onorato0589f0f2010-02-08 13:44:00 -08003661 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003662 boolean customIcon = false;
3663 ShortcutIconResource iconResource = null;
3664
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003665 if (bitmap instanceof Bitmap) {
3666 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003667 customIcon = true;
3668 } else {
3669 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003670 if (extra instanceof ShortcutIconResource) {
3671 iconResource = (ShortcutIconResource) extra;
3672 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003673 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003674 }
3675 }
3676
Michael Jurkac9d95c52011-08-29 14:03:34 -07003677 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003678
Kenny Guyed131872014-04-30 03:02:21 +01003679 // Only support intents for current user for now. Intents sent from other
3680 // users wouldn't get here without intent forwarding anyway.
3681 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003682 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003683 icon = mIconCache.getDefaultIcon(info.user);
3684 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003685 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003686 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003687
Winson Chung82b016c2015-05-08 17:00:10 -07003688 info.title = Utilities.trim(name);
3689 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003690 info.intent = intent;
3691 info.customIcon = customIcon;
3692 info.iconResource = iconResource;
3693
3694 return info;
3695 }
3696
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003698 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 * or make a new one.
3700 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003701 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 // See if a placeholder was created for us already
3703 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003704 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003706 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 folders.put(id, folderInfo);
3708 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003709 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003710 }
3711
Joe Onoratobe386092009-11-17 17:32:16 -08003712
Sunny Goyal651077b2014-06-30 14:15:31 -07003713 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3714 return (provider != null) && (provider.provider != null)
3715 && (provider.provider.getPackageName() != null);
3716 }
3717
Joe Onoratobe386092009-11-17 17:32:16 -08003718 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003719 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003720 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3721 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3722 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3723 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003724 if (mLoaderTask != null) {
3725 mLoaderTask.dumpState();
3726 } else {
3727 Log.d(TAG, "mLoaderTask=null");
3728 }
Joe Onoratobe386092009-11-17 17:32:16 -08003729 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003730
3731 public Callbacks getCallback() {
3732 return mCallbacks != null ? mCallbacks.get() : null;
3733 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003734
3735 /**
3736 * @return {@link FolderInfo} if its already loaded.
3737 */
3738 public FolderInfo findFolderById(Long folderId) {
3739 synchronized (sBgLock) {
3740 return sBgFolders.get(folderId);
3741 }
3742 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003743
3744 /**
3745 * @return the looper for the worker thread which can be used to start background tasks.
3746 */
3747 public static Looper getWorkerLooper() {
3748 return sWorkerThread.getLooper();
3749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003750}