blob: 30072695c31a602249f000f9e10069b275a0d2d8 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070040import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070044import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040045import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080046import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070049import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010050
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.LauncherActivityInfoCompat;
53import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070054import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070055import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.UserHandleCompat;
57import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070058import com.android.launcher3.config.ProviderConfig;
Sunny Goyale5bb7052015-07-27 14:36:07 -070059import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070066import com.android.launcher3.util.ViewOnDrawExecutor;
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;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070083
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084/**
85 * Maintains in-memory state of the Launcher. It is expected that there should be only one
86 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070087 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 */
Kenny Guyed131872014-04-30 03:02:21 +010089public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010090 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080091 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040092 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070093 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040094
Joe Onorato9c1289c2009-08-17 11:03:03 -040095 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070096
Dan Sandlerd5024042014-01-09 15:01:33 -050097 public static final int LOADER_FLAG_NONE = 0;
98 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
99 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
100
Joe Onorato36115782010-06-17 13:28:48 -0400101 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500102 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800105 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400106
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk final LauncherAppState mApp;
108 @Thunk final Object mLock = new Object();
109 @Thunk DeferredHandler mHandler = new DeferredHandler();
110 @Thunk LoaderTask mLoaderTask;
111 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700112 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Jason Monkbbe1e242014-05-16 17:37:34 -0400114 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700115
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117 static {
118 sWorkerThread.start();
119 }
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 // We start off with everything not loaded. After that, we assume that
123 // our monitoring of the package manager provides all updates and we never
124 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk boolean mWorkspaceLoaded;
126 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700127
Sunny Goyal756a28a2015-04-23 17:07:55 -0700128 /**
129 * Set of runnables to be called on the background thread after the workspace binding
130 * is complete.
131 */
132 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
133
Adam Cohen091440a2015-03-18 14:16:05 -0700134 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700136 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700137 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700138 // Entire list of widgets.
139 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700141 // The lock that must be acquired before referencing any static bg data structures. Unlike
142 // other locks, this one can generally be held long-term because we never expect any of these
143 // static data structures to be referenced outside of the worker thread except on the first
144 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700145 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700148 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700149 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
152 // created by LauncherModel that are directly on the home screen (however, no widgets or
153 // shortcuts within folders).
154 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
157 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158 new ArrayList<LauncherAppWidgetInfo>();
159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700161 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700162
Adam Cohendcd297f2013-06-18 13:13:40 -0700163 // sBgWorkspaceScreens is the ordered set of workspace screens.
164 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
165
Adam Cohen59400422014-03-05 18:07:04 -0800166 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000167 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800168
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700169 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700170 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
171 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700172
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173 // </ only access in worker thread >
174
Adam Cohen091440a2015-03-18 14:16:05 -0700175 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk final LauncherAppsCompat mLauncherApps;
178 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700181 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700183 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700185 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
186 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700187 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700188 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700189 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800190 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200192 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700193 public void bindAppsAdded(ArrayList<Long> newScreens,
194 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700195 ArrayList<ItemInfo> addAnimated,
196 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200197 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700198 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
199 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
200 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700201 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700202 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700203 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700204 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700205 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700206 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700207 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700208 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Winson Chungede41292013-09-19 16:27:36 -0700209 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Winson Chungee055712013-07-30 14:46:24 -0700219 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700220 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400221 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
222 // resource string.
223 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
224 ProviderInfo providerInfo =
225 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
226 ProviderInfo redirectProvider =
227 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700228
229 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400230 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 if (mOldContentProviderExists) {
233 Log.d(TAG, "Old launcher provider exists.");
234 } else {
235 Log.d(TAG, "Old launcher provider does not exist.");
236 }
237
Daniel Sandlere4f98912013-06-25 15:13:26 -0400238 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100239 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700240 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Kenny Guyed131872014-04-30 03:02:21 +0100243 mLauncherApps = LauncherAppsCompat.getInstance(context);
244 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
248 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 // If we are on the worker thread, post onto the main handler
252 mHandler.post(r);
253 } else {
254 r.run();
255 }
256 }
257
258 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
259 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700260 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700261 if (sWorkerThread.getThreadId() == Process.myTid()) {
262 r.run();
263 } else {
264 // If we are not on the worker thread, then post to the worker handler
265 sWorker.post(r);
266 }
267 }
268
Winson Chunge43a1e72014-01-15 10:33:02 -0800269 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
270 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800271 }
272
Sunny Goyal756adbc2015-04-16 15:20:51 -0700273 public void setPackageState(final PackageInstallInfo installInfo) {
274 Runnable updateRunnable = new Runnable() {
275
276 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500277 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700278 synchronized (sBgLock) {
279 final HashSet<ItemInfo> updates = new HashSet<>();
280
281 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
282 // Ignore install success events as they are handled by Package add events.
283 return;
284 }
285
Sunny Goyale2df0622015-04-24 11:27:00 -0700286 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700287 if (info instanceof ShortcutInfo) {
288 ShortcutInfo si = (ShortcutInfo) info;
289 ComponentName cn = si.getTargetComponent();
290 if (si.isPromise() && (cn != null)
291 && installInfo.packageName.equals(cn.getPackageName())) {
292 si.setInstallProgress(installInfo.progress);
293
294 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
295 // Mark this info as broken.
296 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
297 }
298 updates.add(si);
299 }
300 }
301 }
302
303 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
304 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
305 widget.installProgress = installInfo.progress;
306 updates.add(widget);
307 }
308 }
309
310 if (!updates.isEmpty()) {
311 // Push changes to the callback.
312 Runnable r = new Runnable() {
313 public void run() {
314 Callbacks callbacks = getCallback();
315 if (callbacks != null) {
316 callbacks.bindRestoreItemsChange(updates);
317 }
318 }
319 };
320 mHandler.post(r);
321 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500322 }
323 }
324 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700325 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500326 }
327
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 /**
329 * Updates the icons and label of all pending icons for the provided package name.
330 */
331 public void updateSessionDisplayInfo(final String packageName) {
332 Runnable updateRunnable = new Runnable() {
333
334 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700335 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700336 synchronized (sBgLock) {
337 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
338 final UserHandleCompat user = UserHandleCompat.myUserHandle();
339
Sunny Goyale2df0622015-04-24 11:27:00 -0700340 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 if (info instanceof ShortcutInfo) {
342 ShortcutInfo si = (ShortcutInfo) info;
343 ComponentName cn = si.getTargetComponent();
344 if (si.isPromise() && (cn != null)
345 && packageName.equals(cn.getPackageName())) {
346 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
347 // For auto install apps update the icon as well as label.
348 mIconCache.getTitleAndIcon(si,
349 si.promisedIntent, user,
350 si.shouldUseLowResIcon());
351 } else {
352 // Only update the icon for restored apps.
353 si.updateIcon(mIconCache);
354 }
355 updates.add(si);
356 }
357 }
358 }
359
360 if (!updates.isEmpty()) {
361 // Push changes to the callback.
362 Runnable r = new Runnable() {
363 public void run() {
364 Callbacks callbacks = getCallback();
365 if (callbacks != null) {
366 callbacks.bindShortcutsChanged(updates,
367 new ArrayList<ShortcutInfo>(), user);
368 }
369 }
370 };
371 mHandler.post(r);
372 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700373 }
374 }
375 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700376 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700377 }
378
Adam Cohen76a47a12014-02-05 11:47:43 -0800379 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800380 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800381
382 if (allAppsApps == null) {
383 throw new RuntimeException("allAppsApps must not be null");
384 }
385 if (allAppsApps.isEmpty()) {
386 return;
387 }
388
389 // Process the newly added applications and add them to the database first
390 Runnable r = new Runnable() {
391 public void run() {
392 runOnMainThread(new Runnable() {
393 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800394 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800395 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500396 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800397 }
398 }
399 });
400 }
401 };
402 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700403 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800404
Sunny Goyala9116722015-04-29 13:55:58 -0700405 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800406 int[] xy, int spanX, int spanY) {
407 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700408 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
409 final int xCount = (int) profile.numColumns;
410 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 boolean[][] occupied = new boolean[xCount][yCount];
412 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700413 for (ItemInfo r : occupiedPos) {
414 int right = r.cellX + r.spanX;
415 int bottom = r.cellY + r.spanY;
416 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
417 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418 occupied[x][y] = true;
419 }
420 }
421 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800422 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700423 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 }
425
426 /**
427 * Find a position on the screen for the given size or adds a new screen.
428 * @return screenId and the coordinates for the item.
429 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700430 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 ArrayList<Long> workspaceScreens,
433 ArrayList<Long> addedWorkspaceScreensFinal,
434 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700435 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436
Sunny Goyala9116722015-04-29 13:55:58 -0700437 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700438 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700439 synchronized (sBgLock) {
440 for (ItemInfo info : sBgItemsIdMap) {
441 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
442 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
443 if (items == null) {
444 items = new ArrayList<>();
445 screenItems.put(info.screenId, items);
446 }
447 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
451
452 // Find appropriate space for the item.
453 long screenId = 0;
454 int[] cordinates = new int[2];
455 boolean found = false;
456
457 int screenCount = workspaceScreens.size();
458 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700459 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800460 if (preferredScreenIndex < screenCount) {
461 screenId = workspaceScreens.get(preferredScreenIndex);
462 found = findNextAvailableIconSpaceInScreen(
463 screenItems.get(screenId), cordinates, spanX, spanY);
464 }
465
466 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700467 // Search on any of the screens starting from the first screen.
468 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 screenId = workspaceScreens.get(screen);
470 if (findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY)) {
472 // We found a space for it
473 found = true;
474 break;
475 }
476 }
477 }
478
479 if (!found) {
480 // Still no position found. Add a new screen to the end.
481 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
482
483 // Save the screen id for binding in the workspace
484 workspaceScreens.add(screenId);
485 addedWorkspaceScreensFinal.add(screenId);
486
487 // If we still can't find an empty space, then God help us all!!!
488 if (!findNextAvailableIconSpaceInScreen(
489 screenItems.get(screenId), cordinates, spanX, spanY)) {
490 throw new RuntimeException("Can't find space to add the item");
491 }
492 }
493 return Pair.create(screenId, cordinates);
494 }
495
496 /**
497 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700499 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700500 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800502 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700503 return;
Winson Chung997a9232013-07-24 15:33:46 -0700504 }
Winson Chung64359a52013-07-08 17:17:08 -0700505 // Process the newly added applications and add them to the database first
506 Runnable r = new Runnable() {
507 public void run() {
508 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
509 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
510
Winson Chung76828c82013-08-19 15:43:29 -0700511 // Get the list of workspace screens. We need to append to this list and
512 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
513 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800514 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700515 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700517 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700519 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700521 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 }
Winson Chung76828c82013-08-19 15:43:29 -0700523
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700525 Pair<Long, int[]> coords = findSpaceForItem(context,
526 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 1, 1);
528 long screenId = coords.first;
529 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700530
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700531 ItemInfo itemInfo;
532 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
533 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700536 } else {
537 throw new RuntimeException("Unexpected info type");
538 }
Winson Chung94d67682013-09-25 16:29:40 -0700539
Winson Chung64359a52013-07-08 17:17:08 -0700540 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700541 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700542 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700543 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700544 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
547 }
548
Winson Chung76828c82013-08-19 15:43:29 -0700549 // Update the workspace screens
550 updateWorkspaceScreenOrder(context, workspaceScreens);
551
Adam Cohen76a47a12014-02-05 11:47:43 -0800552 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700553 runOnMainThread(new Runnable() {
554 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800555 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700556 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700557 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
558 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700559 if (!addedShortcutsFinal.isEmpty()) {
560 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
561 long lastScreenId = info.screenId;
562 for (ItemInfo i : addedShortcutsFinal) {
563 if (i.screenId == lastScreenId) {
564 addAnimated.add(i);
565 } else {
566 addNotAnimated.add(i);
567 }
Winson Chung997a9232013-07-24 15:33:46 -0700568 }
569 }
Winson Chungd64d1762013-08-20 14:37:16 -0700570 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800571 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700572 }
Winson Chung64359a52013-07-08 17:17:08 -0700573 }
Winson Chung997a9232013-07-24 15:33:46 -0700574 });
575 }
Winson Chung64359a52013-07-08 17:17:08 -0700576 }
577 };
578 runOnWorkerThread(r);
579 }
580
Sunny Goyald33860f2015-04-23 16:02:20 -0700581 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700582 if (sWorkerThread.getThreadId() == Process.myTid()) {
583 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
584 "main thread");
585 }
586
Sunny Goyald33860f2015-04-23 16:02:20 -0700587 // Remove any queued UI runnables
588 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700589 // Unbind all the workspace items
590 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700591 }
592
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700593 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700594 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700595 // Ensure that we don't use the same workspace items data structure on the main thread
596 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700597 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700598 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700599 tmpItems.addAll(sBgWorkspaceItems);
600 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700601 }
602 Runnable r = new Runnable() {
603 @Override
604 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700605 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606 item.unbind();
607 }
608 }
609 };
610 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700611 }
612
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 /**
614 * Adds an item to the DB if it was not created previously, or move it to a new
615 * <container, screen, cellX, cellY>
616 */
617 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 if (item.container == ItemInfo.NO_ID) {
620 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700621 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 } else {
623 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700624 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
626 }
627
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700628 static void checkItemInfoLocked(
629 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
630 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
631 if (modelItem != null && item != modelItem) {
632 // check all the data is consistent
633 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
634 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
635 ShortcutInfo shortcut = (ShortcutInfo) item;
636 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
637 modelShortcut.intent.filterEquals(shortcut.intent) &&
638 modelShortcut.id == shortcut.id &&
639 modelShortcut.itemType == shortcut.itemType &&
640 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700642 modelShortcut.cellX == shortcut.cellX &&
643 modelShortcut.cellY == shortcut.cellY &&
644 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700645 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 // For all intents and purposes, this is the same object
647 return;
648 }
649 }
650
651 // the modelItem needs to match up perfectly with item if our model is
652 // to be consistent with the database-- for now, just require
653 // modelItem == item or the equality check above
654 String msg = "item: " + ((item != null) ? item.toString() : "null") +
655 "modelItem: " +
656 ((modelItem != null) ? modelItem.toString() : "null") +
657 "Error: ItemInfo passed to checkItemInfo doesn't match original";
658 RuntimeException e = new RuntimeException(msg);
659 if (stackTrace != null) {
660 e.setStackTrace(stackTrace);
661 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800662 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700663 }
664 }
665
Michael Jurka816474f2012-06-25 14:49:02 -0700666 static void checkItemInfo(final ItemInfo item) {
667 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
668 final long itemId = item.id;
669 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700670 public void run() {
671 synchronized (sBgLock) {
672 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700673 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 }
675 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700677 }
678
Michael Jurkac9d95c52011-08-29 14:03:34 -0700679 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
680 final ItemInfo item, final String callingFunction) {
681 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700682 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700683 final ContentResolver cr = context.getContentResolver();
684
Adam Cohen487f7dd2012-06-28 18:12:10 -0700685 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 Runnable r = new Runnable() {
687 public void run() {
688 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689 updateItemArrays(item, itemId, stackTrace);
690 }
691 };
692 runOnWorkerThread(r);
693 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700695 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
696 final ArrayList<ItemInfo> items, final String callingFunction) {
697 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700698
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
700 Runnable r = new Runnable() {
701 public void run() {
702 ArrayList<ContentProviderOperation> ops =
703 new ArrayList<ContentProviderOperation>();
704 int count = items.size();
705 for (int i = 0; i < count; i++) {
706 ItemInfo item = items.get(i);
707 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700708 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700709 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700710
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700711 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
712 updateItemArrays(item, itemId, stackTrace);
713
714 }
715 try {
716 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
717 } catch (Exception e) {
718 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719 }
720 }
721 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700722 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700724
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
726 // Lock on mBgLock *after* the db operation
727 synchronized (sBgLock) {
728 checkItemInfoLocked(itemId, item, stackTrace);
729
730 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
731 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
732 // Item is in a folder, make sure this folder exists
733 if (!sBgFolders.containsKey(item.container)) {
734 // An items container is being set to a that of an item which is not in
735 // the list of Folders.
736 String msg = "item: " + item + " container being set to: " +
737 item.container + ", not in the list of folders";
738 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700739 }
740 }
741
742 // Items are added/removed from the corresponding FolderInfo elsewhere, such
743 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
744 // that are on the desktop, as appropriate
745 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800746 if (modelItem != null &&
747 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
748 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 switch (modelItem.itemType) {
750 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
751 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
752 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
753 if (!sBgWorkspaceItems.contains(modelItem)) {
754 sBgWorkspaceItems.add(modelItem);
755 }
756 break;
757 default:
758 break;
759 }
760 } else {
761 sBgWorkspaceItems.remove(modelItem);
762 }
763 }
764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400767 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700768 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700769 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700770 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 item.cellX = cellX;
773 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700774
Winson Chung3d503fb2011-07-13 17:25:49 -0700775 // We store hotseat items in canonical form which is this orientation invariant position
776 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700780 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783
784 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
787 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800788 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790
Michael Jurkac9d95c52011-08-29 14:03:34 -0700791 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700792 }
793
794 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700795 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
796 * cellX, cellY have already been updated on the ItemInfos.
797 */
798 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
799 final long container, final int screen) {
800
801 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
802 int count = items.size();
803
804 for (int i = 0; i < count; i++) {
805 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 item.container = container;
807
808 // We store hotseat items in canonical form which is this orientation invariant position
809 // in the hotseat
810 if (context instanceof Launcher && screen < 0 &&
811 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 item.cellY);
814 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 }
817
818 final ContentValues values = new ContentValues();
819 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
820 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
821 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800822 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824
825 contentValues.add(values);
826 }
827 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
828 }
829
830 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700831 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800832 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700833 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700835 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800836 item.cellX = cellX;
837 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 item.spanX = spanX;
839 item.spanY = spanY;
840
841 // We store hotseat items in canonical form which is this orientation invariant position
842 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700844 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700846 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700848 }
Adam Cohend4844c32011-02-18 19:25:06 -0800849
Adam Cohend4844c32011-02-18 19:25:06 -0800850 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800851 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
853 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800854 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
856 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800858
Michael Jurka816474f2012-06-25 14:49:02 -0700859 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700861
862 /**
863 * Update an item to the database in a specified container.
864 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700865 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700866 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100867 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800869 }
870
Sunny Goyal756a28a2015-04-23 17:07:55 -0700871 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700872 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700873 synchronized (mLock) {
874 if (!mHasLoaderCompletedOnce ||
875 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
876 throw new RuntimeException("Trying to add shortcut while loader is running");
877 }
878 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700879 }
880 }
881
Adam Cohend4844c32011-02-18 19:25:06 -0800882 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700883 * Returns true if the shortcuts already exists on the workspace. This must be called after
884 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700886 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
887 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700888 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700889 if (intent.getComponent() != null) {
890 // If component is not null, an intent with null package will produce
891 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700892 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700894 intentWithPkg = intent.toUri(0);
895 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700897 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
898 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700899 }
900 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = intent.toUri(0);
902 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700904
905 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700906 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700907 if (item instanceof ShortcutInfo) {
908 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700909 Intent targetIntent = info.promisedIntent == null
910 ? info.intent : info.promisedIntent;
911 if (targetIntent != null && info.user.equals(user)) {
912 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700913 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
914 return true;
915 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916 }
917 }
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700921 }
922
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 /**
924 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
925 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700926 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 final ContentResolver cr = context.getContentResolver();
928 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
929 "_id=? and (itemType=? or itemType=?)",
930 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700931 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 try {
934 if (c.moveToFirst()) {
935 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
936 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
937 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
938 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
939 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
940 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700941 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 FolderInfo folderInfo = null;
944 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700945 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
946 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700948 }
949
Sunny Goyala508e4f2015-05-21 09:33:57 -0700950 // Do not trim the folder label, as is was set by the user.
951 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 folderInfo.id = id;
953 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700955 folderInfo.cellX = c.getInt(cellXIndex);
956 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700957 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
959 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 } finally {
962 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700963 }
964
965 return null;
966 }
967
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 /**
969 * Add an item to the database in a specified container. Sets the container, screen, cellX and
970 * cellY fields of the item. Also assigns an ID to the item.
971 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700972 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700973 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 item.cellX = cellX;
976 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 // We store hotseat items in canonical form which is this orientation invariant position
978 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700982 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985
986 final ContentValues values = new ContentValues();
987 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100988 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989
Michael Jurka414300a2013-08-27 15:42:35 +0200990 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700991 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700992
Jason Monk8e19cf22014-03-20 15:06:57 -0400993 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700994 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700995 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700996 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700999 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001000 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 sBgItemsIdMap.put(item.id, item);
1002 switch (item.itemType) {
1003 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1004 sBgFolders.put(item.id, (FolderInfo) item);
1005 // Fall through
1006 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1007 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1008 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1009 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1010 sBgWorkspaceItems.add(item);
1011 } else {
1012 if (!sBgFolders.containsKey(item.container)) {
1013 // Adding an item to a folder that doesn't exist.
1014 String msg = "adding item: " + item + " to a folder that " +
1015 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001016 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001018 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 break;
1020 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1021 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1022 break;
1023 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001024 }
1025 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001026 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001028 }
1029
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001031 * Creates a new unique child id, for a given cell span across all layouts.
1032 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001033 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001034 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001036 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001037 }
1038
Sunny Goyal34942622014-08-29 17:20:55 -07001039 private static ArrayList<ItemInfo> getItemsByPackageName(
1040 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001041 ItemInfoFilter filter = new ItemInfoFilter() {
1042 @Override
1043 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1044 return cn.getPackageName().equals(pn) && info.user.equals(user);
1045 }
1046 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001047 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001048 }
1049
1050 /**
1051 * Removes all the items from the database corresponding to the specified package.
1052 */
1053 static void deletePackageFromDatabase(Context context, final String pn,
1054 final UserHandleCompat user) {
1055 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001056 }
1057
1058 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001059 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001061 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001062 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1063 items.add(item);
1064 deleteItemsFromDatabase(context, items);
1065 }
1066
1067 /**
1068 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001069 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001070 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001072 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001073 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001074 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001075 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001076 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001077
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001078 // Lock on mBgLock *after* the db operation
1079 synchronized (sBgLock) {
1080 switch (item.itemType) {
1081 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1082 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001083 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001084 if (info.container == item.id) {
1085 // We are deleting a folder which still contains items that
1086 // think they are contained by that folder.
1087 String msg = "deleting a folder (" + item + ") which still " +
1088 "contains items (" + info + ")";
1089 Log.e(TAG, msg);
1090 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001091 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001092 sBgWorkspaceItems.remove(item);
1093 break;
1094 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1095 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1096 sBgWorkspaceItems.remove(item);
1097 break;
1098 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1099 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1100 break;
1101 }
1102 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001103 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001104 }
1105 }
Michael Jurka83df1882011-08-31 20:59:26 -07001106 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 }
1109
1110 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001111 * Update the order of the workspace screens in the database. The array list contains
1112 * a list of screen ids in the order that they should appear.
1113 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001114 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001115 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001116 final ContentResolver cr = context.getContentResolver();
1117 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1118
1119 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001120 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001121 while (iter.hasNext()) {
1122 long id = iter.next();
1123 if (id < 0) {
1124 iter.remove();
1125 }
1126 }
1127
1128 Runnable r = new Runnable() {
1129 @Override
1130 public void run() {
Yura085c8532014-02-11 15:15:29 +00001131 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001133 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001134 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 for (int i = 0; i < count; i++) {
1136 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001137 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1139 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001140 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001141 }
Yura085c8532014-02-11 15:15:29 +00001142
1143 try {
1144 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1145 } catch (Exception ex) {
1146 throw new RuntimeException(ex);
1147 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001148
Winson Chungba9c37f2013-08-30 14:11:37 -07001149 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001150 sBgWorkspaceScreens.clear();
1151 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001152 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 }
1154 };
1155 runOnWorkerThread(r);
1156 }
1157
1158 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001159 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001160 */
Winsonc0b52fe2015-09-09 16:38:15 -07001161 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 final ContentResolver cr = context.getContentResolver();
1163
Michael Jurkac9d95c52011-08-29 14:03:34 -07001164 Runnable r = new Runnable() {
1165 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001166 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001167 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001168 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001169 sBgItemsIdMap.remove(info.id);
1170 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001171 sBgWorkspaceItems.remove(info);
1172 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001173
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001174 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001175 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001177 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 for (ItemInfo childInfo : info.contents) {
1179 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001181 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001182 }
1183 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 }
1186
1187 /**
1188 * Set this as the current Launcher activity object for the loader.
1189 */
1190 public void initialize(Callbacks callbacks) {
1191 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001192 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1193 // workspace to prevent leaking Launcher activities on orientation change.
1194 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 mCallbacks = new WeakReference<Callbacks>(callbacks);
1196 }
1197 }
1198
Kenny Guyed131872014-04-30 03:02:21 +01001199 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001200 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001201 int op = PackageUpdatedTask.OP_UPDATE;
1202 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1203 user));
1204 }
1205
1206 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001207 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001208 int op = PackageUpdatedTask.OP_REMOVE;
1209 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1210 user));
1211 }
1212
1213 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001214 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001215 int op = PackageUpdatedTask.OP_ADD;
1216 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1217 user));
1218 }
1219
1220 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001221 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001222 boolean replacing) {
1223 if (!replacing) {
1224 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1225 user));
1226 if (mAppsCanBeOnRemoveableStorage) {
1227 // Only rebind if we support removable storage. It catches the
1228 // case where
1229 // apps on the external sd card need to be reloaded
1230 startLoaderFromBackground();
1231 }
1232 } else {
1233 // If we are replacing then just update the packages in the list
1234 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1235 packageNames, user));
1236 }
1237 }
1238
1239 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001240 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001241 boolean replacing) {
1242 if (!replacing) {
1243 enqueuePackageUpdated(new PackageUpdatedTask(
1244 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1245 user));
1246 }
Kenny Guyed131872014-04-30 03:02:21 +01001247 }
1248
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001249 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1251 * ACTION_PACKAGE_CHANGED.
1252 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001253 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001254 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001255 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001256
Joe Onorato36115782010-06-17 13:28:48 -04001257 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001258 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001259 // If we have changed locale we need to clear out the labels in all apps/workspace.
1260 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001261 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001262 Callbacks callbacks = getCallback();
1263 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001264 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001265 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001266 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1267 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001268 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001269 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001270 }
1271 }
1272
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001273 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001274 resetLoadedState(true, true);
1275
Reena Lee93f824a2011-09-23 17:20:28 -07001276 // Do this here because if the launcher activity is running it will be restarted.
1277 // If it's not running startLoaderFromBackground will merely tell it that it needs
1278 // to reload.
1279 startLoaderFromBackground();
1280 }
1281
Winson Chungf0c6ae02012-03-21 16:10:31 -07001282 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1283 synchronized (mLock) {
1284 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1285 // mWorkspaceLoaded to true later
1286 stopLoaderLocked();
1287 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1288 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1289 }
1290 }
1291
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001292 /**
1293 * When the launcher is in the background, it's possible for it to miss paired
1294 * configuration changes. So whenever we trigger the loader from the background
1295 * tell the launcher that it needs to re-run the loader when it comes back instead
1296 * of doing it now.
1297 */
1298 public void startLoaderFromBackground() {
1299 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001300 Callbacks callbacks = getCallback();
1301 if (callbacks != null) {
1302 // Only actually run the loader if they're not paused.
1303 if (!callbacks.setLoadOnResume()) {
1304 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001305 }
1306 }
1307 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001308 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001309 }
Joe Onorato36115782010-06-17 13:28:48 -04001310 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001311
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001312 /**
1313 * If there is already a loader task running, tell it to stop.
1314 */
1315 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001316 LoaderTask oldTask = mLoaderTask;
1317 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001318 oldTask.stopLocked();
1319 }
Reena Lee93f824a2011-09-23 17:20:28 -07001320 }
1321
Adam Cohen1a85c582014-09-30 09:48:49 -07001322 public boolean isCurrentCallbacks(Callbacks callbacks) {
1323 return (mCallbacks != null && mCallbacks.get() == callbacks);
1324 }
1325
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001326 public void startLoader(int synchronousBindPage) {
1327 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001328 }
1329
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001330 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001331 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1332 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001333 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001334 // Don't bother to start the thread if we know it's not going to do anything
1335 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001336 final Callbacks oldCallbacks = mCallbacks.get();
1337 // Clear any pending bind-runnables from the synchronized load process.
1338 runOnMainThread(new Runnable() {
1339 public void run() {
1340 oldCallbacks.clearPendingBinds();
1341 }
1342 });
1343
Joe Onorato36115782010-06-17 13:28:48 -04001344 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001345 stopLoaderLocked();
1346 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001347 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001348 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001349 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1350 } else {
1351 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1352 sWorker.post(mLoaderTask);
1353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 }
1356 }
1357
Joe Onorato36115782010-06-17 13:28:48 -04001358 public void stopLoader() {
1359 synchronized (mLock) {
1360 if (mLoaderTask != null) {
1361 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 }
1363 }
Joe Onorato36115782010-06-17 13:28:48 -04001364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001366 /**
1367 * Loads the workspace screen ids in an ordered list.
1368 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001369 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001370 final ContentResolver contentResolver = context.getContentResolver();
1371 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001372
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001373 // Get screens ordered by rank.
1374 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1375 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1376 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001377 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001378 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001379 while (sc.moveToNext()) {
1380 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001381 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001382 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001383 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1384 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001385 }
1386 }
1387 } finally {
1388 sc.close();
1389 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001390 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001391 }
1392
Michael Jurkac57b7a82011-08-09 22:02:20 -07001393 public boolean isAllAppsLoaded() {
1394 return mAllAppsLoaded;
1395 }
1396
Joe Onorato36115782010-06-17 13:28:48 -04001397 /**
1398 * Runnable for the thread that loads the contents of the launcher:
1399 * - workspace icons
1400 * - widgets
1401 * - all apps icons
1402 */
1403 private class LoaderTask implements Runnable {
1404 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001405 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001406 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001407 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001408 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001410 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001411 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001412 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001413 }
1414
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001415 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001416 mIsLoadingAndBindingWorkspace = true;
1417
Joe Onorato36115782010-06-17 13:28:48 -04001418 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001419 if (DEBUG_LOADERS) {
1420 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001422
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001423 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001424 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001425 synchronized (LoaderTask.this) {
1426 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001427 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001428 }
1429 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 }
1431 }
1432
Joe Onorato36115782010-06-17 13:28:48 -04001433 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001434 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001435 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001436
Joe Onorato36115782010-06-17 13:28:48 -04001437 private void waitForIdle() {
1438 // Wait until the either we're stopped or the other threads are done.
1439 // This way we don't start loading all apps until the workspace has settled
1440 // down.
1441 synchronized (LoaderTask.this) {
1442 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001443
Joe Onorato36115782010-06-17 13:28:48 -04001444 mHandler.postIdle(new Runnable() {
1445 public void run() {
1446 synchronized (LoaderTask.this) {
1447 mLoadAndBindStepFinished = true;
1448 if (DEBUG_LOADERS) {
1449 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001450 }
Joe Onorato36115782010-06-17 13:28:48 -04001451 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001452 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001453 }
Joe Onorato36115782010-06-17 13:28:48 -04001454 });
1455
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001456 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001457 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001458 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1459 // wait no longer than 1sec at a time
1460 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001461 } catch (InterruptedException ex) {
1462 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001463 }
1464 }
Joe Onorato36115782010-06-17 13:28:48 -04001465 if (DEBUG_LOADERS) {
1466 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001467 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001468 + "ms for previous step to finish binding");
1469 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001472
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001473 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001474 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 // Ensure that we have a valid page index to load synchronously
1476 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1477 "valid page index");
1478 }
1479 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1480 // Ensure that we don't try and bind a specified page when the pages have not been
1481 // loaded already (we should load everything asynchronously in that case)
1482 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1483 }
1484 synchronized (mLock) {
1485 if (mIsLoaderTaskRunning) {
1486 // Ensure that we are never running the background loading at this point since
1487 // we also touch the background collections
1488 throw new RuntimeException("Error! Background loading is already running");
1489 }
1490 }
1491
1492 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1493 // data structures, we can't allow any other thread to touch that data, but because
1494 // this call is synchronous, we can get away with not locking).
1495
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001496 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001497 // operations from the previous activity. We need to ensure that all queued operations
1498 // are executed before any synchronous binding work is done.
1499 mHandler.flush();
1500
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001501 // Divide the set of loaded items into those that we are binding synchronously, and
1502 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001503 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001504 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1505 // arise from that.
1506 onlyBindAllApps();
1507 }
1508
Joe Onorato36115782010-06-17 13:28:48 -04001509 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001511 if (mStopped) {
1512 return;
1513 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001514 mIsLoaderTaskRunning = true;
1515 }
Joe Onorato36115782010-06-17 13:28:48 -04001516 // Optimize for end-user experience: if the Launcher is up and // running with the
1517 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1518 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001519 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001520 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001521 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001522
Joe Onorato36115782010-06-17 13:28:48 -04001523 if (mStopped) {
1524 break keep_running;
1525 }
1526
Joe Onorato36115782010-06-17 13:28:48 -04001527 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001528
1529 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001530 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1531 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533
Joe Onorato36115782010-06-17 13:28:48 -04001534 // Clear out this reference, otherwise we end up holding it until all of the
1535 // callback runnables are done.
1536 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537
Joe Onorato36115782010-06-17 13:28:48 -04001538 synchronized (mLock) {
1539 // If we are still the last one to be scheduled, remove ourselves.
1540 if (mLoaderTask == this) {
1541 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001542 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001543 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001544 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001545 }
Joe Onorato36115782010-06-17 13:28:48 -04001546 }
1547
1548 public void stopLocked() {
1549 synchronized (LoaderTask.this) {
1550 mStopped = true;
1551 this.notify();
1552 }
1553 }
1554
1555 /**
1556 * Gets the callbacks object. If we've been stopped, or if the launcher object
1557 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1558 * object that was around when the deferred message was scheduled, and if there's
1559 * a new Callbacks object around then also return null. This will save us from
1560 * calling onto it with data that will be ignored.
1561 */
1562 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1563 synchronized (mLock) {
1564 if (mStopped) {
1565 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001566 }
Joe Onorato36115782010-06-17 13:28:48 -04001567
1568 if (mCallbacks == null) {
1569 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001570 }
Joe Onorato36115782010-06-17 13:28:48 -04001571
1572 final Callbacks callbacks = mCallbacks.get();
1573 if (callbacks != oldCallbacks) {
1574 return null;
1575 }
1576 if (callbacks == null) {
1577 Log.w(TAG, "no mCallbacks");
1578 return null;
1579 }
1580
1581 return callbacks;
1582 }
1583 }
1584
1585 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001586 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1587 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001588 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001589 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001590 final int countX = profile.numColumns;
1591 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001592
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001594 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001595 // Return early if we detect that an item is under the hotseat button
1596 if (mCallbacks == null ||
1597 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001598 Log.e(TAG, "Error loading shortcut into hotseat " + item
1599 + " into position (" + item.screenId + ":" + item.cellX + ","
1600 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001601 return false;
1602 }
1603
Dan Sandler295ae182013-12-10 16:05:47 -05001604 final ItemInfo[][] hotseatItems =
1605 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1606
Adam Cohen2e6da152015-05-06 11:42:25 -07001607 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001608 Log.e(TAG, "Error loading shortcut " + item
1609 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001610 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001611 + ")");
1612 return false;
1613 }
1614
Dan Sandler295ae182013-12-10 16:05:47 -05001615 if (hotseatItems != null) {
1616 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001617 Log.e(TAG, "Error loading shortcut into hotseat " + item
1618 + " into position (" + item.screenId + ":" + item.cellX + ","
1619 + item.cellY + ") occupied by "
1620 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1621 [(int) item.screenId][0]);
1622 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001623 } else {
1624 hotseatItems[(int) item.screenId][0] = item;
1625 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001626 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001627 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001628 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 items[(int) item.screenId][0] = item;
1630 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001631 return true;
1632 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001633 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1634 if (!workspaceScreens.contains((Long) item.screenId)) {
1635 // The item has an invalid screen id.
1636 return false;
1637 }
1638 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001639 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001640 return true;
1641 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001642
Adam Cohendcd297f2013-06-18 13:13:40 -07001643 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001644 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 occupied.put(item.screenId, items);
1646 }
1647
Dan Sandler295ae182013-12-10 16:05:47 -05001648 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001649 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1650 item.cellX < 0 || item.cellY < 0 ||
1651 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1652 Log.e(TAG, "Error loading shortcut " + item
1653 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1654 + item.cellX + "," + item.cellY
1655 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1656 return false;
1657 }
1658
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001659 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001660 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1661 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001662 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001663 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001664 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001665 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001666 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001667 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001668 return false;
1669 }
1670 }
1671 }
1672 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1673 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001674 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001675 }
1676 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001677
Joe Onorato36115782010-06-17 13:28:48 -04001678 return true;
1679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680
Winson Chungba9c37f2013-08-30 14:11:37 -07001681 /** Clears all the sBg data structures */
1682 private void clearSBgDataStructures() {
1683 synchronized (sBgLock) {
1684 sBgWorkspaceItems.clear();
1685 sBgAppWidgets.clear();
1686 sBgFolders.clear();
1687 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001688 sBgWorkspaceScreens.clear();
1689 }
1690 }
1691
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001692 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001693 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694
Joe Onorato36115782010-06-17 13:28:48 -04001695 final Context context = mContext;
1696 final ContentResolver contentResolver = context.getContentResolver();
1697 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001698 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001699 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001700 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001701
Winson Chung892c74d2013-08-22 16:15:50 -07001702 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001703 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001704 int countX = profile.numColumns;
1705 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001706
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001707 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001708 long migrationStartTime = System.currentTimeMillis();
1709 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001710 try {
1711 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1712 // Clear the flags before starting the task, so that we do not run the task
1713 // again, in case there was an uncaught error.
1714 MigrateFromRestoreTask.clearFlags(mContext);
1715 task.execute();
1716 } catch (Exception e) {
1717 Log.e(TAG, "Error during grid migration", e);
1718
1719 // Clear workspace.
1720 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1721 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001722 Log.v(TAG, "Workspace migration completed in "
1723 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001724 }
1725
Dan Sandlerd5024042014-01-09 15:01:33 -05001726 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1727 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1728 LauncherAppState.getLauncherProvider().deleteDatabase();
1729 }
1730
1731 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1732 // append the user's Launcher2 shortcuts
1733 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1734 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1735 } else {
1736 // Make sure the default workspace is loaded
1737 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001738 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001739 }
Adam Cohene25af792013-06-06 23:08:25 -07001740
Winson Chung2abf94d2012-07-18 18:16:38 -07001741 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001742 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001743 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001744 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001745 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001746
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001747 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001748 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001749 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001750 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001751 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 // +1 for the hotseat (it can be larger than the workspace)
1754 // Load workspace in reverse order to ensure that latest items are loaded first (and
1755 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001756 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 try {
1759 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1760 final int intentIndex = c.getColumnIndexOrThrow
1761 (LauncherSettings.Favorites.INTENT);
1762 final int titleIndex = c.getColumnIndexOrThrow
1763 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 final int containerIndex = c.getColumnIndexOrThrow(
1765 LauncherSettings.Favorites.CONTAINER);
1766 final int itemTypeIndex = c.getColumnIndexOrThrow(
1767 LauncherSettings.Favorites.ITEM_TYPE);
1768 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1769 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001770 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1771 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772 final int screenIndex = c.getColumnIndexOrThrow(
1773 LauncherSettings.Favorites.SCREEN);
1774 final int cellXIndex = c.getColumnIndexOrThrow
1775 (LauncherSettings.Favorites.CELLX);
1776 final int cellYIndex = c.getColumnIndexOrThrow
1777 (LauncherSettings.Favorites.CELLY);
1778 final int spanXIndex = c.getColumnIndexOrThrow
1779 (LauncherSettings.Favorites.SPANX);
1780 final int spanYIndex = c.getColumnIndexOrThrow(
1781 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001782 final int rankIndex = c.getColumnIndexOrThrow(
1783 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001784 final int restoredIndex = c.getColumnIndexOrThrow(
1785 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001786 final int profileIdIndex = c.getColumnIndexOrThrow(
1787 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001788 final int optionsIndex = c.getColumnIndexOrThrow(
1789 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001790 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791
Sunny Goyal7f834d22015-04-21 10:10:23 -07001792 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1793 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1794 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1795 }
1796
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 ShortcutInfo info;
1798 String intentDescription;
1799 LauncherAppWidgetInfo appWidgetInfo;
1800 int container;
1801 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001802 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001803 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001804 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 while (!mStopped && c.moveToNext()) {
1807 try {
1808 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001809 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001810 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001811 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 switch (itemType) {
1814 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1815 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001816 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001818 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001819 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001820 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001821 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001822 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001823 if (user == null) {
1824 // User has been deleted remove the item.
1825 itemsToRemove.add(id);
1826 continue;
1827 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 try {
1829 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001830 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001831 if (cn != null && cn.getPackageName() != null) {
1832 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1833 cn.getPackageName(), user);
1834 boolean validComponent = validPkg &&
1835 launcherApps.isActivityEnabledForProfile(cn, user);
1836
1837 if (validComponent) {
1838 if (restored) {
1839 // no special handling necessary for this item
1840 restoredRows.add(id);
1841 restored = false;
1842 }
1843 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001844 intent = null;
1845 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1846 // We allow auto install apps to have their intent
1847 // updated after an install.
1848 intent = manager.getLaunchIntentForPackage(
1849 cn.getPackageName());
1850 if (intent != null) {
1851 ContentValues values = new ContentValues();
1852 values.put(LauncherSettings.Favorites.INTENT,
1853 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001854 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001855 }
1856 }
1857
1858 if (intent == null) {
1859 // The app is installed but the component is no
1860 // longer available.
1861 Launcher.addDumpLog(TAG,
1862 "Invalid component removed: " + cn, true);
1863 itemsToRemove.add(id);
1864 continue;
1865 } else {
1866 // no special handling necessary for this item
1867 restoredRows.add(id);
1868 restored = false;
1869 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001870 } else if (restored) {
1871 // Package is not yet available but might be
1872 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001873 Launcher.addDumpLog(TAG,
1874 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001875
1876 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1877 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001878 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001879 // App restore has started. Update the flag
1880 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1881 ContentValues values = new ContentValues();
1882 values.put(LauncherSettings.Favorites.RESTORED,
1883 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001884 updateItem(id, values);
1885 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1886 // This is a common app. Try to replace this.
1887 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1888 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1889 if (parser.findDefaultApp()) {
1890 // Default app found. Replace it.
1891 intent = parser.parsedIntent;
1892 cn = intent.getComponent();
1893 ContentValues values = parser.parsedValues;
1894 values.put(LauncherSettings.Favorites.RESTORED, 0);
1895 updateItem(id, values);
1896 restored = false;
1897 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001898
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001899 } else if (REMOVE_UNRESTORED_ICONS) {
1900 Launcher.addDumpLog(TAG,
1901 "Unrestored package removed: " + cn, true);
1902 itemsToRemove.add(id);
1903 continue;
1904 }
Sunny Goyal94485362014-09-18 16:13:58 -07001905 } else if (REMOVE_UNRESTORED_ICONS) {
1906 Launcher.addDumpLog(TAG,
1907 "Unrestored package removed: " + cn, true);
1908 itemsToRemove.add(id);
1909 continue;
1910 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001911 } else if (launcherApps.isAppEnabled(
1912 manager, cn.getPackageName(),
1913 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1914 // Package is present but not available.
1915 allowMissingTarget = true;
1916 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1917 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001918 // SdCard is not ready yet. Package might get available,
1919 // once it is ready.
1920 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1921 + " (check again later)", true);
1922 HashSet<String> pkgs = sPendingPackages.get(user);
1923 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001924 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001925 sPendingPackages.put(user, pkgs);
1926 }
1927 pkgs.add(cn.getPackageName());
1928 allowMissingTarget = true;
1929 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001930
1931 } else {
1932 // Do not wait for external media load anymore.
1933 // Log the invalid package, and remove it
1934 Launcher.addDumpLog(TAG,
1935 "Invalid package removed: " + cn, true);
1936 itemsToRemove.add(id);
1937 continue;
Winson Chungee055712013-07-30 14:46:24 -07001938 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001939 } else if (cn == null) {
1940 // For shortcuts with no component, keep them as they are
1941 restoredRows.add(id);
1942 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001943 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001945 Launcher.addDumpLog(TAG,
1946 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001947 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 continue;
1949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950
Sunny Goyal34b65272015-03-11 16:56:52 -07001951 boolean useLowResIcon = container >= 0 &&
1952 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1953
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001954 if (itemReplaced) {
1955 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001956 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001957 cursorIconInfo.iconIndex, titleIndex,
1958 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001959 } else {
1960 // Don't replace items for other profiles.
1961 itemsToRemove.add(id);
1962 continue;
1963 }
1964 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001965 if (user.equals(UserHandleCompat.myUserHandle())) {
1966 Launcher.addDumpLog(TAG,
1967 "constructing info for partially restored package",
1968 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001969 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001970 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001971 intent = getRestoredItemIntent(c, context, intent);
1972 } else {
1973 // Don't restore items for other profiles.
1974 itemsToRemove.add(id);
1975 continue;
1976 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001977 } else if (itemType ==
1978 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001979 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001980 cursorIconInfo.iconIndex, titleIndex,
1981 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001983 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001984
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 // App shortcuts that used to be automatically added to Launcher
1986 // didn't always have the correct intent flags set, so do that
1987 // here
1988 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001989 intent.getCategories() != null &&
1990 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001991 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 intent.addFlags(
1993 Intent.FLAG_ACTIVITY_NEW_TASK |
1994 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1995 }
Michael Jurka96879562012-03-22 05:54:33 -07001996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001999 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 info.cellX = c.getInt(cellXIndex);
2004 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002005 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002006 info.spanX = 1;
2007 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002008 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002009 if (info.promisedIntent != null) {
2010 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2011 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002012 info.isDisabled = disabledState;
2013 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2014 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2015 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002018 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002019 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 break;
2021 }
2022
Sunny Goyal756adbc2015-04-16 15:20:51 -07002023 if (restored) {
2024 ComponentName cn = info.getTargetComponent();
2025 if (cn != null) {
2026 Integer progress = installingPkgs.get(cn.getPackageName());
2027 if (progress != null) {
2028 info.setInstallProgress(progress);
2029 } else {
2030 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2031 }
2032 }
2033 }
2034
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 switch (container) {
2036 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2037 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2038 sBgWorkspaceItems.add(info);
2039 break;
2040 default:
2041 // Item is in a user folder
2042 FolderInfo folderInfo =
2043 findOrMakeFolder(sBgFolders, container);
2044 folderInfo.add(info);
2045 break;
2046 }
2047 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002048 } else {
2049 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 }
2051 break;
2052
2053 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2054 id = c.getLong(idIndex);
2055 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2056
Sunny Goyala508e4f2015-05-21 09:33:57 -07002057 // Do not trim the folder label, as is was set by the user.
2058 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 folderInfo.cellX = c.getInt(cellXIndex);
2063 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002064 folderInfo.spanX = 1;
2065 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002066 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067
Daniel Sandler8802e962010-05-26 16:28:16 -04002068 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002069 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002070 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002071 break;
2072 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002073
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2076 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2077 sBgWorkspaceItems.add(folderInfo);
2078 break;
Joe Onorato36115782010-06-17 13:28:48 -04002079 }
Joe Onorato17a89222011-02-08 17:26:11 -08002080
Chris Wrenf4d08112014-01-16 18:13:56 -05002081 if (restored) {
2082 // no special handling required for restored folders
2083 restoredRows.add(id);
2084 }
2085
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2087 sBgFolders.put(folderInfo.id, folderInfo);
2088 break;
2089
2090 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002091 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002093 boolean customWidget = itemType ==
2094 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2095
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002097 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002098 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002099 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002100 user = allUsers.get(serialNumber);
2101 if (user == null) {
2102 itemsToRemove.add(id);
2103 continue;
2104 }
2105
Sunny Goyalff572272014-07-23 13:58:07 -07002106 final ComponentName component =
2107 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002108
Sunny Goyal651077b2014-06-30 14:15:31 -07002109 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002110 final boolean isIdValid = (restoreStatus &
2111 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002112 final boolean wasProviderReady = (restoreStatus &
2113 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002114
Adam Cohen59400422014-03-05 18:07:04 -08002115 final LauncherAppWidgetProviderInfo provider =
2116 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002117 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002118 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002119
2120 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002121 if (!isSafeMode && !customWidget &&
2122 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002123 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002124 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002125
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002127 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 itemsToRemove.add(id);
2129 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002130 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002131 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2132 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002133
Sunny Goyal53f96722015-07-13 19:54:53 -07002134 // The provider is available. So the widget is either
2135 // available or not available. We do not need to track
2136 // any future restore updates.
2137 int status = restoreStatus &
2138 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002139 if (!wasProviderReady) {
2140 // If provider was not previously ready, update the
2141 // status and UI flag.
2142
2143 // Id would be valid only if the widget restore broadcast was received.
2144 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002145 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002146 } else {
2147 status &= ~LauncherAppWidgetInfo
2148 .FLAG_PROVIDER_NOT_READY;
2149 }
2150 }
2151 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002152 } else {
2153 Log.v(TAG, "Widget restore pending id=" + id
2154 + " appWidgetId=" + appWidgetId
2155 + " status =" + restoreStatus);
2156 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002157 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002158 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002159 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002160
2161 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2162 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002163 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002164 // App restore has started. Update the flag
2165 appWidgetInfo.restoreStatus |=
2166 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002167 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002168 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002169 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002170 itemsToRemove.add(id);
2171 continue;
2172 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002173
2174 appWidgetInfo.installProgress =
2175 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 }
Sunny Goyalff572272014-07-23 13:58:07 -07002177
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 appWidgetInfo.cellX = c.getInt(cellXIndex);
2181 appWidgetInfo.cellY = c.getInt(cellYIndex);
2182 appWidgetInfo.spanX = c.getInt(spanXIndex);
2183 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002184 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002185
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2187 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2188 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002189 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2190 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 continue;
2192 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002194 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002196 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002197 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 break;
2199 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002200
Adam Cohen59400422014-03-05 18:07:04 -08002201 if (!customWidget) {
2202 String providerName =
2203 appWidgetInfo.providerName.flattenToString();
2204 if (!providerName.equals(savedProvider) ||
2205 (appWidgetInfo.restoreStatus != restoreStatus)) {
2206 ContentValues values = new ContentValues();
2207 values.put(
2208 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2209 providerName);
2210 values.put(LauncherSettings.Favorites.RESTORED,
2211 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002212 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002213 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002214 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2216 sBgAppWidgets.add(appWidgetInfo);
2217 }
Joe Onorato36115782010-06-17 13:28:48 -04002218 break;
2219 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002221 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002222 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 }
2224 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002225 if (c != null) {
2226 c.close();
2227 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 }
2229
Winson Chungba9c37f2013-08-30 14:11:37 -07002230 // Break early if we've stopped loading
2231 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002232 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002233 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002234 }
2235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002238 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2239 Utilities.createDbSelectionQuery(
2240 LauncherSettings.Favorites._ID, itemsToRemove), null);
2241 if (DEBUG_LOADERS) {
2242 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2243 LauncherSettings.Favorites._ID, itemsToRemove));
2244 }
2245
2246 // Remove any empty folder
2247 for (long folderId : LauncherAppState.getLauncherProvider()
2248 .deleteEmptyFolders()) {
2249 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2250 sBgFolders.remove(folderId);
2251 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002252 }
2253 }
2254
Sunny Goyal317698b2015-07-29 11:45:41 -07002255 // Sort all the folder items and make sure the first 3 items are high resolution.
2256 for (FolderInfo folder : sBgFolders) {
2257 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2258 int pos = 0;
2259 for (ShortcutInfo info : folder.contents) {
2260 if (info.usingLowResIcon) {
2261 info.updateIcon(mIconCache, false);
2262 }
2263 pos ++;
2264 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2265 break;
2266 }
2267 }
2268 }
2269
Chris Wrenf4d08112014-01-16 18:13:56 -05002270 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002271 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002272 ContentValues values = new ContentValues();
2273 values.put(LauncherSettings.Favorites.RESTORED, 0);
2274 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2275 Utilities.createDbSelectionQuery(
2276 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002277 }
2278
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002279 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2280 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002281 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002282 null, sWorker);
2283 }
2284
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002285 // Remove any empty screens
2286 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002287 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002288 long screenId = item.screenId;
2289 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2290 unusedScreens.contains(screenId)) {
2291 unusedScreens.remove(screenId);
2292 }
2293 }
2294
2295 // If there are any empty screens remove them, and update.
2296 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002297 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 }
2300
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 if (DEBUG_LOADERS) {
2302 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2303 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002305 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002307
Sunny Goyale2df0622015-04-24 11:27:00 -07002308 for (int i = 0; i < nScreens; i++) {
2309 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002310 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 line += " | ";
2312 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002313 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002314 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002315 if (x < screen.length && y < screen[x].length) {
2316 line += (screen[x][y] != null) ? "#" : ".";
2317 } else {
2318 line += "!";
2319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 }
Joe Onorato36115782010-06-17 13:28:48 -04002321 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 }
Joe Onorato36115782010-06-17 13:28:48 -04002325 }
Adam Cohene25af792013-06-06 23:08:25 -07002326 }
2327
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002328 /**
2329 * Partially updates the item without any notification. Must be called on the worker thread.
2330 */
2331 private void updateItem(long itemId, ContentValues update) {
2332 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002333 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002334 update,
2335 BaseColumns._ID + "= ?",
2336 new String[]{Long.toString(itemId)});
2337 }
2338
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 /** Filters the set of items who are directly or indirectly (via another container) on the
2340 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002341 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002342 ArrayList<ItemInfo> allWorkspaceItems,
2343 ArrayList<ItemInfo> currentScreenItems,
2344 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002345 // Purge any null ItemInfos
2346 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2347 while (iter.hasNext()) {
2348 ItemInfo i = iter.next();
2349 if (i == null) {
2350 iter.remove();
2351 }
2352 }
2353
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 // Order the set of items by their containers first, this allows use to walk through the
2355 // list sequentially, build up a list of containers that are in the specified screen,
2356 // as well as all items in those containers.
2357 Set<Long> itemsOnScreen = new HashSet<Long>();
2358 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2359 @Override
2360 public int compare(ItemInfo lhs, ItemInfo rhs) {
2361 return (int) (lhs.container - rhs.container);
2362 }
2363 });
2364 for (ItemInfo info : allWorkspaceItems) {
2365 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002366 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 currentScreenItems.add(info);
2368 itemsOnScreen.add(info.id);
2369 } else {
2370 otherScreenItems.add(info);
2371 }
2372 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2373 currentScreenItems.add(info);
2374 itemsOnScreen.add(info.id);
2375 } else {
2376 if (itemsOnScreen.contains(info.container)) {
2377 currentScreenItems.add(info);
2378 itemsOnScreen.add(info.id);
2379 } else {
2380 otherScreenItems.add(info);
2381 }
2382 }
2383 }
2384 }
2385
2386 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002387 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 ArrayList<LauncherAppWidgetInfo> appWidgets,
2389 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2390 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391
2392 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002393 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002395 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 currentScreenWidgets.add(widget);
2397 } else {
2398 otherScreenWidgets.add(widget);
2399 }
2400 }
2401 }
2402
2403 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002405 LongArrayMap<ItemInfo> itemsIdMap,
2406 LongArrayMap<FolderInfo> folders,
2407 LongArrayMap<FolderInfo> currentScreenFolders,
2408 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409
Sunny Goyale2df0622015-04-24 11:27:00 -07002410 int total = folders.size();
2411 for (int i = 0; i < total; i++) {
2412 long id = folders.keyAt(i);
2413 FolderInfo folder = folders.valueAt(i);
2414
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002416 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002418 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 currentScreenFolders.put(id, folder);
2420 } else {
2421 otherScreenFolders.put(id, folder);
2422 }
2423 }
2424 }
2425
2426 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2427 * right) */
2428 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002429 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002430 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002431 final int screenCols = profile.numColumns;
2432 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002434 @Override
2435 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002436 if (lhs.container == rhs.container) {
2437 // Within containers, order by their spatial position in that container
2438 switch ((int) lhs.container) {
2439 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2440 long lr = (lhs.screenId * screenCellCount +
2441 lhs.cellY * screenCols + lhs.cellX);
2442 long rr = (rhs.screenId * screenCellCount +
2443 rhs.cellY * screenCols + rhs.cellX);
2444 return (int) (lr - rr);
2445 }
2446 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2447 // We currently use the screen id as the rank
2448 return (int) (lhs.screenId - rhs.screenId);
2449 }
2450 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002451 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002452 throw new RuntimeException("Unexpected container type when " +
2453 "sorting workspace items.");
2454 }
2455 return 0;
2456 }
2457 } else {
2458 // Between containers, order by hotseat, desktop
2459 return (int) (lhs.container - rhs.container);
2460 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002461 }
2462 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002464
Adam Cohendcd297f2013-06-18 13:13:40 -07002465 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2466 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002467 final Runnable r = new Runnable() {
2468 @Override
2469 public void run() {
2470 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2471 if (callbacks != null) {
2472 callbacks.bindScreens(orderedScreens);
2473 }
2474 }
2475 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002476 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 }
2478
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2480 final ArrayList<ItemInfo> workspaceItems,
2481 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002482 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002483 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484
2485 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002486 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002488 final int start = i;
2489 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490 final Runnable r = new Runnable() {
2491 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002492 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002493 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002494 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002495 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2496 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 }
2498 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002500 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002501 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502
2503 // Bind the folders
2504 if (!folders.isEmpty()) {
2505 final Runnable r = new Runnable() {
2506 public void run() {
2507 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2508 if (callbacks != null) {
2509 callbacks.bindFolders(folders);
2510 }
2511 }
2512 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002513 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 }
2515
2516 // Bind the widgets, one at a time
2517 N = appWidgets.size();
2518 for (int i = 0; i < N; i++) {
2519 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2520 final Runnable r = new Runnable() {
2521 public void run() {
2522 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2523 if (callbacks != null) {
2524 callbacks.bindAppWidget(widget);
2525 }
2526 }
2527 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002528 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529 }
2530 }
2531
2532 /**
2533 * Binds all loaded data to actual views on the main thread.
2534 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002535 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 final long t = SystemClock.uptimeMillis();
2537 Runnable r;
2538
2539 // Don't use these two variables in any of the callback runnables.
2540 // Otherwise we hold a reference to them.
2541 final Callbacks oldCallbacks = mCallbacks.get();
2542 if (oldCallbacks == null) {
2543 // This launcher has exited and nobody bothered to tell us. Just bail.
2544 Log.w(TAG, "LoaderTask running with no launcher");
2545 return;
2546 }
2547
Winson Chung9b9fb962013-11-15 15:39:34 -08002548 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2550 ArrayList<LauncherAppWidgetInfo> appWidgets =
2551 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002552 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002553
2554 final LongArrayMap<FolderInfo> folders;
2555 final LongArrayMap<ItemInfo> itemsIdMap;
2556
Winson Chung2abf94d2012-07-18 18:16:38 -07002557 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 workspaceItems.addAll(sBgWorkspaceItems);
2559 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002560 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002561
2562 folders = sBgFolders.clone();
2563 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 }
2565
Derek Prothro7aff3992013-12-10 14:00:37 -05002566 final boolean isLoadingSynchronously =
2567 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002568 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002569 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002570 if (currScreen >= orderedScreenIds.size()) {
2571 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002572 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002573 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002574 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002575 final long currentScreenId = currentScreen < 0
2576 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002577
2578 // Load all the items that are on the current page first (and in the process, unbind
2579 // all the existing workspace items before we call startBinding() below.
2580 unbindWorkspaceItemsOnMainThread();
2581
2582 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2584 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2585 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2586 new ArrayList<LauncherAppWidgetInfo>();
2587 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2588 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002589 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2590 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591
Winson Chung9b9fb962013-11-15 15:39:34 -08002592 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002594 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002596 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002597 otherFolders);
2598 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2599 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2600
2601 // Tell the workspace that we're about to start binding items
2602 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002603 public void run() {
2604 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2605 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002606 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002607 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002608 }
2609 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002611 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002612
Adam Cohendcd297f2013-06-18 13:13:40 -07002613 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2614
Sunny Goyal527c7d32015-08-28 15:19:36 -07002615 Executor mainExecutor = new DeferredMainThreadExecutor();
2616 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002617 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002618 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002619
Sunny Goyal527c7d32015-08-28 15:19:36 -07002620 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2621 // remaining screens after first onDraw is called. This ensures that the first screen
2622 // is immediately visible (eg. during rotation)
2623 // In case of !isLoadingSynchronously, bind all pages one after other.
2624 final Executor deferredExecutor = isLoadingSynchronously ?
2625 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2626
2627 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2628 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629
2630 // Tell the workspace that we're done binding items
2631 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002632 public void run() {
2633 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2634 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002635 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002636 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637
Sunny Goyal639e9062015-08-19 19:17:06 -07002638 mIsLoadingAndBindingWorkspace = false;
2639
2640 // Run all the bind complete runnables after workspace is bound.
2641 if (!mBindCompleteRunnables.isEmpty()) {
2642 synchronized (mBindCompleteRunnables) {
2643 for (final Runnable r : mBindCompleteRunnables) {
2644 runOnWorkerThread(r);
2645 }
2646 mBindCompleteRunnables.clear();
2647 }
2648 }
2649
Winson Chung98e030b2012-05-07 16:01:11 -07002650 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002651 if (DEBUG_LOADERS) {
2652 Log.d(TAG, "bound workspace in "
2653 + (SystemClock.uptimeMillis()-t) + "ms");
2654 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002655
Joe Onorato36115782010-06-17 13:28:48 -04002656 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002657 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002658 deferredExecutor.execute(r);
2659
Winson Chung4a2afa32012-07-19 14:53:05 -07002660 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002661 r = new Runnable() {
2662 public void run() {
2663 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2664 if (callbacks != null) {
2665 // We are loading synchronously, which means, some of the pages will be
2666 // bound after first draw. Inform the callbacks that page binding is
2667 // not complete, and schedule the remaining pages.
2668 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2669 callbacks.onPageBoundSynchronously(currentScreen);
2670 }
2671 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2672 }
2673 }
2674 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002675 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002676 }
Joe Onorato36115782010-06-17 13:28:48 -04002677 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002678
Joe Onorato36115782010-06-17 13:28:48 -04002679 private void loadAndBindAllApps() {
2680 if (DEBUG_LOADERS) {
2681 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2682 }
2683 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002684 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002685 synchronized (LoaderTask.this) {
2686 if (mStopped) {
2687 return;
2688 }
2689 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002690 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002691 synchronized (LoaderTask.this) {
2692 if (mStopped) {
2693 return;
2694 }
2695 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002696 }
Joe Onorato36115782010-06-17 13:28:48 -04002697 } else {
2698 onlyBindAllApps();
2699 }
2700 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002701
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002702 private void updateIconCache() {
2703 // Ignore packages which have a promise icon.
2704 HashSet<String> packagesToIgnore = new HashSet<>();
2705 synchronized (sBgLock) {
2706 for (ItemInfo info : sBgItemsIdMap) {
2707 if (info instanceof ShortcutInfo) {
2708 ShortcutInfo si = (ShortcutInfo) info;
2709 if (si.isPromise() && si.getTargetComponent() != null) {
2710 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2711 }
2712 } else if (info instanceof LauncherAppWidgetInfo) {
2713 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2714 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2715 packagesToIgnore.add(lawi.providerName.getPackageName());
2716 }
2717 }
2718 }
2719 }
2720 mIconCache.updateDbIcons(packagesToIgnore);
2721 }
2722
Joe Onorato36115782010-06-17 13:28:48 -04002723 private void onlyBindAllApps() {
2724 final Callbacks oldCallbacks = mCallbacks.get();
2725 if (oldCallbacks == null) {
2726 // This launcher has exited and nobody bothered to tell us. Just bail.
2727 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2728 return;
2729 }
2730
2731 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002732 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002733 final ArrayList<AppInfo> list
2734 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002735 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002736 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002737 public void run() {
2738 final long t = SystemClock.uptimeMillis();
2739 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2740 if (callbacks != null) {
2741 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002742 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002743 }
2744 if (DEBUG_LOADERS) {
2745 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2746 + (SystemClock.uptimeMillis()-t) + "ms");
2747 }
2748 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002749 };
2750 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002751 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002752 r.run();
2753 } else {
2754 mHandler.post(r);
2755 }
Joe Onorato36115782010-06-17 13:28:48 -04002756 }
2757
Winson Chung64359a52013-07-08 17:17:08 -07002758 private void loadAllApps() {
2759 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002760
Joe Onorato36115782010-06-17 13:28:48 -04002761 final Callbacks oldCallbacks = mCallbacks.get();
2762 if (oldCallbacks == null) {
2763 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002764 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002765 return;
2766 }
2767
Kenny Guyed131872014-04-30 03:02:21 +01002768 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2769
Winson Chung64359a52013-07-08 17:17:08 -07002770 // Clear the list of apps
2771 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002772 for (UserHandleCompat user : profiles) {
2773 // Query for the set of apps
2774 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002775 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002776 if (DEBUG_LOADERS) {
2777 Log.d(TAG, "getActivityList took "
2778 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2779 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2780 }
2781 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002782 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002783 if (apps == null || apps.isEmpty()) {
2784 return;
2785 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002786
Kenny Guyed131872014-04-30 03:02:21 +01002787 // Create the ApplicationInfos
2788 for (int i = 0; i < apps.size(); i++) {
2789 LauncherActivityInfoCompat app = apps.get(i);
2790 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002791 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002792 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002793
Sunny Goyal756a28a2015-04-23 17:07:55 -07002794 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2795 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002796 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002797
2798 @Override
2799 public void run() {
2800 heuristic.processUserApps(apps);
2801 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002802 };
2803 runOnMainThread(new Runnable() {
2804
2805 @Override
2806 public void run() {
2807 // Check isLoadingWorkspace on the UI thread, as it is updated on
2808 // the UI thread.
2809 if (mIsLoadingAndBindingWorkspace) {
2810 synchronized (mBindCompleteRunnables) {
2811 mBindCompleteRunnables.add(r);
2812 }
2813 } else {
2814 runOnWorkerThread(r);
2815 }
2816 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002817 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002818 }
Winson Chung64359a52013-07-08 17:17:08 -07002819 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002820 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002821 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2822 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002823
2824 // Post callback on main thread
2825 mHandler.post(new Runnable() {
2826 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002827
Winson Chung64359a52013-07-08 17:17:08 -07002828 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002829 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002830 if (callbacks != null) {
2831 callbacks.bindAllApplications(added);
2832 if (DEBUG_LOADERS) {
2833 Log.d(TAG, "bound " + added.size() + " apps in "
2834 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2835 }
2836 } else {
2837 Log.i(TAG, "not binding apps: no Launcher activity");
2838 }
2839 }
2840 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002841 // Cleanup any data stored for a deleted user.
2842 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002843
Sunny Goyal2d648b02015-08-11 13:56:28 -07002844 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002845 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002846 Log.d(TAG, "Icons processed in "
2847 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002848 }
2849 }
2850
2851 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002852 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002853 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002854 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2855 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2856 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2857 }
Joe Onorato36115782010-06-17 13:28:48 -04002858 }
2859 }
2860
Sunny Goyal75b0f552015-05-20 21:57:06 -07002861 /**
2862 * Called when the icons for packages have been updated in the icon cache.
2863 */
2864 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2865 final Callbacks callbacks = getCallback();
2866 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2867 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2868
2869 // If any package icon has changed (app was updated while launcher was dead),
2870 // update the corresponding shortcuts.
2871 synchronized (sBgLock) {
2872 for (ItemInfo info : sBgItemsIdMap) {
2873 if (info instanceof ShortcutInfo && user.equals(info.user)
2874 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2875 ShortcutInfo si = (ShortcutInfo) info;
2876 ComponentName cn = si.getTargetComponent();
2877 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2878 si.updateIcon(mIconCache);
2879 updatedShortcuts.add(si);
2880 }
2881 }
2882 }
2883 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2884 }
2885
2886 if (!updatedShortcuts.isEmpty()) {
2887 final UserHandleCompat userFinal = user;
2888 mHandler.post(new Runnable() {
2889
2890 public void run() {
2891 Callbacks cb = getCallback();
2892 if (cb != null && callbacks == cb) {
2893 cb.bindShortcutsChanged(updatedShortcuts,
2894 new ArrayList<ShortcutInfo>(), userFinal);
2895 }
2896 }
2897 });
2898 }
2899
2900 if (!updatedApps.isEmpty()) {
2901 mHandler.post(new Runnable() {
2902
2903 public void run() {
2904 Callbacks cb = getCallback();
2905 if (cb != null && callbacks == cb) {
2906 cb.bindAppsUpdated(updatedApps);
2907 }
2908 }
2909 });
2910 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002911
2912 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002913 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002914 }
2915
Joe Onorato36115782010-06-17 13:28:48 -04002916 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002917 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002918 }
2919
Adam Cohen091440a2015-03-18 14:16:05 -07002920 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002921
2922 @Override
2923 public void onReceive(Context context, Intent intent) {
2924 synchronized (sBgLock) {
2925 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2926 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002927 final PackageManager manager = context.getPackageManager();
2928 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2929 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002930 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2931 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002932 packagesRemoved.clear();
2933 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002934 for (String pkg : entry.getValue()) {
2935 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002936 boolean packageOnSdcard = launcherApps.isAppEnabled(
2937 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2938 if (packageOnSdcard) {
2939 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2940 packagesUnavailable.add(pkg);
2941 } else {
2942 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2943 packagesRemoved.add(pkg);
2944 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002945 }
2946 }
2947 if (!packagesRemoved.isEmpty()) {
2948 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2949 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2950 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002951 if (!packagesUnavailable.isEmpty()) {
2952 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2953 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2954 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002955 }
Sunny Goyal34942622014-08-29 17:20:55 -07002956 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002957 }
2958 }
2959 }
2960
Joe Onorato36115782010-06-17 13:28:48 -04002961 private class PackageUpdatedTask implements Runnable {
2962 int mOp;
2963 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002964 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002965
2966 public static final int OP_NONE = 0;
2967 public static final int OP_ADD = 1;
2968 public static final int OP_UPDATE = 2;
2969 public static final int OP_REMOVE = 3; // uninstlled
2970 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2971
2972
Kenny Guyed131872014-04-30 03:02:21 +01002973 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002974 mOp = op;
2975 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002976 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002977 }
2978
2979 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002980 if (!mHasLoaderCompletedOnce) {
2981 // Loader has not yet run.
2982 return;
2983 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002984 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002985
2986 final String[] packages = mPackages;
2987 final int N = packages.length;
2988 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002989 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002990 for (int i=0; i<N; i++) {
2991 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002992 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002993 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002994 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002995
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002996 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2997 if (heuristic != null) {
2998 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002999 }
Joe Onorato36115782010-06-17 13:28:48 -04003000 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003001 }
Joe Onorato36115782010-06-17 13:28:48 -04003002 case OP_UPDATE:
3003 for (int i=0; i<N; i++) {
3004 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003005 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003006 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003007 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003008 }
3009 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003010 case OP_REMOVE: {
3011 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3012 if (heuristic != null) {
3013 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003014 }
Joe Onorato36115782010-06-17 13:28:48 -04003015 for (int i=0; i<N; i++) {
3016 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003017 mIconCache.removeIconsForPkg(packages[i], mUser);
3018 }
3019 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003020 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003021 case OP_UNAVAILABLE:
3022 for (int i=0; i<N; i++) {
3023 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3024 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003025 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
3027 break;
3028 }
3029
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003030 ArrayList<AppInfo> added = null;
3031 ArrayList<AppInfo> modified = null;
3032 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003033
Adam Cohen487f7dd2012-06-28 18:12:10 -07003034 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003035 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003036 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003038 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003039 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003040 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003041 }
Winson Chung5d55f332012-07-16 20:45:03 -07003042 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003043 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003044 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003045 }
3046
Sunny Goyale0f58d72014-11-10 18:05:31 -08003047 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003048 if (callbacks == null) {
3049 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3050 return;
3051 }
3052
Sunny Goyal4390ace2014-10-13 11:33:11 -07003053 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3054 new HashMap<ComponentName, AppInfo>();
3055
Joe Onorato36115782010-06-17 13:28:48 -04003056 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003057 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003058 for (AppInfo ai : added) {
3059 addedOrUpdatedApps.put(ai.componentName, ai);
3060 }
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003062
Joe Onorato36115782010-06-17 13:28:48 -04003063 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003065 for (AppInfo ai : modified) {
3066 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003067 }
3068
Joe Onorato36115782010-06-17 13:28:48 -04003069 mHandler.post(new Runnable() {
3070 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003071 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003072 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003073 callbacks.bindAppsUpdated(modifiedFinal);
3074 }
3075 }
3076 });
3077 }
Winson Chung83892cc2013-05-01 16:53:33 -07003078
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003081 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3082 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3083 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3084
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003085 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003087 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003088 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3089 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 boolean infoUpdated = false;
3091 boolean shortcutUpdated = false;
3092
3093 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003094 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003096 Bitmap icon = Utilities.createIconBitmap(
3097 si.iconResource.packageName,
3098 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 if (icon != null) {
3100 si.setIcon(icon);
3101 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003103 }
3104 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105
3106 ComponentName cn = si.getTargetComponent();
3107 if (cn != null && packageSet.contains(cn.getPackageName())) {
3108 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3109
3110 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3112 // Auto install icon
3113 PackageManager pm = context.getPackageManager();
3114 ResolveInfo matched = pm.resolveActivity(
3115 new Intent(Intent.ACTION_MAIN)
3116 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3117 PackageManager.MATCH_DEFAULT_ONLY);
3118 if (matched == null) {
3119 // Try to find the best match activity.
3120 Intent intent = pm.getLaunchIntentForPackage(
3121 cn.getPackageName());
3122 if (intent != null) {
3123 cn = intent.getComponent();
3124 appInfo = addedOrUpdatedApps.get(cn);
3125 }
3126
3127 if ((intent == null) || (appInfo == null)) {
3128 removedShortcuts.add(si);
3129 continue;
3130 }
3131 si.promisedIntent = intent;
3132 }
3133 }
3134
3135 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003136 if (appInfo != null) {
3137 si.flags = appInfo.flags;
3138 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139
Sunny Goyal756adbc2015-04-16 15:20:51 -07003140 si.intent = si.promisedIntent;
3141 si.promisedIntent = null;
3142 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003143 infoUpdated = true;
3144 si.updateIcon(mIconCache);
3145 }
3146
3147 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3148 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3149 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003150 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 si.contentDescription = appInfo.contentDescription;
3152 infoUpdated = true;
3153 }
3154
3155 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3156 // Since package was just updated, the target must be available now.
3157 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3158 shortcutUpdated = true;
3159 }
3160 }
3161
3162 if (infoUpdated || shortcutUpdated) {
3163 updatedShortcuts.add(si);
3164 }
3165 if (infoUpdated) {
3166 updateItemInDatabase(context, si);
3167 }
3168 } else if (info instanceof LauncherAppWidgetInfo) {
3169 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3170 if (mUser.equals(widgetInfo.user)
3171 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3172 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003173 widgetInfo.restoreStatus &=
3174 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3175 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003176
3177 // adding this flag ensures that launcher shows 'click to setup'
3178 // if the widget has a config activity. In case there is no config
3179 // activity, it will be marked as 'restored' during bind.
3180 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3181
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 widgets.add(widgetInfo);
3183 updateItemInDatabase(context, widgetInfo);
3184 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003185 }
3186 }
3187 }
3188
Sunny Goyal4390ace2014-10-13 11:33:11 -07003189 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3190 mHandler.post(new Runnable() {
3191
3192 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003193 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003194 if (callbacks == cb && cb != null) {
3195 callbacks.bindShortcutsChanged(
3196 updatedShortcuts, removedShortcuts, mUser);
3197 }
3198 }
3199 });
3200 if (!removedShortcuts.isEmpty()) {
3201 deleteItemsFromDatabase(context, removedShortcuts);
3202 }
3203 }
3204 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 mHandler.post(new Runnable() {
3206 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003207 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003208 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003210 }
3211 }
3212 });
3213 }
3214 }
3215
Winson Chungdf95eb12013-10-16 14:57:07 -07003216 final ArrayList<String> removedPackageNames =
3217 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003218 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 // Mark all packages in the broadcast to be removed
3220 removedPackageNames.addAll(Arrays.asList(packages));
3221 } else if (mOp == OP_UPDATE) {
3222 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003223 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003224 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003225 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003226 }
3227 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003228 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003229
Winson Chungdf95eb12013-10-16 14:57:07 -07003230 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003231 final int removeReason;
3232 if (mOp == OP_UNAVAILABLE) {
3233 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3234 } else {
3235 // Remove all the components associated with this package
3236 for (String pn : removedPackageNames) {
3237 deletePackageFromDatabase(context, pn, mUser);
3238 }
3239 // Remove all the specific components
3240 for (AppInfo a : removedApps) {
3241 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3242 deleteItemsFromDatabase(context, infos);
3243 }
3244 removeReason = 0;
3245 }
3246
Winson Chungdf95eb12013-10-16 14:57:07 -07003247 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003248 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003249 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003250 mHandler.post(new Runnable() {
3251 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003252 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003253 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003254 callbacks.bindComponentsRemoved(
3255 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003256 }
3257 }
3258 });
Joe Onoratobe386092009-11-17 17:32:16 -08003259 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003260
Sunny Goyal2d648b02015-08-11 13:56:28 -07003261 // Update widgets
3262 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3263 // Always refresh for a package event on secondary user
3264 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3265
3266 // Refresh widget list, if the package already had a widget.
3267 synchronized (sBgLock) {
3268 if (sBgWidgetProviders != null) {
3269 HashSet<String> pkgSet = new HashSet<>();
3270 Collections.addAll(pkgSet, mPackages);
3271
3272 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3273 needToRefresh |= key.user.equals(mUser) &&
3274 pkgSet.contains(key.componentName.getPackageName());
3275 }
3276 }
3277 }
3278
3279 if (!needToRefresh && mOp != OP_REMOVE) {
3280 // Refresh widget list, if there is any newly added widget
3281 PackageManager pm = context.getPackageManager();
3282 for (String pkg : mPackages) {
3283 needToRefresh |= !pm.queryBroadcastReceivers(
3284 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
3285 .setPackage(pkg), 0).isEmpty();
3286 }
3287 }
3288
3289 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3290 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003291
Adam Cohen4caf2982013-08-20 18:54:31 -07003292 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003293 mHandler.post(new Runnable() {
3294 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003295 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003296 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003297 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003298 }
3299 }
3300 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 }
3302 }
3303
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003304 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3305 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003306 ArrayList<LauncherAppWidgetProviderInfo> results =
3307 new ArrayList<LauncherAppWidgetProviderInfo>();
3308 try {
3309 synchronized (sBgLock) {
3310 if (sBgWidgetProviders == null || refresh) {
3311 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3312 = new HashMap<>();
3313 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3314 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003315
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003316 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3317 for (AppWidgetProviderInfo pInfo : widgets) {
3318 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3319 UserHandleCompat user = wm.getUser(info);
3320 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3321 }
Robin Lee26ace122015-03-16 19:41:43 +00003322
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003323 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3324 for (CustomAppWidget widget : customWidgets) {
3325 info = new LauncherAppWidgetProviderInfo(context, widget);
3326 UserHandleCompat user = wm.getUser(info);
3327 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3328 }
3329 // Replace the global list at the very end, so that if there is an exception,
3330 // previously loaded provider list is used.
3331 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003332 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003333 results.addAll(sBgWidgetProviders.values());
3334 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003335 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003336 } catch (Exception e) {
3337 if (e.getCause() instanceof TransactionTooLargeException) {
3338 // the returned value may be incomplete and will not be refreshed until the next
3339 // time Launcher starts.
3340 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3341 // onResume is called to refresh the widget provider list.
3342 synchronized (sBgLock) {
3343 if (sBgWidgetProviders != null) {
3344 results.addAll(sBgWidgetProviders.values());
3345 }
3346 return results;
3347 }
3348 } else {
3349 throw e;
3350 }
Adam Cohen59400422014-03-05 18:07:04 -08003351 }
3352 }
3353
Robin Lee26ace122015-03-16 19:41:43 +00003354 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3355 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003356 synchronized (sBgLock) {
3357 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003358 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003359 }
Robin Lee26ace122015-03-16 19:41:43 +00003360 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003361 }
3362 }
3363
Sunny Goyal2d648b02015-08-11 13:56:28 -07003364 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003365
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003366 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003367 @Override
3368 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003369 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003370 final WidgetsModel model = mBgWidgetsModel.clone();
3371
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003372 mHandler.post(new Runnable() {
3373 @Override
3374 public void run() {
3375 Callbacks cb = getCallback();
3376 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003377 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003378 }
3379 }
3380 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003381 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003382 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3383 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003384 }
3385 });
3386 }
3387
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003388 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003389 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003390 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003391 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003392 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003393 @Thunk void updateWidgetsModel(boolean refresh) {
3394 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003395 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003396 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003397 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3398 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003399 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003400 }
3401
Adam Cohen091440a2015-03-18 14:16:05 -07003402 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003403 UserHandleCompat user) {
3404 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3405 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003406 }
Adam Cohen556f6132014-01-15 15:18:08 -08003407
Kenny Guyed131872014-04-30 03:02:21 +01003408 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3409 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003410 if (cn == null) {
3411 return false;
3412 }
Kenny Guyed131872014-04-30 03:02:21 +01003413 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3414 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003415 return false;
3416 }
Kenny Guyed131872014-04-30 03:02:21 +01003417 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003418 }
3419
Adam Cohena28b78e2014-05-20 17:03:04 -07003420 public static boolean isValidPackage(Context context, String packageName,
3421 UserHandleCompat user) {
3422 if (packageName == null) {
3423 return false;
3424 }
3425 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3426 return launcherApps.isPackageEnabledForProfile(packageName, user);
3427 }
3428
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003430 * Make an ShortcutInfo object for a restored application or shortcut item that points
3431 * to a package that is not yet installed on the system.
3432 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003433 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003434 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003435 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003436 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003437
3438 Bitmap icon = iconInfo.loadIcon(c, info, context);
3439 // the fallback icon
3440 if (icon == null) {
3441 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3442 } else {
3443 info.setIcon(icon);
3444 }
Sunny Goyal34942622014-08-29 17:20:55 -07003445
3446 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003447 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003448 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003449 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003450 }
Sunny Goyal34942622014-08-29 17:20:55 -07003451 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3452 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003453 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003454 }
Sunny Goyal34942622014-08-29 17:20:55 -07003455 } else {
3456 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3457 }
3458
Winson Chung82b016c2015-05-08 17:00:10 -07003459 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003460 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003461 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003462 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003463 return info;
3464 }
3465
3466 /**
3467 * Make an Intent object for a restored application or shortcut item that points
3468 * to the market page for the item.
3469 */
Adam Cohen091440a2015-03-18 14:16:05 -07003470 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003471 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003472 return getMarketIntent(componentName.getPackageName());
3473 }
3474
3475 static Intent getMarketIntent(String packageName) {
3476 return new Intent(Intent.ACTION_VIEW)
3477 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003478 .scheme("market")
3479 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003480 .appendQueryParameter("id", packageName)
3481 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003482 }
3483
3484 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003485 * Make an ShortcutInfo object for a shortcut that is an application.
3486 *
3487 * If c is not null, then it will be used to fill in missing data like the title and icon.
3488 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003489 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003490 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003491 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003492 if (user == null) {
3493 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003494 return null;
3495 }
3496
Kenny Guyed131872014-04-30 03:02:21 +01003497 ComponentName componentName = intent.getComponent();
3498 if (componentName == null) {
3499 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3500 return null;
3501 }
3502
3503 Intent newIntent = new Intent(intent.getAction(), null);
3504 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3505 newIntent.setComponent(componentName);
3506 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003507 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003508 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3509 return null;
3510 }
3511
3512 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003513 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003514 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3515 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3516 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003517 }
3518
Joe Onorato56d82912010-03-07 14:32:10 -05003519 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003520 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003521 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003522 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003523
Joe Onorato56d82912010-03-07 14:32:10 -05003524 // fall back to the class name of the activity
3525 if (info.title == null) {
3526 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003527 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003528
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003530 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003531 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003532 if (lai != null) {
3533 info.flags = AppInfo.initFlags(lai);
3534 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003536 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003537
Sunny Goyale2df0622015-04-24 11:27:00 -07003538 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003539 ItemInfoFilter f) {
3540 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3541 for (ItemInfo i : infos) {
3542 if (i instanceof ShortcutInfo) {
3543 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003544 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003545 if (cn != null && f.filterItem(null, info, cn)) {
3546 filtered.add(info);
3547 }
3548 } else if (i instanceof FolderInfo) {
3549 FolderInfo info = (FolderInfo) i;
3550 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003551 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003552 if (cn != null && f.filterItem(info, s, cn)) {
3553 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003554 }
3555 }
Winson Chung64359a52013-07-08 17:17:08 -07003556 } else if (i instanceof LauncherAppWidgetInfo) {
3557 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3558 ComponentName cn = info.providerName;
3559 if (cn != null && f.filterItem(null, info, cn)) {
3560 filtered.add(info);
3561 }
Winson Chung8a435102012-08-30 17:16:53 -07003562 }
3563 }
Winson Chung64359a52013-07-08 17:17:08 -07003564 return new ArrayList<ItemInfo>(filtered);
3565 }
3566
Adam Cohen091440a2015-03-18 14:16:05 -07003567 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003568 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003569 ItemInfoFilter filter = new ItemInfoFilter() {
3570 @Override
3571 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003572 if (info.user == null) {
3573 return cn.equals(cname);
3574 } else {
3575 return cn.equals(cname) && info.user.equals(user);
3576 }
Winson Chung64359a52013-07-08 17:17:08 -07003577 }
3578 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003579 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003580 }
3581
Sunny Goyal1a745e82014-10-02 15:58:31 -07003582 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003583 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 */
Adam Cohen091440a2015-03-18 14:16:05 -07003585 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003586 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003587 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003588 // Non-app shortcuts are only supported for current user.
3589 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003591
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003592 // TODO: If there's an explicit component and we can't install that, delete it.
3593
Winson Chung82b016c2015-05-08 17:00:10 -07003594 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003595
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003596 Bitmap icon = iconInfo.loadIcon(c, info, context);
3597 // the fallback icon
3598 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003599 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003600 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003601 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003602 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 return info;
3604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605
Sunny Goyal2350bc92014-10-14 16:42:54 -07003606 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3608 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3609 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3610
Adam Cohend9198822011-11-22 16:42:47 -08003611 if (intent == null) {
3612 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3613 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3614 return null;
3615 }
3616
Joe Onorato0589f0f2010-02-08 13:44:00 -08003617 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003618 boolean customIcon = false;
3619 ShortcutIconResource iconResource = null;
3620
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003621 if (bitmap instanceof Bitmap) {
3622 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003623 customIcon = true;
3624 } else {
3625 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003626 if (extra instanceof ShortcutIconResource) {
3627 iconResource = (ShortcutIconResource) extra;
3628 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003629 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003630 }
3631 }
3632
Michael Jurkac9d95c52011-08-29 14:03:34 -07003633 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003634
Kenny Guyed131872014-04-30 03:02:21 +01003635 // Only support intents for current user for now. Intents sent from other
3636 // users wouldn't get here without intent forwarding anyway.
3637 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003638 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003639 icon = mIconCache.getDefaultIcon(info.user);
3640 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003641 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003642 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003643
Winson Chung82b016c2015-05-08 17:00:10 -07003644 info.title = Utilities.trim(name);
3645 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003646 info.intent = intent;
3647 info.customIcon = customIcon;
3648 info.iconResource = iconResource;
3649
3650 return info;
3651 }
3652
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003654 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 * or make a new one.
3656 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003657 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 // See if a placeholder was created for us already
3659 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003660 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003662 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 folders.put(id, folderInfo);
3664 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003665 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 }
3667
Joe Onoratobe386092009-11-17 17:32:16 -08003668
Sunny Goyal651077b2014-06-30 14:15:31 -07003669 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3670 return (provider != null) && (provider.provider != null)
3671 && (provider.provider.getPackageName() != null);
3672 }
3673
Joe Onoratobe386092009-11-17 17:32:16 -08003674 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003675 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003676 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3677 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3678 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3679 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003680 if (mLoaderTask != null) {
3681 mLoaderTask.dumpState();
3682 } else {
3683 Log.d(TAG, "mLoaderTask=null");
3684 }
Joe Onoratobe386092009-11-17 17:32:16 -08003685 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003686
3687 public Callbacks getCallback() {
3688 return mCallbacks != null ? mCallbacks.get() : null;
3689 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003690
3691 /**
3692 * @return {@link FolderInfo} if its already loaded.
3693 */
3694 public FolderInfo findFolderById(Long folderId) {
3695 synchronized (sBgLock) {
3696 return sBgFolders.get(folderId);
3697 }
3698 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003699
Sunny Goyal527c7d32015-08-28 15:19:36 -07003700 @Thunk class DeferredMainThreadExecutor implements Executor {
3701
3702 @Override
3703 public void execute(Runnable command) {
3704 runOnMainThread(command);
3705 }
3706 }
3707
Sunny Goyal756adbc2015-04-16 15:20:51 -07003708 /**
3709 * @return the looper for the worker thread which can be used to start background tasks.
3710 */
3711 public static Looper getWorkerLooper() {
3712 return sWorkerThread.getLooper();
3713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714}