blob: 0ef15eef201d09df0cf33092f4f75543166953e8 [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;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import 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;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070039import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080040import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040041import android.os.Handler;
42import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070043import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040048import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080049import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080051import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070052import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010053
Sunny Goyalffe83f12014-08-14 17:39:34 -070054import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010055import com.android.launcher3.compat.LauncherActivityInfoCompat;
56import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070057import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070058import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010059import com.android.launcher3.compat.UserHandleCompat;
60import com.android.launcher3.compat.UserManagerCompat;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -070062import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080065
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.lang.ref.WeakReference;
67import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070068import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.text.Collator;
70import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700151 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700163 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 // sBgWorkspaceScreens is the ordered set of workspace screens.
166 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
167
Adam Cohen59400422014-03-05 18:07:04 -0800168 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000169 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Adam Cohen091440a2015-03-18 14:16:05 -0700177 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Reena Lee99a73f32011-10-24 17:27:37 -0700179 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
187 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700188 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
189 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700190 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700192 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800193 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700196 public void bindAppsAdded(ArrayList<Long> newScreens,
197 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700198 ArrayList<ItemInfo> addAnimated,
199 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
202 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
203 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700204 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700205 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700206 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song8821ca92015-05-04 16:28:20 -0700207 public void bindAllPackages(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100208 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700209 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700210 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700211 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chung64359a52013-07-08 17:17:08 -0700214 public interface ItemInfoFilter {
215 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
216 }
217
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800219 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400220
Winson Chungee055712013-07-30 14:46:24 -0700221 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700222 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400223 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
224 // resource string.
225 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
226 ProviderInfo providerInfo =
227 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
228 ProviderInfo redirectProvider =
229 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700230
231 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400232 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 if (mOldContentProviderExists) {
235 Log.d(TAG, "Old launcher provider exists.");
236 } else {
237 Log.d(TAG, "Old launcher provider does not exist.");
238 }
239
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100241 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400244 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700245 Configuration config = res.getConfiguration();
246 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100247 mLauncherApps = LauncherAppsCompat.getInstance(context);
248 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 }
250
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
252 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700254 if (sWorkerThread.getThreadId() == Process.myTid()) {
255 // If we are on the worker thread, post onto the main handler
256 mHandler.post(r);
257 } else {
258 r.run();
259 }
260 }
261
262 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
263 * posted on the worker thread handler. */
264 private static void runOnWorkerThread(Runnable r) {
265 if (sWorkerThread.getThreadId() == Process.myTid()) {
266 r.run();
267 } else {
268 // If we are not on the worker thread, then post to the worker handler
269 sWorker.post(r);
270 }
271 }
272
Sunny Goyal756a28a2015-04-23 17:07:55 -0700273 /**
274 * Runs the specified runnable after the loader is complete
275 */
276 private void runAfterBindCompletes(Runnable r) {
277 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
278 synchronized (mBindCompleteRunnables) {
279 mBindCompleteRunnables.add(r);
280 }
281 } else {
282 runOnWorkerThread(r);
283 }
284 }
285
Winson Chunge43a1e72014-01-15 10:33:02 -0800286 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
287 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800288 }
289
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 public void setPackageState(final PackageInstallInfo installInfo) {
291 Runnable updateRunnable = new Runnable() {
292
293 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700295 synchronized (sBgLock) {
296 final HashSet<ItemInfo> updates = new HashSet<>();
297
298 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
299 // Ignore install success events as they are handled by Package add events.
300 return;
301 }
302
Sunny Goyale2df0622015-04-24 11:27:00 -0700303 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700304 if (info instanceof ShortcutInfo) {
305 ShortcutInfo si = (ShortcutInfo) info;
306 ComponentName cn = si.getTargetComponent();
307 if (si.isPromise() && (cn != null)
308 && installInfo.packageName.equals(cn.getPackageName())) {
309 si.setInstallProgress(installInfo.progress);
310
311 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
312 // Mark this info as broken.
313 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
314 }
315 updates.add(si);
316 }
317 }
318 }
319
320 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
321 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
322 widget.installProgress = installInfo.progress;
323 updates.add(widget);
324 }
325 }
326
327 if (!updates.isEmpty()) {
328 // Push changes to the callback.
329 Runnable r = new Runnable() {
330 public void run() {
331 Callbacks callbacks = getCallback();
332 if (callbacks != null) {
333 callbacks.bindRestoreItemsChange(updates);
334 }
335 }
336 };
337 mHandler.post(r);
338 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500339 }
340 }
341 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500343 }
344
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 /**
346 * Updates the icons and label of all pending icons for the provided package name.
347 */
348 public void updateSessionDisplayInfo(final String packageName) {
349 Runnable updateRunnable = new Runnable() {
350
351 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700352 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700353 synchronized (sBgLock) {
354 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
355 final UserHandleCompat user = UserHandleCompat.myUserHandle();
356
Sunny Goyale2df0622015-04-24 11:27:00 -0700357 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700358 if (info instanceof ShortcutInfo) {
359 ShortcutInfo si = (ShortcutInfo) info;
360 ComponentName cn = si.getTargetComponent();
361 if (si.isPromise() && (cn != null)
362 && packageName.equals(cn.getPackageName())) {
363 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
364 // For auto install apps update the icon as well as label.
365 mIconCache.getTitleAndIcon(si,
366 si.promisedIntent, user,
367 si.shouldUseLowResIcon());
368 } else {
369 // Only update the icon for restored apps.
370 si.updateIcon(mIconCache);
371 }
372 updates.add(si);
373 }
374 }
375 }
376
377 if (!updates.isEmpty()) {
378 // Push changes to the callback.
379 Runnable r = new Runnable() {
380 public void run() {
381 Callbacks callbacks = getCallback();
382 if (callbacks != null) {
383 callbacks.bindShortcutsChanged(updates,
384 new ArrayList<ShortcutInfo>(), user);
385 }
386 }
387 };
388 mHandler.post(r);
389 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700390 }
391 }
392 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700393 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700394 }
395
Adam Cohen76a47a12014-02-05 11:47:43 -0800396 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398
399 if (allAppsApps == null) {
400 throw new RuntimeException("allAppsApps must not be null");
401 }
402 if (allAppsApps.isEmpty()) {
403 return;
404 }
405
406 // Process the newly added applications and add them to the database first
407 Runnable r = new Runnable() {
408 public void run() {
409 runOnMainThread(new Runnable() {
410 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800411 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800412 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500413 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800414 }
415 }
416 });
417 }
418 };
419 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700420 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800421
Sunny Goyala9116722015-04-29 13:55:58 -0700422 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423 int[] xy, int spanX, int spanY) {
424 LauncherAppState app = LauncherAppState.getInstance();
425 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
426 final int xCount = (int) grid.numColumns;
427 final int yCount = (int) grid.numRows;
428 boolean[][] occupied = new boolean[xCount][yCount];
429 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700430 for (ItemInfo r : occupiedPos) {
431 int right = r.cellX + r.spanX;
432 int bottom = r.cellY + r.spanY;
433 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
434 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 occupied[x][y] = true;
436 }
437 }
438 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800439 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700440 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800441 }
442
443 /**
444 * Find a position on the screen for the given size or adds a new screen.
445 * @return screenId and the coordinates for the item.
446 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700447 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 ArrayList<Long> workspaceScreens,
450 ArrayList<Long> addedWorkspaceScreensFinal,
451 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700452 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453
Sunny Goyala9116722015-04-29 13:55:58 -0700454 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700455 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700456 synchronized (sBgLock) {
457 for (ItemInfo info : sBgItemsIdMap) {
458 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
459 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
460 if (items == null) {
461 items = new ArrayList<>();
462 screenItems.put(info.screenId, items);
463 }
464 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467 }
468
469 // Find appropriate space for the item.
470 long screenId = 0;
471 int[] cordinates = new int[2];
472 boolean found = false;
473
474 int screenCount = workspaceScreens.size();
475 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700476 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800477 if (preferredScreenIndex < screenCount) {
478 screenId = workspaceScreens.get(preferredScreenIndex);
479 found = findNextAvailableIconSpaceInScreen(
480 screenItems.get(screenId), cordinates, spanX, spanY);
481 }
482
483 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700484 // Search on any of the screens starting from the first screen.
485 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 screenId = workspaceScreens.get(screen);
487 if (findNextAvailableIconSpaceInScreen(
488 screenItems.get(screenId), cordinates, spanX, spanY)) {
489 // We found a space for it
490 found = true;
491 break;
492 }
493 }
494 }
495
496 if (!found) {
497 // Still no position found. Add a new screen to the end.
498 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
499
500 // Save the screen id for binding in the workspace
501 workspaceScreens.add(screenId);
502 addedWorkspaceScreensFinal.add(screenId);
503
504 // If we still can't find an empty space, then God help us all!!!
505 if (!findNextAvailableIconSpaceInScreen(
506 screenItems.get(screenId), cordinates, spanX, spanY)) {
507 throw new RuntimeException("Can't find space to add the item");
508 }
509 }
510 return Pair.create(screenId, cordinates);
511 }
512
513 /**
514 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700516 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700517 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800519 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700520 return;
Winson Chung997a9232013-07-24 15:33:46 -0700521 }
Winson Chung64359a52013-07-08 17:17:08 -0700522 // Process the newly added applications and add them to the database first
523 Runnable r = new Runnable() {
524 public void run() {
525 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
526 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
527
Winson Chung76828c82013-08-19 15:43:29 -0700528 // Get the list of workspace screens. We need to append to this list and
529 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
530 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800531 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700532 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700534 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700536 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700538 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 }
Winson Chung76828c82013-08-19 15:43:29 -0700540
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700542 Pair<Long, int[]> coords = findSpaceForItem(context,
543 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800544 1, 1);
545 long screenId = coords.first;
546 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700547
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 ItemInfo itemInfo;
549 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
550 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800551 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700552 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700553 } else {
554 throw new RuntimeException("Unexpected info type");
555 }
Winson Chung94d67682013-09-25 16:29:40 -0700556
Winson Chung64359a52013-07-08 17:17:08 -0700557 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700558 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700559 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700560 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700561 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700562 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700563 }
564 }
565
Winson Chung76828c82013-08-19 15:43:29 -0700566 // Update the workspace screens
567 updateWorkspaceScreenOrder(context, workspaceScreens);
568
Adam Cohen76a47a12014-02-05 11:47:43 -0800569 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700570 runOnMainThread(new Runnable() {
571 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800572 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700573 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700574 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
575 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700576 if (!addedShortcutsFinal.isEmpty()) {
577 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
578 long lastScreenId = info.screenId;
579 for (ItemInfo i : addedShortcutsFinal) {
580 if (i.screenId == lastScreenId) {
581 addAnimated.add(i);
582 } else {
583 addNotAnimated.add(i);
584 }
Winson Chung997a9232013-07-24 15:33:46 -0700585 }
586 }
Winson Chungd64d1762013-08-20 14:37:16 -0700587 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800588 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700589 }
Winson Chung64359a52013-07-08 17:17:08 -0700590 }
Winson Chung997a9232013-07-24 15:33:46 -0700591 });
592 }
Winson Chung64359a52013-07-08 17:17:08 -0700593 }
594 };
595 runOnWorkerThread(r);
596 }
597
Sunny Goyald33860f2015-04-23 16:02:20 -0700598 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700599 if (sWorkerThread.getThreadId() == Process.myTid()) {
600 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
601 "main thread");
602 }
603
604 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400605 synchronized (mDeferredBindRunnables) {
606 mDeferredBindRunnables.clear();
607 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700608
609 // Remove any queued UI runnables
610 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700611 // Unbind all the workspace items
612 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700613 }
614
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700615 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700616 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700617 // Ensure that we don't use the same workspace items data structure on the main thread
618 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700619 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700620 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700621 tmpItems.addAll(sBgWorkspaceItems);
622 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700623 }
624 Runnable r = new Runnable() {
625 @Override
626 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700627 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 item.unbind();
629 }
630 }
631 };
632 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700633 }
634
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 /**
636 * Adds an item to the DB if it was not created previously, or move it to a new
637 * <container, screen, cellX, cellY>
638 */
639 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 if (item.container == ItemInfo.NO_ID) {
642 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700643 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 } else {
645 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
648 }
649
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700650 static void checkItemInfoLocked(
651 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
652 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
653 if (modelItem != null && item != modelItem) {
654 // check all the data is consistent
655 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
656 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
657 ShortcutInfo shortcut = (ShortcutInfo) item;
658 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
659 modelShortcut.intent.filterEquals(shortcut.intent) &&
660 modelShortcut.id == shortcut.id &&
661 modelShortcut.itemType == shortcut.itemType &&
662 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700663 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 modelShortcut.cellX == shortcut.cellX &&
665 modelShortcut.cellY == shortcut.cellY &&
666 modelShortcut.spanX == shortcut.spanX &&
667 modelShortcut.spanY == shortcut.spanY &&
668 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
669 (modelShortcut.dropPos != null &&
670 shortcut.dropPos != null &&
671 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
672 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
673 // For all intents and purposes, this is the same object
674 return;
675 }
676 }
677
678 // the modelItem needs to match up perfectly with item if our model is
679 // to be consistent with the database-- for now, just require
680 // modelItem == item or the equality check above
681 String msg = "item: " + ((item != null) ? item.toString() : "null") +
682 "modelItem: " +
683 ((modelItem != null) ? modelItem.toString() : "null") +
684 "Error: ItemInfo passed to checkItemInfo doesn't match original";
685 RuntimeException e = new RuntimeException(msg);
686 if (stackTrace != null) {
687 e.setStackTrace(stackTrace);
688 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800689 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700690 }
691 }
692
Michael Jurka816474f2012-06-25 14:49:02 -0700693 static void checkItemInfo(final ItemInfo item) {
694 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
695 final long itemId = item.id;
696 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700697 public void run() {
698 synchronized (sBgLock) {
699 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700700 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700701 }
702 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700703 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700704 }
705
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
707 final ItemInfo item, final String callingFunction) {
708 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700709 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700710 final ContentResolver cr = context.getContentResolver();
711
Adam Cohen487f7dd2012-06-28 18:12:10 -0700712 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713 Runnable r = new Runnable() {
714 public void run() {
715 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 updateItemArrays(item, itemId, stackTrace);
717 }
718 };
719 runOnWorkerThread(r);
720 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
723 final ArrayList<ItemInfo> items, final String callingFunction) {
724 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700725
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
727 Runnable r = new Runnable() {
728 public void run() {
729 ArrayList<ContentProviderOperation> ops =
730 new ArrayList<ContentProviderOperation>();
731 int count = items.size();
732 for (int i = 0; i < count; i++) {
733 ItemInfo item = items.get(i);
734 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700735 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700737
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
739 updateItemArrays(item, itemId, stackTrace);
740
741 }
742 try {
743 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
744 } catch (Exception e) {
745 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700746 }
747 }
748 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700749 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700750 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700751
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
753 // Lock on mBgLock *after* the db operation
754 synchronized (sBgLock) {
755 checkItemInfoLocked(itemId, item, stackTrace);
756
757 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
758 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
759 // Item is in a folder, make sure this folder exists
760 if (!sBgFolders.containsKey(item.container)) {
761 // An items container is being set to a that of an item which is not in
762 // the list of Folders.
763 String msg = "item: " + item + " container being set to: " +
764 item.container + ", not in the list of folders";
765 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 }
767 }
768
769 // Items are added/removed from the corresponding FolderInfo elsewhere, such
770 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
771 // that are on the desktop, as appropriate
772 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800773 if (modelItem != null &&
774 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
775 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776 switch (modelItem.itemType) {
777 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
778 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
779 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
780 if (!sBgWorkspaceItems.contains(modelItem)) {
781 sBgWorkspaceItems.add(modelItem);
782 }
783 break;
784 default:
785 break;
786 }
787 } else {
788 sBgWorkspaceItems.remove(modelItem);
789 }
790 }
791 }
792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700795 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700796 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 item.cellX = cellX;
800 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700801
Winson Chung3d503fb2011-07-13 17:25:49 -0700802 // We store hotseat items in canonical form which is this orientation invariant position
803 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700804 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810
811 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700813 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
814 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800815 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700819 }
820
821 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700822 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
823 * cellX, cellY have already been updated on the ItemInfos.
824 */
825 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
826 final long container, final int screen) {
827
828 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
829 int count = items.size();
830
831 for (int i = 0; i < count; i++) {
832 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700833 item.container = container;
834
835 // We store hotseat items in canonical form which is this orientation invariant position
836 // in the hotseat
837 if (context instanceof Launcher && screen < 0 &&
838 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700840 item.cellY);
841 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700843 }
844
845 final ContentValues values = new ContentValues();
846 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
847 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
848 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800849 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700851
852 contentValues.add(values);
853 }
854 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
855 }
856
857 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800859 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700862 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800863 item.cellX = cellX;
864 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700865 item.spanX = spanX;
866 item.spanY = spanY;
867
868 // We store hotseat items in canonical form which is this orientation invariant position
869 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 }
Adam Cohend4844c32011-02-18 19:25:06 -0800876
Adam Cohend4844c32011-02-18 19:25:06 -0800877 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800878 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700879 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
880 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800881 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700882 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
883 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800885
Michael Jurka816474f2012-06-25 14:49:02 -0700886 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700887 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888
889 /**
890 * Update an item to the database in a specified container.
891 */
892 static void updateItemInDatabase(Context context, final ItemInfo item) {
893 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100894 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700895 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800896 }
897
Sunny Goyal756a28a2015-04-23 17:07:55 -0700898 private void assertWorkspaceLoaded() {
899 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
900 throw new RuntimeException("Trying to add shortcut while loader is running");
901 }
902 }
903
Adam Cohend4844c32011-02-18 19:25:06 -0800904 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700905 * Returns true if the shortcuts already exists on the workspace. This must be called after
906 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700908 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
909 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 if (intent.getComponent() != null) {
913 // If component is not null, an intent with null package will produce
914 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700916 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700917 intentWithPkg = intent.toUri(0);
918 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700919 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
921 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700922 }
923 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700924 intentWithPkg = intent.toUri(0);
925 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700926 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700927 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700928
929 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700930 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 if (item instanceof ShortcutInfo) {
932 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700933 if (info.getIntent() != null && info.user.equals(user)) {
934 String s = info.getIntent().toUri(0);
935 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
936 return true;
937 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700938 }
939 }
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700942 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700943 }
944
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 /**
946 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
947 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700948 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 final ContentResolver cr = context.getContentResolver();
950 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
951 "_id=? and (itemType=? or itemType=?)",
952 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700953 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700954
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 try {
956 if (c.moveToFirst()) {
957 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
958 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
959 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
960 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
961 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
962 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700963 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 FolderInfo folderInfo = null;
966 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700967 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
968 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700970 }
971
Winson Chung82b016c2015-05-08 17:00:10 -0700972 folderInfo.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 folderInfo.id = id;
974 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700976 folderInfo.cellX = c.getInt(cellXIndex);
977 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700978 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
980 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 } finally {
983 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700984 }
985
986 return null;
987 }
988
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 /**
990 * Add an item to the database in a specified container. Sets the container, screen, cellX and
991 * cellY fields of the item. Also assigns an ID to the item.
992 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700993 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700994 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 item.cellX = cellX;
997 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 // We store hotseat items in canonical form which is this orientation invariant position
999 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006
1007 final ContentValues values = new ContentValues();
1008 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001009 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
Michael Jurka414300a2013-08-27 15:42:35 +02001011 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001013
Jason Monk8e19cf22014-03-20 15:06:57 -04001014 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001015 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001017 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001020 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001021 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 sBgItemsIdMap.put(item.id, item);
1023 switch (item.itemType) {
1024 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1025 sBgFolders.put(item.id, (FolderInfo) item);
1026 // Fall through
1027 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1028 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1029 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1030 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1031 sBgWorkspaceItems.add(item);
1032 } else {
1033 if (!sBgFolders.containsKey(item.container)) {
1034 // Adding an item to a folder that doesn't exist.
1035 String msg = "adding item: " + item + " to a folder that " +
1036 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001037 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001039 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 break;
1041 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1042 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1043 break;
1044 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001045 }
1046 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001049 }
1050
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001052 * Creates a new unique child id, for a given cell span across all layouts.
1053 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001054 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001056 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001058 }
1059
Sunny Goyal34942622014-08-29 17:20:55 -07001060 private static ArrayList<ItemInfo> getItemsByPackageName(
1061 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001062 ItemInfoFilter filter = new ItemInfoFilter() {
1063 @Override
1064 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1065 return cn.getPackageName().equals(pn) && info.user.equals(user);
1066 }
1067 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001068 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001069 }
1070
1071 /**
1072 * Removes all the items from the database corresponding to the specified package.
1073 */
1074 static void deletePackageFromDatabase(Context context, final String pn,
1075 final UserHandleCompat user) {
1076 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 }
1078
1079 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001080 * Removes the specified item from the database
1081 * @param context
1082 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001084 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001085 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1086 items.add(item);
1087 deleteItemsFromDatabase(context, items);
1088 }
1089
1090 /**
1091 * Removes the specified items from the database
1092 * @param context
1093 * @param item
1094 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001095 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001097 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001098 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001099 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001100 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 // Lock on mBgLock *after* the db operation
1104 synchronized (sBgLock) {
1105 switch (item.itemType) {
1106 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1107 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001108 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001109 if (info.container == item.id) {
1110 // We are deleting a folder which still contains items that
1111 // think they are contained by that folder.
1112 String msg = "deleting a folder (" + item + ") which still " +
1113 "contains items (" + info + ")";
1114 Log.e(TAG, msg);
1115 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001117 sBgWorkspaceItems.remove(item);
1118 break;
1119 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1120 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1121 sBgWorkspaceItems.remove(item);
1122 break;
1123 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1124 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1125 break;
1126 }
1127 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001129 }
1130 }
Michael Jurka83df1882011-08-31 20:59:26 -07001131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 * Update the order of the workspace screens in the database. The array list contains
1137 * a list of screen ids in the order that they should appear.
1138 */
Winson Chungc9168342013-06-26 14:54:55 -07001139 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001140 // Log to disk
1141 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1142 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1143
Winson Chung64359a52013-07-08 17:17:08 -07001144 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 final ContentResolver cr = context.getContentResolver();
1146 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1147
1148 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001149 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 while (iter.hasNext()) {
1151 long id = iter.next();
1152 if (id < 0) {
1153 iter.remove();
1154 }
1155 }
1156
1157 Runnable r = new Runnable() {
1158 @Override
1159 public void run() {
Yura085c8532014-02-11 15:15:29 +00001160 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001162 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001163 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 for (int i = 0; i < count; i++) {
1165 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001166 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1168 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001169 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 }
Yura085c8532014-02-11 15:15:29 +00001171
1172 try {
1173 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1174 } catch (Exception ex) {
1175 throw new RuntimeException(ex);
1176 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001177
Winson Chungba9c37f2013-08-30 14:11:37 -07001178 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001179 sBgWorkspaceScreens.clear();
1180 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001181 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 }
1183 };
1184 runOnWorkerThread(r);
1185 }
1186
1187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 * Remove the contents of the specified folder from the database
1189 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001190 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 final ContentResolver cr = context.getContentResolver();
1192
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 Runnable r = new Runnable() {
1194 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001195 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001197 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 sBgItemsIdMap.remove(info.id);
1199 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 sBgWorkspaceItems.remove(info);
1201 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001202
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001203 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001204 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001206 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 for (ItemInfo childInfo : info.contents) {
1208 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001210 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001211 }
1212 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
1215
1216 /**
1217 * Set this as the current Launcher activity object for the loader.
1218 */
1219 public void initialize(Callbacks callbacks) {
1220 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001221 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1222 // workspace to prevent leaking Launcher activities on orientation change.
1223 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 mCallbacks = new WeakReference<Callbacks>(callbacks);
1225 }
1226 }
1227
Kenny Guyed131872014-04-30 03:02:21 +01001228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001230 int op = PackageUpdatedTask.OP_UPDATE;
1231 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1232 user));
1233 }
1234
1235 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001236 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001237 int op = PackageUpdatedTask.OP_REMOVE;
1238 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1239 user));
1240 }
1241
1242 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001243 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001244 int op = PackageUpdatedTask.OP_ADD;
1245 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1246 user));
1247 }
1248
1249 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001250 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001251 boolean replacing) {
1252 if (!replacing) {
1253 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1254 user));
1255 if (mAppsCanBeOnRemoveableStorage) {
1256 // Only rebind if we support removable storage. It catches the
1257 // case where
1258 // apps on the external sd card need to be reloaded
1259 startLoaderFromBackground();
1260 }
1261 } else {
1262 // If we are replacing then just update the packages in the list
1263 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1264 packageNames, user));
1265 }
1266 }
1267
1268 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001269 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001270 boolean replacing) {
1271 if (!replacing) {
1272 enqueuePackageUpdated(new PackageUpdatedTask(
1273 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1274 user));
1275 }
Kenny Guyed131872014-04-30 03:02:21 +01001276 }
1277
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001278 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1280 * ACTION_PACKAGE_CHANGED.
1281 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001282 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001283 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001284 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Joe Onorato36115782010-06-17 13:28:48 -04001286 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001287 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 // If we have changed locale we need to clear out the labels in all apps/workspace.
1289 forceReload();
1290 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1291 // Check if configuration change was an mcc/mnc change which would affect app resources
1292 // and we would need to clear out the labels in all apps/workspace. Same handling as
1293 // above for ACTION_LOCALE_CHANGED
1294 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001295 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001296 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001297 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001298 forceReload();
1299 }
1300 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001301 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001302 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1303 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001304 Callbacks callbacks = getCallback();
1305 if (callbacks != null) {
1306 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001307 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001308 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1309 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1310 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001311 }
1312 }
1313
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001314 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001315 resetLoadedState(true, true);
1316
Reena Lee93f824a2011-09-23 17:20:28 -07001317 // Do this here because if the launcher activity is running it will be restarted.
1318 // If it's not running startLoaderFromBackground will merely tell it that it needs
1319 // to reload.
1320 startLoaderFromBackground();
1321 }
1322
Winson Chungf0c6ae02012-03-21 16:10:31 -07001323 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1324 synchronized (mLock) {
1325 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1326 // mWorkspaceLoaded to true later
1327 stopLoaderLocked();
1328 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1329 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1330 }
1331 }
1332
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001333 /**
1334 * When the launcher is in the background, it's possible for it to miss paired
1335 * configuration changes. So whenever we trigger the loader from the background
1336 * tell the launcher that it needs to re-run the loader when it comes back instead
1337 * of doing it now.
1338 */
1339 public void startLoaderFromBackground() {
1340 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001341 Callbacks callbacks = getCallback();
1342 if (callbacks != null) {
1343 // Only actually run the loader if they're not paused.
1344 if (!callbacks.setLoadOnResume()) {
1345 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001346 }
1347 }
1348 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001349 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001350 }
Joe Onorato36115782010-06-17 13:28:48 -04001351 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001352
Reena Lee93f824a2011-09-23 17:20:28 -07001353 // If there is already a loader task running, tell it to stop.
1354 // returns true if isLaunching() was true on the old task
1355 private boolean stopLoaderLocked() {
1356 boolean isLaunching = false;
1357 LoaderTask oldTask = mLoaderTask;
1358 if (oldTask != null) {
1359 if (oldTask.isLaunching()) {
1360 isLaunching = true;
1361 }
1362 oldTask.stopLocked();
1363 }
1364 return isLaunching;
1365 }
1366
Adam Cohen1a85c582014-09-30 09:48:49 -07001367 public boolean isCurrentCallbacks(Callbacks callbacks) {
1368 return (mCallbacks != null && mCallbacks.get() == callbacks);
1369 }
1370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001372 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1373 }
1374
1375 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001376 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1377 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001378 synchronized (mLock) {
1379 if (DEBUG_LOADERS) {
1380 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 // Clear any deferred bind-runnables from the synchronized load process
1384 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001385 synchronized (mDeferredBindRunnables) {
1386 mDeferredBindRunnables.clear();
1387 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388
Joe Onorato36115782010-06-17 13:28:48 -04001389 // Don't bother to start the thread if we know it's not going to do anything
1390 if (mCallbacks != null && mCallbacks.get() != null) {
1391 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001392 // also, don't downgrade isLaunching if we're already running
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001393 isLaunching = stopLoaderLocked() || isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001394 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001395 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001396 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1398 } else {
1399 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1400 sWorker.post(mLoaderTask);
1401 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 }
1404 }
1405
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001406 void bindRemainingSynchronousPages() {
1407 // Post the remaining side pages to be loaded
1408 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001409 Runnable[] deferredBindRunnables = null;
1410 synchronized (mDeferredBindRunnables) {
1411 deferredBindRunnables = mDeferredBindRunnables.toArray(
1412 new Runnable[mDeferredBindRunnables.size()]);
1413 mDeferredBindRunnables.clear();
1414 }
1415 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001416 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001417 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001419
1420 // Run all the bind complete runnables after workspace is bound.
1421 if (!mBindCompleteRunnables.isEmpty()) {
1422 synchronized (mBindCompleteRunnables) {
1423 for (final Runnable r : mBindCompleteRunnables) {
1424 runOnWorkerThread(r);
1425 }
1426 mBindCompleteRunnables.clear();
1427 }
1428 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 }
1430
Joe Onorato36115782010-06-17 13:28:48 -04001431 public void stopLoader() {
1432 synchronized (mLock) {
1433 if (mLoaderTask != null) {
1434 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 }
1436 }
Joe Onorato36115782010-06-17 13:28:48 -04001437 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001438
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001439 /**
1440 * Loads the workspace screen ids in an ordered list.
1441 */
Adam Cohen091440a2015-03-18 14:16:05 -07001442 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001443 final ContentResolver contentResolver = context.getContentResolver();
1444 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001445
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001446 // Get screens ordered by rank.
1447 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1448 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1449 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001450 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001451 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001452 while (sc.moveToNext()) {
1453 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001454 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001455 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001456 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1457 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001458 }
1459 }
1460 } finally {
1461 sc.close();
1462 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001463 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001464 }
1465
Michael Jurkac57b7a82011-08-09 22:02:20 -07001466 public boolean isAllAppsLoaded() {
1467 return mAllAppsLoaded;
1468 }
1469
Winson Chung36a62fe2012-05-06 18:04:42 -07001470 boolean isLoadingWorkspace() {
1471 synchronized (mLock) {
1472 if (mLoaderTask != null) {
1473 return mLoaderTask.isLoadingWorkspace();
1474 }
1475 }
1476 return false;
1477 }
1478
Joe Onorato36115782010-06-17 13:28:48 -04001479 /**
1480 * Runnable for the thread that loads the contents of the launcher:
1481 * - workspace icons
1482 * - widgets
1483 * - all apps icons
1484 */
1485 private class LoaderTask implements Runnable {
1486 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001487 private boolean mIsLaunching;
Adam Cohen091440a2015-03-18 14:16:05 -07001488 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001489 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001490 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001491 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001492
Dan Sandlerd5024042014-01-09 15:01:33 -05001493 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001494 mContext = context;
1495 mIsLaunching = isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001496 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 }
1498
Joe Onorato36115782010-06-17 13:28:48 -04001499 boolean isLaunching() {
1500 return mIsLaunching;
1501 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502
Winson Chung36a62fe2012-05-06 18:04:42 -07001503 boolean isLoadingWorkspace() {
1504 return mIsLoadingAndBindingWorkspace;
1505 }
1506
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001507 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001508 mIsLoadingAndBindingWorkspace = true;
1509
Joe Onorato36115782010-06-17 13:28:48 -04001510 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001511 if (DEBUG_LOADERS) {
1512 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001513 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001514
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001515 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001516 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001517 synchronized (LoaderTask.this) {
1518 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001519 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001520 }
1521 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 }
1523 }
1524
Joe Onorato36115782010-06-17 13:28:48 -04001525 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001526 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001527 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001528
Joe Onorato36115782010-06-17 13:28:48 -04001529 private void waitForIdle() {
1530 // Wait until the either we're stopped or the other threads are done.
1531 // This way we don't start loading all apps until the workspace has settled
1532 // down.
1533 synchronized (LoaderTask.this) {
1534 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001535
Joe Onorato36115782010-06-17 13:28:48 -04001536 mHandler.postIdle(new Runnable() {
1537 public void run() {
1538 synchronized (LoaderTask.this) {
1539 mLoadAndBindStepFinished = true;
1540 if (DEBUG_LOADERS) {
1541 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001542 }
Joe Onorato36115782010-06-17 13:28:48 -04001543 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001544 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001545 }
Joe Onorato36115782010-06-17 13:28:48 -04001546 });
1547
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001548 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001549 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001550 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1551 // wait no longer than 1sec at a time
1552 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001553 } catch (InterruptedException ex) {
1554 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001555 }
1556 }
Joe Onorato36115782010-06-17 13:28:48 -04001557 if (DEBUG_LOADERS) {
1558 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001559 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001560 + "ms for previous step to finish binding");
1561 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001562 }
Joe Onorato36115782010-06-17 13:28:48 -04001563 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001564
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001566 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 // Ensure that we have a valid page index to load synchronously
1568 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1569 "valid page index");
1570 }
1571 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1572 // Ensure that we don't try and bind a specified page when the pages have not been
1573 // loaded already (we should load everything asynchronously in that case)
1574 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1575 }
1576 synchronized (mLock) {
1577 if (mIsLoaderTaskRunning) {
1578 // Ensure that we are never running the background loading at this point since
1579 // we also touch the background collections
1580 throw new RuntimeException("Error! Background loading is already running");
1581 }
1582 }
1583
1584 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1585 // data structures, we can't allow any other thread to touch that data, but because
1586 // this call is synchronous, we can get away with not locking).
1587
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001588 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001589 // operations from the previous activity. We need to ensure that all queued operations
1590 // are executed before any synchronous binding work is done.
1591 mHandler.flush();
1592
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001593 // Divide the set of loaded items into those that we are binding synchronously, and
1594 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001595 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001596 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1597 // arise from that.
1598 onlyBindAllApps();
1599 }
1600
Joe Onorato36115782010-06-17 13:28:48 -04001601 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001603 if (mStopped) {
1604 return;
1605 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001606 mIsLoaderTaskRunning = true;
1607 }
Joe Onorato36115782010-06-17 13:28:48 -04001608 // Optimize for end-user experience: if the Launcher is up and // running with the
1609 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1610 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001611 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001612 // Elevate priority when Home launches for the first time to avoid
1613 // starving at boot time. Staring at a blank home is not cool.
1614 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001615 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1616 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001617 android.os.Process.setThreadPriority(mIsLaunching
1618 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1619 }
Winson Chung64359a52013-07-08 17:17:08 -07001620 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001621 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001622
Joe Onorato36115782010-06-17 13:28:48 -04001623 if (mStopped) {
1624 break keep_running;
1625 }
1626
1627 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1628 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001629 synchronized (mLock) {
1630 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001631 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001632 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1633 }
1634 }
Joe Onorato36115782010-06-17 13:28:48 -04001635 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001636
1637 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001638 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1639 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001640
1641 // Restore the default thread priority after we are done loading items
1642 synchronized (mLock) {
1643 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1644 }
Joe Onorato36115782010-06-17 13:28:48 -04001645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646
Joe Onorato36115782010-06-17 13:28:48 -04001647 // Clear out this reference, otherwise we end up holding it until all of the
1648 // callback runnables are done.
1649 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650
Joe Onorato36115782010-06-17 13:28:48 -04001651 synchronized (mLock) {
1652 // If we are still the last one to be scheduled, remove ourselves.
1653 if (mLoaderTask == this) {
1654 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001657 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001658 }
Joe Onorato36115782010-06-17 13:28:48 -04001659 }
1660
1661 public void stopLocked() {
1662 synchronized (LoaderTask.this) {
1663 mStopped = true;
1664 this.notify();
1665 }
1666 }
1667
1668 /**
1669 * Gets the callbacks object. If we've been stopped, or if the launcher object
1670 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1671 * object that was around when the deferred message was scheduled, and if there's
1672 * a new Callbacks object around then also return null. This will save us from
1673 * calling onto it with data that will be ignored.
1674 */
1675 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1676 synchronized (mLock) {
1677 if (mStopped) {
1678 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001679 }
Joe Onorato36115782010-06-17 13:28:48 -04001680
1681 if (mCallbacks == null) {
1682 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001683 }
Joe Onorato36115782010-06-17 13:28:48 -04001684
1685 final Callbacks callbacks = mCallbacks.get();
1686 if (callbacks != oldCallbacks) {
1687 return null;
1688 }
1689 if (callbacks == null) {
1690 Log.w(TAG, "no mCallbacks");
1691 return null;
1692 }
1693
1694 return callbacks;
1695 }
1696 }
1697
1698 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001699 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001700 LauncherAppState app = LauncherAppState.getInstance();
1701 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001702 final int countX = (int) grid.numColumns;
1703 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001704
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001706 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001707 // Return early if we detect that an item is under the hotseat button
1708 if (mCallbacks == null ||
1709 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001710 Log.e(TAG, "Error loading shortcut into hotseat " + item
1711 + " into position (" + item.screenId + ":" + item.cellX + ","
1712 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001713 return false;
1714 }
1715
Dan Sandler295ae182013-12-10 16:05:47 -05001716 final ItemInfo[][] hotseatItems =
1717 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1718
Adam Cohenae4409d2013-11-26 10:34:59 -08001719 if (item.screenId >= grid.numHotseatIcons) {
1720 Log.e(TAG, "Error loading shortcut " + item
1721 + " into hotseat position " + item.screenId
1722 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1723 + ")");
1724 return false;
1725 }
1726
Dan Sandler295ae182013-12-10 16:05:47 -05001727 if (hotseatItems != null) {
1728 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001729 Log.e(TAG, "Error loading shortcut into hotseat " + item
1730 + " into position (" + item.screenId + ":" + item.cellX + ","
1731 + item.cellY + ") occupied by "
1732 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1733 [(int) item.screenId][0]);
1734 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001735 } else {
1736 hotseatItems[(int) item.screenId][0] = item;
1737 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001738 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001739 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001740 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001741 items[(int) item.screenId][0] = item;
1742 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001743 return true;
1744 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001745 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1746 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001747 return true;
1748 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001749
Adam Cohendcd297f2013-06-18 13:13:40 -07001750 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001751 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001752 occupied.put(item.screenId, items);
1753 }
1754
Dan Sandler295ae182013-12-10 16:05:47 -05001755 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001756 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1757 item.cellX < 0 || item.cellY < 0 ||
1758 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1759 Log.e(TAG, "Error loading shortcut " + item
1760 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1761 + item.cellX + "," + item.cellY
1762 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1763 return false;
1764 }
1765
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001766 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001767 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1768 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001769 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001770 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001771 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001772 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001773 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001774 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001775 return false;
1776 }
1777 }
1778 }
1779 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1780 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001781 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001782 }
1783 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001784
Joe Onorato36115782010-06-17 13:28:48 -04001785 return true;
1786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787
Winson Chungba9c37f2013-08-30 14:11:37 -07001788 /** Clears all the sBg data structures */
1789 private void clearSBgDataStructures() {
1790 synchronized (sBgLock) {
1791 sBgWorkspaceItems.clear();
1792 sBgAppWidgets.clear();
1793 sBgFolders.clear();
1794 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001795 sBgWorkspaceScreens.clear();
1796 }
1797 }
1798
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001799 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001800 // Log to disk
1801 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1802
Joe Onorato36115782010-06-17 13:28:48 -04001803 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Joe Onorato36115782010-06-17 13:28:48 -04001805 final Context context = mContext;
1806 final ContentResolver contentResolver = context.getContentResolver();
1807 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001808 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001809 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1810 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001811 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001812
Winson Chung892c74d2013-08-22 16:15:50 -07001813 LauncherAppState app = LauncherAppState.getInstance();
1814 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1815 int countX = (int) grid.numColumns;
1816 int countY = (int) grid.numRows;
1817
Dan Sandlerd5024042014-01-09 15:01:33 -05001818 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1819 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1820 LauncherAppState.getLauncherProvider().deleteDatabase();
1821 }
1822
1823 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1824 // append the user's Launcher2 shortcuts
1825 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1826 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1827 } else {
1828 // Make sure the default workspace is loaded
1829 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001830 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001831 }
Adam Cohene25af792013-06-06 23:08:25 -07001832
Winson Chung2abf94d2012-07-18 18:16:38 -07001833 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001834 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001835 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001836 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001837
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001839 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001840 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001841 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001842 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001843
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 // +1 for the hotseat (it can be larger than the workspace)
1845 // Load workspace in reverse order to ensure that latest items are loaded first (and
1846 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001847 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 try {
1850 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1851 final int intentIndex = c.getColumnIndexOrThrow
1852 (LauncherSettings.Favorites.INTENT);
1853 final int titleIndex = c.getColumnIndexOrThrow
1854 (LauncherSettings.Favorites.TITLE);
1855 final int iconTypeIndex = c.getColumnIndexOrThrow(
1856 LauncherSettings.Favorites.ICON_TYPE);
1857 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1858 final int iconPackageIndex = c.getColumnIndexOrThrow(
1859 LauncherSettings.Favorites.ICON_PACKAGE);
1860 final int iconResourceIndex = c.getColumnIndexOrThrow(
1861 LauncherSettings.Favorites.ICON_RESOURCE);
1862 final int containerIndex = c.getColumnIndexOrThrow(
1863 LauncherSettings.Favorites.CONTAINER);
1864 final int itemTypeIndex = c.getColumnIndexOrThrow(
1865 LauncherSettings.Favorites.ITEM_TYPE);
1866 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1867 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001868 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1869 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 final int screenIndex = c.getColumnIndexOrThrow(
1871 LauncherSettings.Favorites.SCREEN);
1872 final int cellXIndex = c.getColumnIndexOrThrow
1873 (LauncherSettings.Favorites.CELLX);
1874 final int cellYIndex = c.getColumnIndexOrThrow
1875 (LauncherSettings.Favorites.CELLY);
1876 final int spanXIndex = c.getColumnIndexOrThrow
1877 (LauncherSettings.Favorites.SPANX);
1878 final int spanYIndex = c.getColumnIndexOrThrow(
1879 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001880 final int rankIndex = c.getColumnIndexOrThrow(
1881 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001882 final int restoredIndex = c.getColumnIndexOrThrow(
1883 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001884 final int profileIdIndex = c.getColumnIndexOrThrow(
1885 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001886 final int optionsIndex = c.getColumnIndexOrThrow(
1887 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001888
Sunny Goyal7f834d22015-04-21 10:10:23 -07001889 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1890 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1891 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1892 }
1893
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 ShortcutInfo info;
1895 String intentDescription;
1896 LauncherAppWidgetInfo appWidgetInfo;
1897 int container;
1898 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001899 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001900 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001901 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001902
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903 while (!mStopped && c.moveToNext()) {
1904 try {
1905 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001906 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001907 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 switch (itemType) {
1910 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1911 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001912 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001914 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001915 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001916 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001917 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001918 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001919 if (user == null) {
1920 // User has been deleted remove the item.
1921 itemsToRemove.add(id);
1922 continue;
1923 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 try {
1925 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001926 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001927 if (cn != null && cn.getPackageName() != null) {
1928 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1929 cn.getPackageName(), user);
1930 boolean validComponent = validPkg &&
1931 launcherApps.isActivityEnabledForProfile(cn, user);
1932
1933 if (validComponent) {
1934 if (restored) {
1935 // no special handling necessary for this item
1936 restoredRows.add(id);
1937 restored = false;
1938 }
1939 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001940 intent = null;
1941 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1942 // We allow auto install apps to have their intent
1943 // updated after an install.
1944 intent = manager.getLaunchIntentForPackage(
1945 cn.getPackageName());
1946 if (intent != null) {
1947 ContentValues values = new ContentValues();
1948 values.put(LauncherSettings.Favorites.INTENT,
1949 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001950 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001951 }
1952 }
1953
1954 if (intent == null) {
1955 // The app is installed but the component is no
1956 // longer available.
1957 Launcher.addDumpLog(TAG,
1958 "Invalid component removed: " + cn, true);
1959 itemsToRemove.add(id);
1960 continue;
1961 } else {
1962 // no special handling necessary for this item
1963 restoredRows.add(id);
1964 restored = false;
1965 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001966 } else if (restored) {
1967 // Package is not yet available but might be
1968 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001969 Launcher.addDumpLog(TAG,
1970 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001971
1972 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1973 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001974 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001975 // App restore has started. Update the flag
1976 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1977 ContentValues values = new ContentValues();
1978 values.put(LauncherSettings.Favorites.RESTORED,
1979 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001980 updateItem(id, values);
1981 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1982 // This is a common app. Try to replace this.
1983 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1984 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1985 if (parser.findDefaultApp()) {
1986 // Default app found. Replace it.
1987 intent = parser.parsedIntent;
1988 cn = intent.getComponent();
1989 ContentValues values = parser.parsedValues;
1990 values.put(LauncherSettings.Favorites.RESTORED, 0);
1991 updateItem(id, values);
1992 restored = false;
1993 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001994
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001995 } else if (REMOVE_UNRESTORED_ICONS) {
1996 Launcher.addDumpLog(TAG,
1997 "Unrestored package removed: " + cn, true);
1998 itemsToRemove.add(id);
1999 continue;
2000 }
Sunny Goyal94485362014-09-18 16:13:58 -07002001 } else if (REMOVE_UNRESTORED_ICONS) {
2002 Launcher.addDumpLog(TAG,
2003 "Unrestored package removed: " + cn, true);
2004 itemsToRemove.add(id);
2005 continue;
2006 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002007 } else if (launcherApps.isAppEnabled(
2008 manager, cn.getPackageName(),
2009 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2010 // Package is present but not available.
2011 allowMissingTarget = true;
2012 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2013 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002014 // SdCard is not ready yet. Package might get available,
2015 // once it is ready.
2016 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2017 + " (check again later)", true);
2018 HashSet<String> pkgs = sPendingPackages.get(user);
2019 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002020 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002021 sPendingPackages.put(user, pkgs);
2022 }
2023 pkgs.add(cn.getPackageName());
2024 allowMissingTarget = true;
2025 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002026
2027 } else {
2028 // Do not wait for external media load anymore.
2029 // Log the invalid package, and remove it
2030 Launcher.addDumpLog(TAG,
2031 "Invalid package removed: " + cn, true);
2032 itemsToRemove.add(id);
2033 continue;
Winson Chungee055712013-07-30 14:46:24 -07002034 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002035 } else if (cn == null) {
2036 // For shortcuts with no component, keep them as they are
2037 restoredRows.add(id);
2038 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002039 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002041 Launcher.addDumpLog(TAG,
2042 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 continue;
2044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002045
Sunny Goyal34b65272015-03-11 16:56:52 -07002046 container = c.getInt(containerIndex);
2047 boolean useLowResIcon = container >= 0 &&
2048 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2049
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002050 if (itemReplaced) {
2051 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002052 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002053 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002054 } else {
2055 // Don't replace items for other profiles.
2056 itemsToRemove.add(id);
2057 continue;
2058 }
2059 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002060 if (user.equals(UserHandleCompat.myUserHandle())) {
2061 Launcher.addDumpLog(TAG,
2062 "constructing info for partially restored package",
2063 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002064 info = getRestoredItemInfo(c, titleIndex, intent,
2065 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002066 intent = getRestoredItemIntent(c, context, intent);
2067 } else {
2068 // Don't restore items for other profiles.
2069 itemsToRemove.add(id);
2070 continue;
2071 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002072 } else if (itemType ==
2073 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002074 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002075 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 } else {
2077 info = getShortcutInfo(c, context, iconTypeIndex,
2078 iconPackageIndex, iconResourceIndex, iconIndex,
2079 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002080
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 // App shortcuts that used to be automatically added to Launcher
2082 // didn't always have the correct intent flags set, so do that
2083 // here
2084 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002085 intent.getCategories() != null &&
2086 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002087 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 intent.addFlags(
2089 Intent.FLAG_ACTIVITY_NEW_TASK |
2090 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2091 }
Michael Jurka96879562012-03-22 05:54:33 -07002092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002095 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002098 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 info.cellX = c.getInt(cellXIndex);
2100 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002101 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002102 info.spanX = 1;
2103 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002104 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002105 info.isDisabled = disabledState;
2106 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2107 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2108 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002109
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002111 if (!checkItemPlacement(occupied, info)) {
2112 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 break;
2114 }
2115
Sunny Goyal756adbc2015-04-16 15:20:51 -07002116 if (restored) {
2117 ComponentName cn = info.getTargetComponent();
2118 if (cn != null) {
2119 Integer progress = installingPkgs.get(cn.getPackageName());
2120 if (progress != null) {
2121 info.setInstallProgress(progress);
2122 } else {
2123 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2124 }
2125 }
2126 }
2127
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 switch (container) {
2129 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2130 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2131 sBgWorkspaceItems.add(info);
2132 break;
2133 default:
2134 // Item is in a user folder
2135 FolderInfo folderInfo =
2136 findOrMakeFolder(sBgFolders, container);
2137 folderInfo.add(info);
2138 break;
2139 }
2140 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002141 } else {
2142 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 }
2144 break;
2145
2146 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2147 id = c.getLong(idIndex);
2148 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2149
Winson Chung82b016c2015-05-08 17:00:10 -07002150 folderInfo.title = Utilities.trim(c.getString(titleIndex));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002154 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 folderInfo.cellX = c.getInt(cellXIndex);
2156 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002157 folderInfo.spanX = 1;
2158 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002159 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160
Daniel Sandler8802e962010-05-26 16:28:16 -04002161 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002162 if (!checkItemPlacement(occupied, folderInfo)) {
2163 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002164 break;
2165 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002166
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002168 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2169 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2170 sBgWorkspaceItems.add(folderInfo);
2171 break;
Joe Onorato36115782010-06-17 13:28:48 -04002172 }
Joe Onorato17a89222011-02-08 17:26:11 -08002173
Chris Wrenf4d08112014-01-16 18:13:56 -05002174 if (restored) {
2175 // no special handling required for restored folders
2176 restoredRows.add(id);
2177 }
2178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2180 sBgFolders.put(folderInfo.id, folderInfo);
2181 break;
2182
2183 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002184 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002185 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002186 boolean customWidget = itemType ==
2187 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2188
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002190 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002191 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002192 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002193 user = allUsers.get(serialNumber);
2194 if (user == null) {
2195 itemsToRemove.add(id);
2196 continue;
2197 }
2198
Sunny Goyalff572272014-07-23 13:58:07 -07002199 final ComponentName component =
2200 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002201
Sunny Goyal651077b2014-06-30 14:15:31 -07002202 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002203 final boolean isIdValid = (restoreStatus &
2204 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002205 final boolean wasProviderReady = (restoreStatus &
2206 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002207
Adam Cohen59400422014-03-05 18:07:04 -08002208 final LauncherAppWidgetProviderInfo provider =
2209 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002210 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002211 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002212
2213 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002214 if (!isSafeMode && !customWidget &&
2215 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002216 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002217 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002218
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002220 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 itemsToRemove.add(id);
2222 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002223 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002224 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2225 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002226
2227 if (!customWidget) {
2228 int[] minSpan =
2229 Launcher.getMinSpanForWidget(context, provider);
2230 appWidgetInfo.minSpanX = minSpan[0];
2231 appWidgetInfo.minSpanY = minSpan[1];
2232 }
Sunny Goyalff572272014-07-23 13:58:07 -07002233
2234 int status = restoreStatus;
2235 if (!wasProviderReady) {
2236 // If provider was not previously ready, update the
2237 // status and UI flag.
2238
2239 // Id would be valid only if the widget restore broadcast was received.
2240 if (isIdValid) {
2241 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2242 } else {
2243 status &= ~LauncherAppWidgetInfo
2244 .FLAG_PROVIDER_NOT_READY;
2245 }
2246 }
2247 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002248 } else {
2249 Log.v(TAG, "Widget restore pending id=" + id
2250 + " appWidgetId=" + appWidgetId
2251 + " status =" + restoreStatus);
2252 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002253 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002254 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002255 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002256
2257 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2258 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002259 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002260 // App restore has started. Update the flag
2261 appWidgetInfo.restoreStatus |=
2262 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002263 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002264 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002265 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002266 itemsToRemove.add(id);
2267 continue;
2268 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002269
2270 appWidgetInfo.installProgress =
2271 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002272 }
Sunny Goyalff572272014-07-23 13:58:07 -07002273
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 appWidgetInfo.cellX = c.getInt(cellXIndex);
2277 appWidgetInfo.cellY = c.getInt(cellYIndex);
2278 appWidgetInfo.spanX = c.getInt(spanXIndex);
2279 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002280
Adam Cohen59400422014-03-05 18:07:04 -08002281 if (!customWidget) {
2282 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2283 appWidgetInfo.minSpanX = minSpan[0];
2284 appWidgetInfo.minSpanY = minSpan[1];
2285 }
2286
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 container = c.getInt(containerIndex);
2288 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2289 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2290 Log.e(TAG, "Widget found where container != " +
2291 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2292 continue;
2293 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294
Adam Cohene25af792013-06-06 23:08:25 -07002295 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002297 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2298 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 break;
2300 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002301
Adam Cohen59400422014-03-05 18:07:04 -08002302 if (!customWidget) {
2303 String providerName =
2304 appWidgetInfo.providerName.flattenToString();
2305 if (!providerName.equals(savedProvider) ||
2306 (appWidgetInfo.restoreStatus != restoreStatus)) {
2307 ContentValues values = new ContentValues();
2308 values.put(
2309 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2310 providerName);
2311 values.put(LauncherSettings.Favorites.RESTORED,
2312 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002313 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002314 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002315 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2317 sBgAppWidgets.add(appWidgetInfo);
2318 }
Joe Onorato36115782010-06-17 13:28:48 -04002319 break;
2320 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002322 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002323 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 }
2325 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002326 if (c != null) {
2327 c.close();
2328 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 }
2330
Winson Chungba9c37f2013-08-30 14:11:37 -07002331 // Break early if we've stopped loading
2332 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002333 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002334 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002335 }
2336
Sunny Goyal1936ec02015-05-12 11:03:04 -07002337 // Remove any empty folder
2338 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2339 for (ItemInfo item: sBgItemsIdMap) {
2340 long container = item.container;
2341 if (emptyFolders.containsKey(container)) {
2342 emptyFolders.remove(container);
2343 }
2344 }
2345 for (FolderInfo folder : emptyFolders) {
2346 long folderId = folder.id;
2347 sBgFolders.remove(folderId);
2348 sBgItemsIdMap.remove(folderId);
2349 sBgWorkspaceItems.remove(folder);
2350 itemsToRemove.add(folderId);
2351 }
2352
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 if (itemsToRemove.size() > 0) {
2354 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002355 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 // Remove dead items
2357 for (long id : itemsToRemove) {
2358 if (DEBUG_LOADERS) {
2359 Log.d(TAG, "Removed id = " + id);
2360 }
2361 // Don't notify content observers
2362 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002363 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 } catch (RemoteException e) {
2365 Log.w(TAG, "Could not remove id = " + id);
2366 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002367 }
2368 }
2369
Chris Wrenf4d08112014-01-16 18:13:56 -05002370 if (restoredRows.size() > 0) {
2371 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002372 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002373 // Update restored items that no longer require special handling
2374 try {
2375 StringBuilder selectionBuilder = new StringBuilder();
2376 selectionBuilder.append(LauncherSettings.Favorites._ID);
2377 selectionBuilder.append(" IN (");
2378 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2379 selectionBuilder.append(")");
2380 ContentValues values = new ContentValues();
2381 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002382 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002383 values, selectionBuilder.toString(), null);
2384 } catch (RemoteException e) {
2385 Log.w(TAG, "Could not update restored rows");
2386 }
2387 }
2388
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002389 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2390 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002391 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002392 null, sWorker);
2393 }
2394
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002395 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2396 // Log to disk
2397 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2398 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002399
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002400 // Remove any empty screens
2401 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002402 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002403 long screenId = item.screenId;
2404 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2405 unusedScreens.contains(screenId)) {
2406 unusedScreens.remove(screenId);
2407 }
2408 }
2409
2410 // If there are any empty screens remove them, and update.
2411 if (unusedScreens.size() != 0) {
2412 // Log to disk
2413 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2414 TextUtils.join(", ", unusedScreens), true);
2415
2416 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002418 }
2419
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420 if (DEBUG_LOADERS) {
2421 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2422 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002423 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002424 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002426
Sunny Goyale2df0622015-04-24 11:27:00 -07002427 for (int i = 0; i < nScreens; i++) {
2428 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002429 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 line += " | ";
2431 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002432 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002433 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002434 if (x < screen.length && y < screen[x].length) {
2435 line += (screen[x][y] != null) ? "#" : ".";
2436 } else {
2437 line += "!";
2438 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439 }
Joe Onorato36115782010-06-17 13:28:48 -04002440 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002442 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002443 }
Joe Onorato36115782010-06-17 13:28:48 -04002444 }
Adam Cohene25af792013-06-06 23:08:25 -07002445 }
2446
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002447 /**
2448 * Partially updates the item without any notification. Must be called on the worker thread.
2449 */
2450 private void updateItem(long itemId, ContentValues update) {
2451 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002452 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002453 update,
2454 BaseColumns._ID + "= ?",
2455 new String[]{Long.toString(itemId)});
2456 }
2457
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 /** Filters the set of items who are directly or indirectly (via another container) on the
2459 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002460 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461 ArrayList<ItemInfo> allWorkspaceItems,
2462 ArrayList<ItemInfo> currentScreenItems,
2463 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002464 // Purge any null ItemInfos
2465 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2466 while (iter.hasNext()) {
2467 ItemInfo i = iter.next();
2468 if (i == null) {
2469 iter.remove();
2470 }
2471 }
2472
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 // Order the set of items by their containers first, this allows use to walk through the
2474 // list sequentially, build up a list of containers that are in the specified screen,
2475 // as well as all items in those containers.
2476 Set<Long> itemsOnScreen = new HashSet<Long>();
2477 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2478 @Override
2479 public int compare(ItemInfo lhs, ItemInfo rhs) {
2480 return (int) (lhs.container - rhs.container);
2481 }
2482 });
2483 for (ItemInfo info : allWorkspaceItems) {
2484 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002485 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 currentScreenItems.add(info);
2487 itemsOnScreen.add(info.id);
2488 } else {
2489 otherScreenItems.add(info);
2490 }
2491 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2492 currentScreenItems.add(info);
2493 itemsOnScreen.add(info.id);
2494 } else {
2495 if (itemsOnScreen.contains(info.container)) {
2496 currentScreenItems.add(info);
2497 itemsOnScreen.add(info.id);
2498 } else {
2499 otherScreenItems.add(info);
2500 }
2501 }
2502 }
2503 }
2504
2505 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002506 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 ArrayList<LauncherAppWidgetInfo> appWidgets,
2508 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2509 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510
2511 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002512 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002514 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 currentScreenWidgets.add(widget);
2516 } else {
2517 otherScreenWidgets.add(widget);
2518 }
2519 }
2520 }
2521
2522 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002524 LongArrayMap<ItemInfo> itemsIdMap,
2525 LongArrayMap<FolderInfo> folders,
2526 LongArrayMap<FolderInfo> currentScreenFolders,
2527 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528
Sunny Goyale2df0622015-04-24 11:27:00 -07002529 int total = folders.size();
2530 for (int i = 0; i < total; i++) {
2531 long id = folders.keyAt(i);
2532 FolderInfo folder = folders.valueAt(i);
2533
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002535 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002537 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 currentScreenFolders.put(id, folder);
2539 } else {
2540 otherScreenFolders.put(id, folder);
2541 }
2542 }
2543 }
2544
2545 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2546 * right) */
2547 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002548 final LauncherAppState app = LauncherAppState.getInstance();
2549 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 // XXX: review this
2551 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002552 @Override
2553 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002554 int cellCountX = (int) grid.numColumns;
2555 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002556 int screenOffset = cellCountX * cellCountY;
2557 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002558 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002559 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002560 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002561 rhs.cellY * cellCountX + rhs.cellX);
2562 return (int) (lr - rr);
2563 }
2564 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002566
Adam Cohendcd297f2013-06-18 13:13:40 -07002567 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2568 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002569 final Runnable r = new Runnable() {
2570 @Override
2571 public void run() {
2572 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2573 if (callbacks != null) {
2574 callbacks.bindScreens(orderedScreens);
2575 }
2576 }
2577 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002578 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002579 }
2580
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002581 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2582 final ArrayList<ItemInfo> workspaceItems,
2583 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002584 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002586
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002587 final boolean postOnMainThread = (deferredBindRunnables != null);
2588
2589 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002590 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002592 final int start = i;
2593 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 final Runnable r = new Runnable() {
2595 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002596 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002597 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002598 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002599 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2600 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002601 }
2602 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603 };
2604 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002605 synchronized (deferredBindRunnables) {
2606 deferredBindRunnables.add(r);
2607 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002608 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002609 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610 }
Joe Onorato36115782010-06-17 13:28:48 -04002611 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002612
2613 // Bind the folders
2614 if (!folders.isEmpty()) {
2615 final Runnable r = new Runnable() {
2616 public void run() {
2617 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2618 if (callbacks != null) {
2619 callbacks.bindFolders(folders);
2620 }
2621 }
2622 };
2623 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002624 synchronized (deferredBindRunnables) {
2625 deferredBindRunnables.add(r);
2626 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002628 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 }
2630 }
2631
2632 // Bind the widgets, one at a time
2633 N = appWidgets.size();
2634 for (int i = 0; i < N; i++) {
2635 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2636 final Runnable r = new Runnable() {
2637 public void run() {
2638 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2639 if (callbacks != null) {
2640 callbacks.bindAppWidget(widget);
2641 }
2642 }
2643 };
2644 if (postOnMainThread) {
2645 deferredBindRunnables.add(r);
2646 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002647 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 }
2649 }
2650 }
2651
2652 /**
2653 * Binds all loaded data to actual views on the main thread.
2654 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002655 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002656 final long t = SystemClock.uptimeMillis();
2657 Runnable r;
2658
2659 // Don't use these two variables in any of the callback runnables.
2660 // Otherwise we hold a reference to them.
2661 final Callbacks oldCallbacks = mCallbacks.get();
2662 if (oldCallbacks == null) {
2663 // This launcher has exited and nobody bothered to tell us. Just bail.
2664 Log.w(TAG, "LoaderTask running with no launcher");
2665 return;
2666 }
2667
Winson Chung9b9fb962013-11-15 15:39:34 -08002668 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002669 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2670 ArrayList<LauncherAppWidgetInfo> appWidgets =
2671 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002672 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002673
2674 final LongArrayMap<FolderInfo> folders;
2675 final LongArrayMap<ItemInfo> itemsIdMap;
2676
Winson Chung2abf94d2012-07-18 18:16:38 -07002677 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678 workspaceItems.addAll(sBgWorkspaceItems);
2679 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002680 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002681
2682 folders = sBgFolders.clone();
2683 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002684 }
2685
Derek Prothro7aff3992013-12-10 14:00:37 -05002686 final boolean isLoadingSynchronously =
2687 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002688 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002689 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002690 if (currScreen >= orderedScreenIds.size()) {
2691 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002692 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002693 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002694 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002695 final long currentScreenId = currentScreen < 0
2696 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002697
2698 // Load all the items that are on the current page first (and in the process, unbind
2699 // all the existing workspace items before we call startBinding() below.
2700 unbindWorkspaceItemsOnMainThread();
2701
2702 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002703 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2704 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2705 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2706 new ArrayList<LauncherAppWidgetInfo>();
2707 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2708 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002709 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2710 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002711
Winson Chung9b9fb962013-11-15 15:39:34 -08002712 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002713 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002714 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002715 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002716 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002717 otherFolders);
2718 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2719 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2720
2721 // Tell the workspace that we're about to start binding items
2722 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002723 public void run() {
2724 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2725 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002726 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
2728 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002729 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002730 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002731
Adam Cohendcd297f2013-06-18 13:13:40 -07002732 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2733
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002734 // Load items on the current page
2735 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2736 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002737 if (isLoadingSynchronously) {
2738 r = new Runnable() {
2739 public void run() {
2740 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002741 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002742 callbacks.onPageBoundSynchronously(currentScreen);
2743 }
2744 }
2745 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002746 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002747 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002748
Winson Chung4a2afa32012-07-19 14:53:05 -07002749 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2750 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002751 synchronized (mDeferredBindRunnables) {
2752 mDeferredBindRunnables.clear();
2753 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002754 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002755 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002756
2757 // Tell the workspace that we're done binding items
2758 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002759 public void run() {
2760 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2761 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002762 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002763 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002764
Winson Chung98e030b2012-05-07 16:01:11 -07002765 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002766 if (DEBUG_LOADERS) {
2767 Log.d(TAG, "bound workspace in "
2768 + (SystemClock.uptimeMillis()-t) + "ms");
2769 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002770
2771 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002772 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002773 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002774 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002775 synchronized (mDeferredBindRunnables) {
2776 mDeferredBindRunnables.add(r);
2777 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002778 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002779 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002780 }
Joe Onorato36115782010-06-17 13:28:48 -04002781 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002782
Joe Onorato36115782010-06-17 13:28:48 -04002783 private void loadAndBindAllApps() {
2784 if (DEBUG_LOADERS) {
2785 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2786 }
2787 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002788 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002789 synchronized (LoaderTask.this) {
2790 if (mStopped) {
2791 return;
2792 }
2793 }
Sunny Goyal77919b92015-05-06 16:53:21 -07002794 updateAllAppsIconsCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002795 synchronized (LoaderTask.this) {
2796 if (mStopped) {
2797 return;
2798 }
2799 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002800 }
Joe Onorato36115782010-06-17 13:28:48 -04002801 } else {
2802 onlyBindAllApps();
2803 }
2804 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002805
Joe Onorato36115782010-06-17 13:28:48 -04002806 private void onlyBindAllApps() {
2807 final Callbacks oldCallbacks = mCallbacks.get();
2808 if (oldCallbacks == null) {
2809 // This launcher has exited and nobody bothered to tell us. Just bail.
2810 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2811 return;
2812 }
2813
2814 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002815 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002816 final ArrayList<AppInfo> list
2817 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002818 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002819 public void run() {
2820 final long t = SystemClock.uptimeMillis();
2821 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2822 if (callbacks != null) {
2823 callbacks.bindAllApplications(list);
2824 }
2825 if (DEBUG_LOADERS) {
2826 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2827 + (SystemClock.uptimeMillis()-t) + "ms");
2828 }
2829 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002830 };
2831 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002832 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002833 r.run();
2834 } else {
2835 mHandler.post(r);
2836 }
Hyunyoung Song219d0482015-05-07 12:51:42 -07002837 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2838 false /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002839 }
2840
Winson Chung64359a52013-07-08 17:17:08 -07002841 private void loadAllApps() {
2842 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002843
Joe Onorato36115782010-06-17 13:28:48 -04002844 final Callbacks oldCallbacks = mCallbacks.get();
2845 if (oldCallbacks == null) {
2846 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002847 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002848 return;
2849 }
2850
Kenny Guyed131872014-04-30 03:02:21 +01002851 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2852
Winson Chung64359a52013-07-08 17:17:08 -07002853 // Clear the list of apps
2854 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002855 for (UserHandleCompat user : profiles) {
2856 // Query for the set of apps
2857 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002858 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002859 if (DEBUG_LOADERS) {
2860 Log.d(TAG, "getActivityList took "
2861 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2862 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2863 }
2864 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002865 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002866 if (apps == null || apps.isEmpty()) {
2867 return;
2868 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002869
Kenny Guyed131872014-04-30 03:02:21 +01002870 // Create the ApplicationInfos
2871 for (int i = 0; i < apps.size(); i++) {
2872 LauncherActivityInfoCompat app = apps.get(i);
2873 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002874 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002875 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002876
Sunny Goyal756a28a2015-04-23 17:07:55 -07002877 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2878 if (heuristic != null) {
2879 runAfterBindCompletes(new Runnable() {
2880
2881 @Override
2882 public void run() {
2883 heuristic.processUserApps(apps);
2884 }
2885 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002886 }
Winson Chung64359a52013-07-08 17:17:08 -07002887 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002888 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002889 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2890 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002891
2892 // Post callback on main thread
2893 mHandler.post(new Runnable() {
2894 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002895
Winson Chung64359a52013-07-08 17:17:08 -07002896 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002897 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002898 if (callbacks != null) {
2899 callbacks.bindAllApplications(added);
2900 if (DEBUG_LOADERS) {
2901 Log.d(TAG, "bound " + added.size() + " apps in "
2902 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2903 }
2904 } else {
2905 Log.i(TAG, "not binding apps: no Launcher activity");
2906 }
2907 }
2908 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002909 // Cleanup any data stored for a deleted user.
2910 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002911
Hyunyoung Song219d0482015-05-07 12:51:42 -07002912 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2913 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002914 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002915 Log.d(TAG, "Icons processed in "
2916 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002917 }
2918 }
2919
Sunny Goyal77919b92015-05-06 16:53:21 -07002920 private void updateAllAppsIconsCache() {
2921 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2922
2923 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2924 // Query for the set of apps
2925 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2926 // Fail if we don't have any apps
2927 // TODO: Fix this. Only fail for the current user.
2928 if (apps == null || apps.isEmpty()) {
2929 return;
2930 }
2931
2932 // Update icon cache
2933 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2934
2935 // If any package icon has changed (app was updated while launcher was dead),
2936 // update the corresponding shortcuts.
2937 if (!updatedPackages.isEmpty()) {
2938 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2939 synchronized (sBgLock) {
2940 for (ItemInfo info : sBgItemsIdMap) {
2941 if (info instanceof ShortcutInfo && user.equals(info.user)
2942 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2943 ShortcutInfo si = (ShortcutInfo) info;
2944 ComponentName cn = si.getTargetComponent();
2945 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2946 si.updateIcon(mIconCache);
2947 updatedShortcuts.add(si);
2948 }
2949 }
2950 }
2951 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2952 }
2953
2954 if (!updatedShortcuts.isEmpty()) {
2955 final UserHandleCompat userFinal = user;
2956 mHandler.post(new Runnable() {
2957
2958 public void run() {
2959 Callbacks cb = getCallback();
2960 if (cb != null) {
2961 cb.bindShortcutsChanged(updatedShortcuts,
2962 new ArrayList<ShortcutInfo>(), userFinal);
2963 }
2964 }
2965 });
2966 }
2967 }
2968 }
2969 if (!updatedApps.isEmpty()) {
2970 mHandler.post(new Runnable() {
2971
2972 public void run() {
2973 Callbacks cb = getCallback();
2974 if (cb != null) {
2975 cb.bindAppsUpdated(updatedApps);
2976 }
2977 }
2978 });
2979 }
2980 }
2981
Joe Onoratobe386092009-11-17 17:32:16 -08002982 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002983 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002984 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2985 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2986 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2987 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2988 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2989 }
Joe Onorato36115782010-06-17 13:28:48 -04002990 }
2991 }
2992
2993 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002994 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
2996
Adam Cohen091440a2015-03-18 14:16:05 -07002997 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002998
2999 @Override
3000 public void onReceive(Context context, Intent intent) {
3001 synchronized (sBgLock) {
3002 final LauncherAppsCompat launcherApps = LauncherAppsCompat
3003 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07003004 final PackageManager manager = context.getPackageManager();
3005 final ArrayList<String> packagesRemoved = new ArrayList<String>();
3006 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003007 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
3008 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003009 packagesRemoved.clear();
3010 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003011 for (String pkg : entry.getValue()) {
3012 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003013 boolean packageOnSdcard = launcherApps.isAppEnabled(
3014 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
3015 if (packageOnSdcard) {
3016 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
3017 packagesUnavailable.add(pkg);
3018 } else {
3019 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
3020 packagesRemoved.add(pkg);
3021 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003022 }
3023 }
3024 if (!packagesRemoved.isEmpty()) {
3025 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
3026 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3027 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003028 if (!packagesUnavailable.isEmpty()) {
3029 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
3030 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3031 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003032 }
Sunny Goyal34942622014-08-29 17:20:55 -07003033 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003034 }
3035 }
3036 }
3037
Joe Onorato36115782010-06-17 13:28:48 -04003038 private class PackageUpdatedTask implements Runnable {
3039 int mOp;
3040 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003041 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003042
3043 public static final int OP_NONE = 0;
3044 public static final int OP_ADD = 1;
3045 public static final int OP_UPDATE = 2;
3046 public static final int OP_REMOVE = 3; // uninstlled
3047 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3048
3049
Kenny Guyed131872014-04-30 03:02:21 +01003050 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003051 mOp = op;
3052 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003053 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003054 }
3055
3056 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003057 if (!mHasLoaderCompletedOnce) {
3058 // Loader has not yet run.
3059 return;
3060 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003061 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003062
3063 final String[] packages = mPackages;
3064 final int N = packages.length;
3065 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003066 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003067 for (int i=0; i<N; i++) {
3068 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003069 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003070 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003071 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003072
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003073 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3074 if (heuristic != null) {
3075 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003076 }
Joe Onorato36115782010-06-17 13:28:48 -04003077 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003078 }
Joe Onorato36115782010-06-17 13:28:48 -04003079 case OP_UPDATE:
3080 for (int i=0; i<N; i++) {
3081 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003082 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003083 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003084 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003085 }
3086 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003087 case OP_REMOVE: {
3088 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3089 if (heuristic != null) {
3090 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003091 }
Joe Onorato36115782010-06-17 13:28:48 -04003092 for (int i=0; i<N; i++) {
3093 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003094 mIconCache.removeIconsForPkg(packages[i], mUser);
3095 }
3096 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003097 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003098 case OP_UNAVAILABLE:
3099 for (int i=0; i<N; i++) {
3100 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3101 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003102 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003103 }
3104 break;
3105 }
3106
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003107 ArrayList<AppInfo> added = null;
3108 ArrayList<AppInfo> modified = null;
3109 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003110
Adam Cohen487f7dd2012-06-28 18:12:10 -07003111 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003112 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003113 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003114 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003115 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003116 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003117 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003118 }
Winson Chung5d55f332012-07-16 20:45:03 -07003119 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003120 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003121 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003122 }
3123
Sunny Goyale0f58d72014-11-10 18:05:31 -08003124 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003125 if (callbacks == null) {
3126 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3127 return;
3128 }
3129
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3131 new HashMap<ComponentName, AppInfo>();
3132
Joe Onorato36115782010-06-17 13:28:48 -04003133 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003134 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 for (AppInfo ai : added) {
3136 addedOrUpdatedApps.put(ai.componentName, ai);
3137 }
Joe Onorato36115782010-06-17 13:28:48 -04003138 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003139
Joe Onorato36115782010-06-17 13:28:48 -04003140 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003141 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142 for (AppInfo ai : modified) {
3143 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003144 }
3145
Joe Onorato36115782010-06-17 13:28:48 -04003146 mHandler.post(new Runnable() {
3147 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003148 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003149 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003150 callbacks.bindAppsUpdated(modifiedFinal);
3151 }
3152 }
3153 });
3154 }
Winson Chung83892cc2013-05-01 16:53:33 -07003155
Sunny Goyal4390ace2014-10-13 11:33:11 -07003156 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003157 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003158 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3159 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3160 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3161
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003162 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003164 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003165 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3166 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167 boolean infoUpdated = false;
3168 boolean shortcutUpdated = false;
3169
3170 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003171 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003173 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3174 si.iconResource.resourceName, mIconCache, context);
3175 if (icon != null) {
3176 si.setIcon(icon);
3177 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003179 }
3180 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003181
3182 ComponentName cn = si.getTargetComponent();
3183 if (cn != null && packageSet.contains(cn.getPackageName())) {
3184 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3185
3186 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003187 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3188 // Auto install icon
3189 PackageManager pm = context.getPackageManager();
3190 ResolveInfo matched = pm.resolveActivity(
3191 new Intent(Intent.ACTION_MAIN)
3192 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3193 PackageManager.MATCH_DEFAULT_ONLY);
3194 if (matched == null) {
3195 // Try to find the best match activity.
3196 Intent intent = pm.getLaunchIntentForPackage(
3197 cn.getPackageName());
3198 if (intent != null) {
3199 cn = intent.getComponent();
3200 appInfo = addedOrUpdatedApps.get(cn);
3201 }
3202
3203 if ((intent == null) || (appInfo == null)) {
3204 removedShortcuts.add(si);
3205 continue;
3206 }
3207 si.promisedIntent = intent;
3208 }
3209 }
3210
3211 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003212 if (appInfo != null) {
3213 si.flags = appInfo.flags;
3214 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003215
Sunny Goyal756adbc2015-04-16 15:20:51 -07003216 si.intent = si.promisedIntent;
3217 si.promisedIntent = null;
3218 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 infoUpdated = true;
3220 si.updateIcon(mIconCache);
3221 }
3222
3223 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3224 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3225 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003226 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003227 si.contentDescription = appInfo.contentDescription;
3228 infoUpdated = true;
3229 }
3230
3231 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3232 // Since package was just updated, the target must be available now.
3233 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3234 shortcutUpdated = true;
3235 }
3236 }
3237
3238 if (infoUpdated || shortcutUpdated) {
3239 updatedShortcuts.add(si);
3240 }
3241 if (infoUpdated) {
3242 updateItemInDatabase(context, si);
3243 }
3244 } else if (info instanceof LauncherAppWidgetInfo) {
3245 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3246 if (mUser.equals(widgetInfo.user)
3247 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3248 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3249 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3250 widgets.add(widgetInfo);
3251 updateItemInDatabase(context, widgetInfo);
3252 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003253 }
3254 }
3255 }
3256
Sunny Goyal4390ace2014-10-13 11:33:11 -07003257 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3258 mHandler.post(new Runnable() {
3259
3260 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003261 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003262 if (callbacks == cb && cb != null) {
3263 callbacks.bindShortcutsChanged(
3264 updatedShortcuts, removedShortcuts, mUser);
3265 }
3266 }
3267 });
3268 if (!removedShortcuts.isEmpty()) {
3269 deleteItemsFromDatabase(context, removedShortcuts);
3270 }
3271 }
3272 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003273 mHandler.post(new Runnable() {
3274 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003275 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003276 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003277 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003278 }
3279 }
3280 });
3281 }
3282 }
3283
Winson Chungdf95eb12013-10-16 14:57:07 -07003284 final ArrayList<String> removedPackageNames =
3285 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003286 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003287 // Mark all packages in the broadcast to be removed
3288 removedPackageNames.addAll(Arrays.asList(packages));
3289 } else if (mOp == OP_UPDATE) {
3290 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003291 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003292 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003293 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003294 }
3295 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003296 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003297
Winson Chungdf95eb12013-10-16 14:57:07 -07003298 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003299 final int removeReason;
3300 if (mOp == OP_UNAVAILABLE) {
3301 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3302 } else {
3303 // Remove all the components associated with this package
3304 for (String pn : removedPackageNames) {
3305 deletePackageFromDatabase(context, pn, mUser);
3306 }
3307 // Remove all the specific components
3308 for (AppInfo a : removedApps) {
3309 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3310 deleteItemsFromDatabase(context, infos);
3311 }
3312 removeReason = 0;
3313 }
3314
Winson Chungdf95eb12013-10-16 14:57:07 -07003315 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003316 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003317 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003318 mHandler.post(new Runnable() {
3319 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003320 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003321 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003322 callbacks.bindComponentsRemoved(
3323 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003324 }
3325 }
3326 });
Joe Onoratobe386092009-11-17 17:32:16 -08003327 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003328
3329 // onProvidersChanged method (API >= 17) already refreshed the widget list
3330 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3331
Adam Cohen4caf2982013-08-20 18:54:31 -07003332 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003333 mHandler.post(new Runnable() {
3334 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003335 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003336 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003337 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003338 }
3339 }
3340 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 }
3342 }
3343
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003344 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3345 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003346 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003347 if (sBgWidgetProviders == null || refresh) {
3348 sBgWidgetProviders = new HashMap<>();
3349 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3350 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003351
Robin Lee26ace122015-03-16 19:41:43 +00003352 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3353 for (AppWidgetProviderInfo pInfo : widgets) {
3354 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3355 UserHandleCompat user = wm.getUser(info);
3356 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3357 }
3358
3359 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3360 for (CustomAppWidget widget : customWidgets) {
3361 info = new LauncherAppWidgetProviderInfo(context, widget);
3362 UserHandleCompat user = wm.getUser(info);
3363 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3364 }
Adam Cohen59400422014-03-05 18:07:04 -08003365 }
Adam Cohen59400422014-03-05 18:07:04 -08003366 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3367 }
3368 }
3369
Robin Lee26ace122015-03-16 19:41:43 +00003370 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3371 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003372 synchronized (sBgLock) {
3373 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003374 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003375 }
Robin Lee26ace122015-03-16 19:41:43 +00003376 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003377 }
3378 }
3379
Hyunyoung Song227239e2015-05-04 18:17:35 -07003380 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3381 final boolean refresh) {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003382 runOnWorkerThread(new Runnable(){
3383 @Override
3384 public void run() {
Hyunyoung Song227239e2015-05-04 18:17:35 -07003385 final ArrayList<Object> list = getWidgetsAndShortcuts(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003386 mHandler.post(new Runnable() {
3387 @Override
3388 public void run() {
3389 Callbacks cb = getCallback();
3390 if (callbacks == cb && cb != null) {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003391 callbacks.bindAllPackages(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003392 }
3393 }
3394 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003395 // update the Widget entries inside DB on the worker thread.
3396 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003397 }
3398 });
3399 }
3400
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003401 /**
3402 * Returns a list of ResolveInfos/AppWidgetInfos.
3403 *
3404 * @see #loadAndBindWidgetsAndShortcuts
3405 */
3406 private ArrayList<Object> getWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003407 PackageManager packageManager = context.getPackageManager();
3408 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003409 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003410 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3411 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Michael Jurkac402cd92013-05-20 15:49:32 +02003412 return widgetsAndShortcuts;
3413 }
3414
Adam Cohen091440a2015-03-18 14:16:05 -07003415 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003416 UserHandleCompat user) {
3417 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3418 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003419 }
Adam Cohen556f6132014-01-15 15:18:08 -08003420
Kenny Guyed131872014-04-30 03:02:21 +01003421 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3422 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003423 if (cn == null) {
3424 return false;
3425 }
Kenny Guyed131872014-04-30 03:02:21 +01003426 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3427 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003428 return false;
3429 }
Kenny Guyed131872014-04-30 03:02:21 +01003430 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003431 }
3432
Adam Cohena28b78e2014-05-20 17:03:04 -07003433 public static boolean isValidPackage(Context context, String packageName,
3434 UserHandleCompat user) {
3435 if (packageName == null) {
3436 return false;
3437 }
3438 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3439 return launcherApps.isPackageEnabledForProfile(packageName, user);
3440 }
3441
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003443 * Make an ShortcutInfo object for a restored application or shortcut item that points
3444 * to a package that is not yet installed on the system.
3445 */
Sunny Goyal34942622014-08-29 17:20:55 -07003446 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003447 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003448 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003449 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003450 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003451
3452 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3453 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3454 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003455 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003456 }
Sunny Goyal34942622014-08-29 17:20:55 -07003457 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3458 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003459 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003460 }
Sunny Goyal34942622014-08-29 17:20:55 -07003461 } else {
3462 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3463 }
3464
Winson Chung82b016c2015-05-08 17:00:10 -07003465 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003466 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003467 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003468 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003469 return info;
3470 }
3471
3472 /**
3473 * Make an Intent object for a restored application or shortcut item that points
3474 * to the market page for the item.
3475 */
Adam Cohen091440a2015-03-18 14:16:05 -07003476 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003477 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003478 return getMarketIntent(componentName.getPackageName());
3479 }
3480
3481 static Intent getMarketIntent(String packageName) {
3482 return new Intent(Intent.ACTION_VIEW)
3483 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003484 .scheme("market")
3485 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003486 .appendQueryParameter("id", packageName)
3487 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003488 }
3489
3490 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003491 * Make an ShortcutInfo object for a shortcut that is an application.
3492 *
3493 * If c is not null, then it will be used to fill in missing data like the title and icon.
3494 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003495 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003496 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003497 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003498 if (user == null) {
3499 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003500 return null;
3501 }
3502
Kenny Guyed131872014-04-30 03:02:21 +01003503 ComponentName componentName = intent.getComponent();
3504 if (componentName == null) {
3505 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3506 return null;
3507 }
3508
3509 Intent newIntent = new Intent(intent.getAction(), null);
3510 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3511 newIntent.setComponent(componentName);
3512 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003513 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003514 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3515 return null;
3516 }
3517
3518 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003519 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003520 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3521 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3522 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003523 }
3524
Joe Onorato56d82912010-03-07 14:32:10 -05003525 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003526 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003527 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003528 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003529
Joe Onorato56d82912010-03-07 14:32:10 -05003530 // fall back to the class name of the activity
3531 if (info.title == null) {
3532 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003533 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003534
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003536 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003537 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003538 if (lai != null) {
3539 info.flags = AppInfo.initFlags(lai);
3540 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003542 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003543
Sunny Goyale2df0622015-04-24 11:27:00 -07003544 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003545 ItemInfoFilter f) {
3546 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3547 for (ItemInfo i : infos) {
3548 if (i instanceof ShortcutInfo) {
3549 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003550 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003551 if (cn != null && f.filterItem(null, info, cn)) {
3552 filtered.add(info);
3553 }
3554 } else if (i instanceof FolderInfo) {
3555 FolderInfo info = (FolderInfo) i;
3556 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003557 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003558 if (cn != null && f.filterItem(info, s, cn)) {
3559 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003560 }
3561 }
Winson Chung64359a52013-07-08 17:17:08 -07003562 } else if (i instanceof LauncherAppWidgetInfo) {
3563 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3564 ComponentName cn = info.providerName;
3565 if (cn != null && f.filterItem(null, info, cn)) {
3566 filtered.add(info);
3567 }
Winson Chung8a435102012-08-30 17:16:53 -07003568 }
3569 }
Winson Chung64359a52013-07-08 17:17:08 -07003570 return new ArrayList<ItemInfo>(filtered);
3571 }
3572
Adam Cohen091440a2015-03-18 14:16:05 -07003573 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003574 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003575 ItemInfoFilter filter = new ItemInfoFilter() {
3576 @Override
3577 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003578 if (info.user == null) {
3579 return cn.equals(cname);
3580 } else {
3581 return cn.equals(cname) && info.user.equals(user);
3582 }
Winson Chung64359a52013-07-08 17:17:08 -07003583 }
3584 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003585 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003586 }
3587
Sunny Goyal1a745e82014-10-02 15:58:31 -07003588 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003589 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003590 */
Adam Cohen091440a2015-03-18 14:16:05 -07003591 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003592 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3593 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594
Joe Onorato56d82912010-03-07 14:32:10 -05003595 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003596 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003597 // Non-app shortcuts are only supported for current user.
3598 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003600
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003601 // TODO: If there's an explicit component and we can't install that, delete it.
3602
Winson Chung82b016c2015-05-08 17:00:10 -07003603 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003604
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 int iconType = c.getInt(iconTypeIndex);
3606 switch (iconType) {
3607 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3608 String packageName = c.getString(iconPackageIndex);
3609 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003610 info.customIcon = false;
3611 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003612 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003613 // the db
3614 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003615 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003616 }
3617 // the fallback icon
3618 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003619 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003620 info.usingFallbackIcon = true;
3621 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 break;
3623 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003624 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003625 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003626 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003627 info.customIcon = false;
3628 info.usingFallbackIcon = true;
3629 } else {
3630 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 break;
3633 default:
Kenny Guyed131872014-04-30 03:02:21 +01003634 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003635 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 info.customIcon = false;
3637 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003639 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 return info;
3641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003642
Sunny Goyal2350bc92014-10-14 16:42:54 -07003643 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003644 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3645 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3646 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3647
Adam Cohend9198822011-11-22 16:42:47 -08003648 if (intent == null) {
3649 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3650 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3651 return null;
3652 }
3653
Joe Onorato0589f0f2010-02-08 13:44:00 -08003654 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003655 boolean customIcon = false;
3656 ShortcutIconResource iconResource = null;
3657
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003658 if (bitmap instanceof Bitmap) {
3659 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003660 customIcon = true;
3661 } else {
3662 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003663 if (extra instanceof ShortcutIconResource) {
3664 iconResource = (ShortcutIconResource) extra;
3665 icon = Utilities.createIconBitmap(iconResource.packageName,
3666 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003667 }
3668 }
3669
Michael Jurkac9d95c52011-08-29 14:03:34 -07003670 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003671
Kenny Guyed131872014-04-30 03:02:21 +01003672 // Only support intents for current user for now. Intents sent from other
3673 // users wouldn't get here without intent forwarding anyway.
3674 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003675 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003676 icon = mIconCache.getDefaultIcon(info.user);
3677 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003678 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003679 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003680
Winson Chung82b016c2015-05-08 17:00:10 -07003681 info.title = Utilities.trim(name);
3682 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003683 info.intent = intent;
3684 info.customIcon = customIcon;
3685 info.iconResource = iconResource;
3686
3687 return info;
3688 }
3689
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003691 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 * or make a new one.
3693 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003694 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 // See if a placeholder was created for us already
3696 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003697 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003699 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 folders.put(id, folderInfo);
3701 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003702 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003703 }
3704
Winson Chung1ed747a2011-05-03 16:18:34 -07003705 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003706 private final AppWidgetManagerCompat mManager;
3707 private final PackageManager mPackageManager;
3708 private final HashMap<Object, String> mLabelCache;
3709 private final Collator mCollator;
3710
Hyunyoung Song3f471442015-04-08 19:01:34 -07003711 public WidgetAndShortcutNameComparator(Context context) {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003712 mManager = AppWidgetManagerCompat.getInstance(context);
3713 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003714 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003715 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003716 }
3717 public final int compare(Object a, Object b) {
3718 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003719 if (mLabelCache.containsKey(a)) {
3720 labelA = mLabelCache.get(a);
3721 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003722 labelA = (a instanceof LauncherAppWidgetProviderInfo)
Winson Chung82b016c2015-05-08 17:00:10 -07003723 ? Utilities.trim(mManager.loadLabel((LauncherAppWidgetProviderInfo) a))
3724 : Utilities.trim(((ResolveInfo) a).loadLabel(mPackageManager));
Winson Chungc3eecff2011-07-11 17:44:15 -07003725 mLabelCache.put(a, labelA);
3726 }
3727 if (mLabelCache.containsKey(b)) {
3728 labelB = mLabelCache.get(b);
3729 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003730 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Winson Chung82b016c2015-05-08 17:00:10 -07003731 ? Utilities.trim(mManager.loadLabel((LauncherAppWidgetProviderInfo) b))
3732 : Utilities.trim(((ResolveInfo) b).loadLabel(mPackageManager));
Winson Chungc3eecff2011-07-11 17:44:15 -07003733 mLabelCache.put(b, labelB);
3734 }
Winson Chung11904872012-09-17 16:58:46 -07003735 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003736 }
3737 };
Joe Onoratobe386092009-11-17 17:32:16 -08003738
Sunny Goyal651077b2014-06-30 14:15:31 -07003739 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3740 return (provider != null) && (provider.provider != null)
3741 && (provider.provider.getPackageName() != null);
3742 }
3743
Joe Onoratobe386092009-11-17 17:32:16 -08003744 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003745 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003746 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3747 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3748 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3749 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003750 if (mLoaderTask != null) {
3751 mLoaderTask.dumpState();
3752 } else {
3753 Log.d(TAG, "mLoaderTask=null");
3754 }
Joe Onoratobe386092009-11-17 17:32:16 -08003755 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003756
3757 public Callbacks getCallback() {
3758 return mCallbacks != null ? mCallbacks.get() : null;
3759 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003760
3761 /**
3762 * @return {@link FolderInfo} if its already loaded.
3763 */
3764 public FolderInfo findFolderById(Long folderId) {
3765 synchronized (sBgLock) {
3766 return sBgFolders.get(folderId);
3767 }
3768 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003769
3770 /**
3771 * @return the looper for the worker thread which can be used to start background tasks.
3772 */
3773 public static Looper getWorkerLooper() {
3774 return sWorkerThread.getLooper();
3775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003776}