blob: 7fdd523272ed79c0195cdbc6e25a8420d8a6d04f [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700134 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800135
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700136 // The lock that must be acquired before referencing any static bg data structures. Unlike
137 // other locks, this one can generally be held long-term because we never expect any of these
138 // static data structures to be referenced outside of the worker thread except on the first
139 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700140 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700141
Adam Cohen487f7dd2012-06-28 18:12:10 -0700142 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700144 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
147 // created by LauncherModel that are directly on the home screen (however, no widgets or
148 // shortcuts within folders).
149 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
152 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 new ArrayList<LauncherAppWidgetInfo>();
154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700156 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700157
Adam Cohendcd297f2013-06-18 13:13:40 -0700158 // sBgWorkspaceScreens is the ordered set of workspace screens.
159 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
160
Adam Cohen59400422014-03-05 18:07:04 -0800161 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000162 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800163
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700164 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700165 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
166 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700167
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700168 // </ only access in worker thread >
169
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Reena Lee99a73f32011-10-24 17:27:37 -0700172 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700173
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk final LauncherAppsCompat mLauncherApps;
175 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700178 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public int getCurrentWorkspaceScreen();
180 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700181 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
182 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700183 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700185 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800186 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700189 public void bindAppsAdded(ArrayList<Long> newScreens,
190 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700191 ArrayList<ItemInfo> addAnimated,
192 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700194 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
195 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
196 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700197 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700198 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700199 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song8821ca92015-05-04 16:28:20 -0700200 public void bindAllPackages(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100201 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700202 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700203 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700204 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Winson Chung64359a52013-07-08 17:17:08 -0700207 public interface ItemInfoFilter {
208 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
209 }
210
Bjorn Bringert1307f632013-10-03 22:31:03 +0100211 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800212 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400213
Winson Chungee055712013-07-30 14:46:24 -0700214 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700215 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400216 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
217 // resource string.
218 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
219 ProviderInfo providerInfo =
220 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
221 ProviderInfo redirectProvider =
222 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700223
224 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400225 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700226
227 if (mOldContentProviderExists) {
228 Log.d(TAG, "Old launcher provider exists.");
229 } else {
230 Log.d(TAG, "Old launcher provider does not exist.");
231 }
232
Daniel Sandlere4f98912013-06-25 15:13:26 -0400233 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100234 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 mIconCache = iconCache;
236
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400237 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700238 Configuration config = res.getConfiguration();
239 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100240 mLauncherApps = LauncherAppsCompat.getInstance(context);
241 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 }
243
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700244 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
245 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 if (sWorkerThread.getThreadId() == Process.myTid()) {
248 // If we are on the worker thread, post onto the main handler
249 mHandler.post(r);
250 } else {
251 r.run();
252 }
253 }
254
255 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
256 * posted on the worker thread handler. */
257 private static void runOnWorkerThread(Runnable r) {
258 if (sWorkerThread.getThreadId() == Process.myTid()) {
259 r.run();
260 } else {
261 // If we are not on the worker thread, then post to the worker handler
262 sWorker.post(r);
263 }
264 }
265
Winson Chunge43a1e72014-01-15 10:33:02 -0800266 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
267 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800268 }
269
Sunny Goyal756adbc2015-04-16 15:20:51 -0700270 public void setPackageState(final PackageInstallInfo installInfo) {
271 Runnable updateRunnable = new Runnable() {
272
273 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500274 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 synchronized (sBgLock) {
276 final HashSet<ItemInfo> updates = new HashSet<>();
277
278 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
279 // Ignore install success events as they are handled by Package add events.
280 return;
281 }
282
Sunny Goyale2df0622015-04-24 11:27:00 -0700283 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700284 if (info instanceof ShortcutInfo) {
285 ShortcutInfo si = (ShortcutInfo) info;
286 ComponentName cn = si.getTargetComponent();
287 if (si.isPromise() && (cn != null)
288 && installInfo.packageName.equals(cn.getPackageName())) {
289 si.setInstallProgress(installInfo.progress);
290
291 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
292 // Mark this info as broken.
293 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
294 }
295 updates.add(si);
296 }
297 }
298 }
299
300 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
301 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
302 widget.installProgress = installInfo.progress;
303 updates.add(widget);
304 }
305 }
306
307 if (!updates.isEmpty()) {
308 // Push changes to the callback.
309 Runnable r = new Runnable() {
310 public void run() {
311 Callbacks callbacks = getCallback();
312 if (callbacks != null) {
313 callbacks.bindRestoreItemsChange(updates);
314 }
315 }
316 };
317 mHandler.post(r);
318 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500319 }
320 }
321 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700322 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500323 }
324
Sunny Goyal756adbc2015-04-16 15:20:51 -0700325 /**
326 * Updates the icons and label of all pending icons for the provided package name.
327 */
328 public void updateSessionDisplayInfo(final String packageName) {
329 Runnable updateRunnable = new Runnable() {
330
331 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700332 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700333 synchronized (sBgLock) {
334 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
335 final UserHandleCompat user = UserHandleCompat.myUserHandle();
336
Sunny Goyale2df0622015-04-24 11:27:00 -0700337 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 if (info instanceof ShortcutInfo) {
339 ShortcutInfo si = (ShortcutInfo) info;
340 ComponentName cn = si.getTargetComponent();
341 if (si.isPromise() && (cn != null)
342 && packageName.equals(cn.getPackageName())) {
343 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
344 // For auto install apps update the icon as well as label.
345 mIconCache.getTitleAndIcon(si,
346 si.promisedIntent, user,
347 si.shouldUseLowResIcon());
348 } else {
349 // Only update the icon for restored apps.
350 si.updateIcon(mIconCache);
351 }
352 updates.add(si);
353 }
354 }
355 }
356
357 if (!updates.isEmpty()) {
358 // Push changes to the callback.
359 Runnable r = new Runnable() {
360 public void run() {
361 Callbacks callbacks = getCallback();
362 if (callbacks != null) {
363 callbacks.bindShortcutsChanged(updates,
364 new ArrayList<ShortcutInfo>(), user);
365 }
366 }
367 };
368 mHandler.post(r);
369 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700370 }
371 }
372 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700373 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700374 }
375
Adam Cohen76a47a12014-02-05 11:47:43 -0800376 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800377 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800378
379 if (allAppsApps == null) {
380 throw new RuntimeException("allAppsApps must not be null");
381 }
382 if (allAppsApps.isEmpty()) {
383 return;
384 }
385
386 // Process the newly added applications and add them to the database first
387 Runnable r = new Runnable() {
388 public void run() {
389 runOnMainThread(new Runnable() {
390 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800391 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800392 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500393 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800394 }
395 }
396 });
397 }
398 };
399 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700400 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800401
Sunny Goyala9116722015-04-29 13:55:58 -0700402 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 int[] xy, int spanX, int spanY) {
404 LauncherAppState app = LauncherAppState.getInstance();
405 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
406 final int xCount = (int) grid.numColumns;
407 final int yCount = (int) grid.numRows;
408 boolean[][] occupied = new boolean[xCount][yCount];
409 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700410 for (ItemInfo r : occupiedPos) {
411 int right = r.cellX + r.spanX;
412 int bottom = r.cellY + r.spanY;
413 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
414 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415 occupied[x][y] = true;
416 }
417 }
418 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800419 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700420 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 }
422
423 /**
424 * Find a position on the screen for the given size or adds a new screen.
425 * @return screenId and the coordinates for the item.
426 */
Adam Cohen091440a2015-03-18 14:16:05 -0700427 @Thunk static Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 ArrayList<Long> workspaceScreens,
430 ArrayList<Long> addedWorkspaceScreensFinal,
431 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700432 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433
Sunny Goyala9116722015-04-29 13:55:58 -0700434 // Use sBgItemsIdMap as all the items are already loaded.
435 // TODO: Throw exception is above condition is not met.
436 synchronized (sBgLock) {
437 for (ItemInfo info : sBgItemsIdMap) {
438 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
439 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
440 if (items == null) {
441 items = new ArrayList<>();
442 screenItems.put(info.screenId, items);
443 }
444 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 }
448
449 // Find appropriate space for the item.
450 long screenId = 0;
451 int[] cordinates = new int[2];
452 boolean found = false;
453
454 int screenCount = workspaceScreens.size();
455 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700456 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800457 if (preferredScreenIndex < screenCount) {
458 screenId = workspaceScreens.get(preferredScreenIndex);
459 found = findNextAvailableIconSpaceInScreen(
460 screenItems.get(screenId), cordinates, spanX, spanY);
461 }
462
463 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700464 // Search on any of the screens starting from the first screen.
465 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 screenId = workspaceScreens.get(screen);
467 if (findNextAvailableIconSpaceInScreen(
468 screenItems.get(screenId), cordinates, spanX, spanY)) {
469 // We found a space for it
470 found = true;
471 break;
472 }
473 }
474 }
475
476 if (!found) {
477 // Still no position found. Add a new screen to the end.
478 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
479
480 // Save the screen id for binding in the workspace
481 workspaceScreens.add(screenId);
482 addedWorkspaceScreensFinal.add(screenId);
483
484 // If we still can't find an empty space, then God help us all!!!
485 if (!findNextAvailableIconSpaceInScreen(
486 screenItems.get(screenId), cordinates, spanX, spanY)) {
487 throw new RuntimeException("Can't find space to add the item");
488 }
489 }
490 return Pair.create(screenId, cordinates);
491 }
492
493 /**
494 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800495 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700496 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala9116722015-04-29 13:55:58 -0700497 final ArrayList<ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800499 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700500 return;
Winson Chung997a9232013-07-24 15:33:46 -0700501 }
Winson Chung64359a52013-07-08 17:17:08 -0700502 // Process the newly added applications and add them to the database first
503 Runnable r = new Runnable() {
504 public void run() {
505 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
506 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
507
Winson Chung76828c82013-08-19 15:43:29 -0700508 // Get the list of workspace screens. We need to append to this list and
509 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
510 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800511 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700512 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800513 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700514 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700516 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700518 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800519 }
Winson Chung76828c82013-08-19 15:43:29 -0700520
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700522 Pair<Long, int[]> coords = findSpaceForItem(context,
523 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 1, 1);
525 long screenId = coords.first;
526 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700527
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700528 ItemInfo itemInfo;
529 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
530 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700533 } else {
534 throw new RuntimeException("Unexpected info type");
535 }
Winson Chung94d67682013-09-25 16:29:40 -0700536
Winson Chung64359a52013-07-08 17:17:08 -0700537 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700538 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700539 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700540 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700541 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700543 }
544 }
545
Winson Chung76828c82013-08-19 15:43:29 -0700546 // Update the workspace screens
547 updateWorkspaceScreenOrder(context, workspaceScreens);
548
Adam Cohen76a47a12014-02-05 11:47:43 -0800549 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700550 runOnMainThread(new Runnable() {
551 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800552 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700553 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700554 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
555 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700556 if (!addedShortcutsFinal.isEmpty()) {
557 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
558 long lastScreenId = info.screenId;
559 for (ItemInfo i : addedShortcutsFinal) {
560 if (i.screenId == lastScreenId) {
561 addAnimated.add(i);
562 } else {
563 addNotAnimated.add(i);
564 }
Winson Chung997a9232013-07-24 15:33:46 -0700565 }
566 }
Winson Chungd64d1762013-08-20 14:37:16 -0700567 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800568 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700569 }
Winson Chung64359a52013-07-08 17:17:08 -0700570 }
Winson Chung997a9232013-07-24 15:33:46 -0700571 });
572 }
Winson Chung64359a52013-07-08 17:17:08 -0700573 }
574 };
575 runOnWorkerThread(r);
576 }
577
Sunny Goyald33860f2015-04-23 16:02:20 -0700578 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700579 if (sWorkerThread.getThreadId() == Process.myTid()) {
580 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
581 "main thread");
582 }
583
584 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400585 synchronized (mDeferredBindRunnables) {
586 mDeferredBindRunnables.clear();
587 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700588
589 // Remove any queued UI runnables
590 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700591 // Unbind all the workspace items
592 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700593 }
594
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700595 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700596 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700597 // Ensure that we don't use the same workspace items data structure on the main thread
598 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700599 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700600 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700601 tmpItems.addAll(sBgWorkspaceItems);
602 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603 }
604 Runnable r = new Runnable() {
605 @Override
606 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700607 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700608 item.unbind();
609 }
610 }
611 };
612 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700613 }
614
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 /**
616 * Adds an item to the DB if it was not created previously, or move it to a new
617 * <container, screen, cellX, cellY>
618 */
619 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700620 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 if (item.container == ItemInfo.NO_ID) {
622 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700623 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 } else {
625 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700626 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628 }
629
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700630 static void checkItemInfoLocked(
631 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
632 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
633 if (modelItem != null && item != modelItem) {
634 // check all the data is consistent
635 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
636 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
637 ShortcutInfo shortcut = (ShortcutInfo) item;
638 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
639 modelShortcut.intent.filterEquals(shortcut.intent) &&
640 modelShortcut.id == shortcut.id &&
641 modelShortcut.itemType == shortcut.itemType &&
642 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700644 modelShortcut.cellX == shortcut.cellX &&
645 modelShortcut.cellY == shortcut.cellY &&
646 modelShortcut.spanX == shortcut.spanX &&
647 modelShortcut.spanY == shortcut.spanY &&
648 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
649 (modelShortcut.dropPos != null &&
650 shortcut.dropPos != null &&
651 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
652 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
653 // For all intents and purposes, this is the same object
654 return;
655 }
656 }
657
658 // the modelItem needs to match up perfectly with item if our model is
659 // to be consistent with the database-- for now, just require
660 // modelItem == item or the equality check above
661 String msg = "item: " + ((item != null) ? item.toString() : "null") +
662 "modelItem: " +
663 ((modelItem != null) ? modelItem.toString() : "null") +
664 "Error: ItemInfo passed to checkItemInfo doesn't match original";
665 RuntimeException e = new RuntimeException(msg);
666 if (stackTrace != null) {
667 e.setStackTrace(stackTrace);
668 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800669 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700670 }
671 }
672
Michael Jurka816474f2012-06-25 14:49:02 -0700673 static void checkItemInfo(final ItemInfo item) {
674 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
675 final long itemId = item.id;
676 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700677 public void run() {
678 synchronized (sBgLock) {
679 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700680 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 }
682 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700683 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700684 }
685
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
687 final ItemInfo item, final String callingFunction) {
688 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700689 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 final ContentResolver cr = context.getContentResolver();
691
Adam Cohen487f7dd2012-06-28 18:12:10 -0700692 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700693 Runnable r = new Runnable() {
694 public void run() {
695 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 updateItemArrays(item, itemId, stackTrace);
697 }
698 };
699 runOnWorkerThread(r);
700 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
703 final ArrayList<ItemInfo> items, final String callingFunction) {
704 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700705
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
707 Runnable r = new Runnable() {
708 public void run() {
709 ArrayList<ContentProviderOperation> ops =
710 new ArrayList<ContentProviderOperation>();
711 int count = items.size();
712 for (int i = 0; i < count; i++) {
713 ItemInfo item = items.get(i);
714 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700715 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700717
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
719 updateItemArrays(item, itemId, stackTrace);
720
721 }
722 try {
723 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
724 } catch (Exception e) {
725 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726 }
727 }
728 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700729 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700731
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
733 // Lock on mBgLock *after* the db operation
734 synchronized (sBgLock) {
735 checkItemInfoLocked(itemId, item, stackTrace);
736
737 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
738 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
739 // Item is in a folder, make sure this folder exists
740 if (!sBgFolders.containsKey(item.container)) {
741 // An items container is being set to a that of an item which is not in
742 // the list of Folders.
743 String msg = "item: " + item + " container being set to: " +
744 item.container + ", not in the list of folders";
745 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 }
747 }
748
749 // Items are added/removed from the corresponding FolderInfo elsewhere, such
750 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
751 // that are on the desktop, as appropriate
752 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800753 if (modelItem != null &&
754 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
755 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 switch (modelItem.itemType) {
757 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
758 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
759 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
760 if (!sBgWorkspaceItems.contains(modelItem)) {
761 sBgWorkspaceItems.add(modelItem);
762 }
763 break;
764 default:
765 break;
766 }
767 } else {
768 sBgWorkspaceItems.remove(modelItem);
769 }
770 }
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700775 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700776 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 item.cellX = cellX;
780 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 // We store hotseat items in canonical form which is this orientation invariant position
783 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790
791 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
794 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800795 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700799 }
800
801 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
803 * cellX, cellY have already been updated on the ItemInfos.
804 */
805 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
806 final long container, final int screen) {
807
808 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
809 int count = items.size();
810
811 for (int i = 0; i < count; i++) {
812 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 item.container = container;
814
815 // We store hotseat items in canonical form which is this orientation invariant position
816 // in the hotseat
817 if (context instanceof Launcher && screen < 0 &&
818 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 item.cellY);
821 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 }
824
825 final ContentValues values = new ContentValues();
826 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
827 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
828 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800829 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831
832 contentValues.add(values);
833 }
834 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
835 }
836
837 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800839 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700840 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700842 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800843 item.cellX = cellX;
844 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 item.spanX = spanX;
846 item.spanY = spanY;
847
848 // We store hotseat items in canonical form which is this orientation invariant position
849 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 }
Adam Cohend4844c32011-02-18 19:25:06 -0800856
Adam Cohend4844c32011-02-18 19:25:06 -0800857 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800858 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
860 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800861 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
863 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800865
Michael Jurka816474f2012-06-25 14:49:02 -0700866 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868
869 /**
870 * Update an item to the database in a specified container.
871 */
872 static void updateItemInDatabase(Context context, final ItemInfo item) {
873 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100874 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800876 }
877
878 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700879 * Returns true if the shortcuts already exists on the workspace. This must be called after
880 * the workspace has been loaded. We identify a shortcut by its intent.
881 * TODO: Throw exception is above condition is not met.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 */
Sunny Goyal756adbc2015-04-16 15:20:51 -0700883 @Thunk static boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700884 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700885 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700886 if (intent.getComponent() != null) {
887 // If component is not null, an intent with null package will produce
888 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700889 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700890 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700891 intentWithPkg = intent.toUri(0);
892 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700894 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
895 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 }
897 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = intent.toUri(0);
899 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700900 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700901 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700902
903 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700904 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700905 if (item instanceof ShortcutInfo) {
906 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700907 if (info.getIntent() != null && info.user.equals(user)) {
908 String s = info.getIntent().toUri(0);
909 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
910 return true;
911 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700912 }
913 }
914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700917 }
918
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 /**
920 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
921 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700922 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 final ContentResolver cr = context.getContentResolver();
924 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
925 "_id=? and (itemType=? or itemType=?)",
926 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700927 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 try {
930 if (c.moveToFirst()) {
931 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
932 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
933 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
934 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
935 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
936 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700937 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 FolderInfo folderInfo = null;
940 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700941 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
942 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700944 }
945
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 folderInfo.title = c.getString(titleIndex);
947 folderInfo.id = id;
948 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700950 folderInfo.cellX = c.getInt(cellXIndex);
951 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700952 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
954 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 } finally {
957 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700958 }
959
960 return null;
961 }
962
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 /**
964 * Add an item to the database in a specified container. Sets the container, screen, cellX and
965 * cellY fields of the item. Also assigns an ID to the item.
966 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700967 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700968 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 item.cellX = cellX;
971 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 // We store hotseat items in canonical form which is this orientation invariant position
973 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980
981 final ContentValues values = new ContentValues();
982 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100983 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984
Michael Jurka414300a2013-08-27 15:42:35 +0200985 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700986 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700987
Jason Monk8e19cf22014-03-20 15:06:57 -0400988 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700989 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700990 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700991 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700993 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700994 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400995 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700996 sBgItemsIdMap.put(item.id, item);
997 switch (item.itemType) {
998 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
999 sBgFolders.put(item.id, (FolderInfo) item);
1000 // Fall through
1001 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1002 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1003 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1004 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1005 sBgWorkspaceItems.add(item);
1006 } else {
1007 if (!sBgFolders.containsKey(item.container)) {
1008 // Adding an item to a folder that doesn't exist.
1009 String msg = "adding item: " + item + " to a folder that " +
1010 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001011 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001013 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 break;
1015 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1016 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1017 break;
1018 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001019 }
1020 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001023 }
1024
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001026 * Creates a new unique child id, for a given cell span across all layouts.
1027 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001028 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001029 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001030 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001031 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001032 }
1033
Sunny Goyal34942622014-08-29 17:20:55 -07001034 private static ArrayList<ItemInfo> getItemsByPackageName(
1035 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001036 ItemInfoFilter filter = new ItemInfoFilter() {
1037 @Override
1038 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1039 return cn.getPackageName().equals(pn) && info.user.equals(user);
1040 }
1041 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001042 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001043 }
1044
1045 /**
1046 * Removes all the items from the database corresponding to the specified package.
1047 */
1048 static void deletePackageFromDatabase(Context context, final String pn,
1049 final UserHandleCompat user) {
1050 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001051 }
1052
1053 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001054 * Removes the specified item from the database
1055 * @param context
1056 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001058 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001059 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1060 items.add(item);
1061 deleteItemsFromDatabase(context, items);
1062 }
1063
1064 /**
1065 * Removes the specified items from the database
1066 * @param context
1067 * @param item
1068 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001069 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001071 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001072 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001073 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001074 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001075 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001076
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001077 // Lock on mBgLock *after* the db operation
1078 synchronized (sBgLock) {
1079 switch (item.itemType) {
1080 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1081 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001082 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001083 if (info.container == item.id) {
1084 // We are deleting a folder which still contains items that
1085 // think they are contained by that folder.
1086 String msg = "deleting a folder (" + item + ") which still " +
1087 "contains items (" + info + ")";
1088 Log.e(TAG, msg);
1089 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001090 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001091 sBgWorkspaceItems.remove(item);
1092 break;
1093 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1094 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1095 sBgWorkspaceItems.remove(item);
1096 break;
1097 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1098 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1099 break;
1100 }
1101 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001103 }
1104 }
Michael Jurka83df1882011-08-31 20:59:26 -07001105 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 }
1108
1109 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 * Update the order of the workspace screens in the database. The array list contains
1111 * a list of screen ids in the order that they should appear.
1112 */
Winson Chungc9168342013-06-26 14:54:55 -07001113 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001114 // Log to disk
1115 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1116 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1117
Winson Chung64359a52013-07-08 17:17:08 -07001118 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001119 final ContentResolver cr = context.getContentResolver();
1120 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1121
1122 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001123 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 while (iter.hasNext()) {
1125 long id = iter.next();
1126 if (id < 0) {
1127 iter.remove();
1128 }
1129 }
1130
1131 Runnable r = new Runnable() {
1132 @Override
1133 public void run() {
Yura085c8532014-02-11 15:15:29 +00001134 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001136 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001137 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 for (int i = 0; i < count; i++) {
1139 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001140 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001141 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1142 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001143 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001144 }
Yura085c8532014-02-11 15:15:29 +00001145
1146 try {
1147 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1148 } catch (Exception ex) {
1149 throw new RuntimeException(ex);
1150 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001151
Winson Chungba9c37f2013-08-30 14:11:37 -07001152 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001153 sBgWorkspaceScreens.clear();
1154 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001155 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001156 }
1157 };
1158 runOnWorkerThread(r);
1159 }
1160
1161 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 * Remove the contents of the specified folder from the database
1163 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001164 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 final ContentResolver cr = context.getContentResolver();
1166
Michael Jurkac9d95c52011-08-29 14:03:34 -07001167 Runnable r = new Runnable() {
1168 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001169 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001170 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001171 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001172 sBgItemsIdMap.remove(info.id);
1173 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 sBgWorkspaceItems.remove(info);
1175 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001176
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001177 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001178 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001180 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181 for (ItemInfo childInfo : info.contents) {
1182 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001183 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001184 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001185 }
1186 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 }
1189
1190 /**
1191 * Set this as the current Launcher activity object for the loader.
1192 */
1193 public void initialize(Callbacks callbacks) {
1194 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001195 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1196 // workspace to prevent leaking Launcher activities on orientation change.
1197 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 mCallbacks = new WeakReference<Callbacks>(callbacks);
1199 }
1200 }
1201
Kenny Guyed131872014-04-30 03:02:21 +01001202 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001203 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001204 int op = PackageUpdatedTask.OP_UPDATE;
1205 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1206 user));
1207 }
1208
1209 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001210 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001211 int op = PackageUpdatedTask.OP_REMOVE;
1212 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1213 user));
1214 }
1215
1216 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001217 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001218 int op = PackageUpdatedTask.OP_ADD;
1219 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1220 user));
1221 }
1222
1223 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001224 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001225 boolean replacing) {
1226 if (!replacing) {
1227 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1228 user));
1229 if (mAppsCanBeOnRemoveableStorage) {
1230 // Only rebind if we support removable storage. It catches the
1231 // case where
1232 // apps on the external sd card need to be reloaded
1233 startLoaderFromBackground();
1234 }
1235 } else {
1236 // If we are replacing then just update the packages in the list
1237 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1238 packageNames, user));
1239 }
1240 }
1241
1242 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001243 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001244 boolean replacing) {
1245 if (!replacing) {
1246 enqueuePackageUpdated(new PackageUpdatedTask(
1247 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1248 user));
1249 }
Kenny Guyed131872014-04-30 03:02:21 +01001250 }
1251
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001252 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1254 * ACTION_PACKAGE_CHANGED.
1255 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001256 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001257 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001258 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001259
Joe Onorato36115782010-06-17 13:28:48 -04001260 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001261 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001262 // If we have changed locale we need to clear out the labels in all apps/workspace.
1263 forceReload();
1264 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1265 // Check if configuration change was an mcc/mnc change which would affect app resources
1266 // and we would need to clear out the labels in all apps/workspace. Same handling as
1267 // above for ACTION_LOCALE_CHANGED
1268 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001269 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001270 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001271 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001272 forceReload();
1273 }
1274 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001275 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001276 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1277 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001278 Callbacks callbacks = getCallback();
1279 if (callbacks != null) {
1280 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001281 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001282 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1283 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1284 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001285 }
1286 }
1287
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001288 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001289 resetLoadedState(true, true);
1290
Reena Lee93f824a2011-09-23 17:20:28 -07001291 // Do this here because if the launcher activity is running it will be restarted.
1292 // If it's not running startLoaderFromBackground will merely tell it that it needs
1293 // to reload.
1294 startLoaderFromBackground();
1295 }
1296
Winson Chungf0c6ae02012-03-21 16:10:31 -07001297 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1298 synchronized (mLock) {
1299 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1300 // mWorkspaceLoaded to true later
1301 stopLoaderLocked();
1302 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1303 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1304 }
1305 }
1306
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001307 /**
1308 * When the launcher is in the background, it's possible for it to miss paired
1309 * configuration changes. So whenever we trigger the loader from the background
1310 * tell the launcher that it needs to re-run the loader when it comes back instead
1311 * of doing it now.
1312 */
1313 public void startLoaderFromBackground() {
1314 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001315 Callbacks callbacks = getCallback();
1316 if (callbacks != null) {
1317 // Only actually run the loader if they're not paused.
1318 if (!callbacks.setLoadOnResume()) {
1319 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001320 }
1321 }
1322 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001323 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001326
Reena Lee93f824a2011-09-23 17:20:28 -07001327 // If there is already a loader task running, tell it to stop.
1328 // returns true if isLaunching() was true on the old task
1329 private boolean stopLoaderLocked() {
1330 boolean isLaunching = false;
1331 LoaderTask oldTask = mLoaderTask;
1332 if (oldTask != null) {
1333 if (oldTask.isLaunching()) {
1334 isLaunching = true;
1335 }
1336 oldTask.stopLocked();
1337 }
1338 return isLaunching;
1339 }
1340
Adam Cohen1a85c582014-09-30 09:48:49 -07001341 public boolean isCurrentCallbacks(Callbacks callbacks) {
1342 return (mCallbacks != null && mCallbacks.get() == callbacks);
1343 }
1344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001346 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1347 }
1348
1349 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001350 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1351 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001352 synchronized (mLock) {
1353 if (DEBUG_LOADERS) {
1354 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001357 // Clear any deferred bind-runnables from the synchronized load process
1358 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001359 synchronized (mDeferredBindRunnables) {
1360 mDeferredBindRunnables.clear();
1361 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362
Joe Onorato36115782010-06-17 13:28:48 -04001363 // Don't bother to start the thread if we know it's not going to do anything
1364 if (mCallbacks != null && mCallbacks.get() != null) {
1365 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001366 // also, don't downgrade isLaunching if we're already running
1367 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001368 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001369 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1370 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1372 } else {
1373 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1374 sWorker.post(mLoaderTask);
1375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378 }
1379
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 void bindRemainingSynchronousPages() {
1381 // Post the remaining side pages to be loaded
1382 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001383 Runnable[] deferredBindRunnables = null;
1384 synchronized (mDeferredBindRunnables) {
1385 deferredBindRunnables = mDeferredBindRunnables.toArray(
1386 new Runnable[mDeferredBindRunnables.size()]);
1387 mDeferredBindRunnables.clear();
1388 }
1389 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001390 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 }
1393 }
1394
Joe Onorato36115782010-06-17 13:28:48 -04001395 public void stopLoader() {
1396 synchronized (mLock) {
1397 if (mLoaderTask != null) {
1398 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001399 }
1400 }
Joe Onorato36115782010-06-17 13:28:48 -04001401 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001403 /**
1404 * Loads the workspace screen ids in an ordered list.
1405 */
Adam Cohen091440a2015-03-18 14:16:05 -07001406 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001407 final ContentResolver contentResolver = context.getContentResolver();
1408 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001409
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001410 // Get screens ordered by rank.
1411 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1412 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1413 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001414 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001415 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001416 while (sc.moveToNext()) {
1417 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001418 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001419 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001420 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1421 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001422 }
1423 }
1424 } finally {
1425 sc.close();
1426 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001427 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001428 }
1429
Michael Jurkac57b7a82011-08-09 22:02:20 -07001430 public boolean isAllAppsLoaded() {
1431 return mAllAppsLoaded;
1432 }
1433
Winson Chung36a62fe2012-05-06 18:04:42 -07001434 boolean isLoadingWorkspace() {
1435 synchronized (mLock) {
1436 if (mLoaderTask != null) {
1437 return mLoaderTask.isLoadingWorkspace();
1438 }
1439 }
1440 return false;
1441 }
1442
Joe Onorato36115782010-06-17 13:28:48 -04001443 /**
1444 * Runnable for the thread that loads the contents of the launcher:
1445 * - workspace icons
1446 * - widgets
1447 * - all apps icons
1448 */
1449 private class LoaderTask implements Runnable {
1450 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001451 private boolean mIsLaunching;
Adam Cohen091440a2015-03-18 14:16:05 -07001452 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001453 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001454 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001455 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456
Dan Sandlerd5024042014-01-09 15:01:33 -05001457 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001458 mContext = context;
1459 mIsLaunching = isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001460 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001461 }
1462
Joe Onorato36115782010-06-17 13:28:48 -04001463 boolean isLaunching() {
1464 return mIsLaunching;
1465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466
Winson Chung36a62fe2012-05-06 18:04:42 -07001467 boolean isLoadingWorkspace() {
1468 return mIsLoadingAndBindingWorkspace;
1469 }
1470
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001471 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001472 mIsLoadingAndBindingWorkspace = true;
1473
Joe Onorato36115782010-06-17 13:28:48 -04001474 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001475 if (DEBUG_LOADERS) {
1476 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001477 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001478
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001479 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001480 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001481 synchronized (LoaderTask.this) {
1482 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001483 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001484 }
1485 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 }
1487 }
1488
Joe Onorato36115782010-06-17 13:28:48 -04001489 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001490 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001492
Joe Onorato36115782010-06-17 13:28:48 -04001493 private void waitForIdle() {
1494 // Wait until the either we're stopped or the other threads are done.
1495 // This way we don't start loading all apps until the workspace has settled
1496 // down.
1497 synchronized (LoaderTask.this) {
1498 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001499
Joe Onorato36115782010-06-17 13:28:48 -04001500 mHandler.postIdle(new Runnable() {
1501 public void run() {
1502 synchronized (LoaderTask.this) {
1503 mLoadAndBindStepFinished = true;
1504 if (DEBUG_LOADERS) {
1505 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001506 }
Joe Onorato36115782010-06-17 13:28:48 -04001507 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001508 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510 });
1511
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001512 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001513 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001514 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1515 // wait no longer than 1sec at a time
1516 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001517 } catch (InterruptedException ex) {
1518 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001519 }
1520 }
Joe Onorato36115782010-06-17 13:28:48 -04001521 if (DEBUG_LOADERS) {
1522 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001523 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001524 + "ms for previous step to finish binding");
1525 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001526 }
Joe Onorato36115782010-06-17 13:28:48 -04001527 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001528
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001530 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531 // Ensure that we have a valid page index to load synchronously
1532 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1533 "valid page index");
1534 }
1535 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1536 // Ensure that we don't try and bind a specified page when the pages have not been
1537 // loaded already (we should load everything asynchronously in that case)
1538 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1539 }
1540 synchronized (mLock) {
1541 if (mIsLoaderTaskRunning) {
1542 // Ensure that we are never running the background loading at this point since
1543 // we also touch the background collections
1544 throw new RuntimeException("Error! Background loading is already running");
1545 }
1546 }
1547
1548 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1549 // data structures, we can't allow any other thread to touch that data, but because
1550 // this call is synchronous, we can get away with not locking).
1551
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001552 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001553 // operations from the previous activity. We need to ensure that all queued operations
1554 // are executed before any synchronous binding work is done.
1555 mHandler.flush();
1556
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001557 // Divide the set of loaded items into those that we are binding synchronously, and
1558 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001559 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001560 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1561 // arise from that.
1562 onlyBindAllApps();
1563 }
1564
Joe Onorato36115782010-06-17 13:28:48 -04001565 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001566 synchronized (mLock) {
1567 mIsLoaderTaskRunning = true;
1568 }
Joe Onorato36115782010-06-17 13:28:48 -04001569 // Optimize for end-user experience: if the Launcher is up and // running with the
1570 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1571 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001572 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001573 // Elevate priority when Home launches for the first time to avoid
1574 // starving at boot time. Staring at a blank home is not cool.
1575 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001576 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1577 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001578 android.os.Process.setThreadPriority(mIsLaunching
1579 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1580 }
Winson Chung64359a52013-07-08 17:17:08 -07001581 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001582 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001583
Joe Onorato36115782010-06-17 13:28:48 -04001584 if (mStopped) {
1585 break keep_running;
1586 }
1587
1588 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1589 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001590 synchronized (mLock) {
1591 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001592 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001593 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1594 }
1595 }
Joe Onorato36115782010-06-17 13:28:48 -04001596 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001597
1598 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001599 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1600 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001601
1602 // Restore the default thread priority after we are done loading items
1603 synchronized (mLock) {
1604 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1605 }
Joe Onorato36115782010-06-17 13:28:48 -04001606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607
Joe Onorato36115782010-06-17 13:28:48 -04001608 // Clear out this reference, otherwise we end up holding it until all of the
1609 // callback runnables are done.
1610 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611
Joe Onorato36115782010-06-17 13:28:48 -04001612 synchronized (mLock) {
1613 // If we are still the last one to be scheduled, remove ourselves.
1614 if (mLoaderTask == this) {
1615 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001618 }
Joe Onorato36115782010-06-17 13:28:48 -04001619 }
1620
1621 public void stopLocked() {
1622 synchronized (LoaderTask.this) {
1623 mStopped = true;
1624 this.notify();
1625 }
1626 }
1627
1628 /**
1629 * Gets the callbacks object. If we've been stopped, or if the launcher object
1630 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1631 * object that was around when the deferred message was scheduled, and if there's
1632 * a new Callbacks object around then also return null. This will save us from
1633 * calling onto it with data that will be ignored.
1634 */
1635 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1636 synchronized (mLock) {
1637 if (mStopped) {
1638 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001639 }
Joe Onorato36115782010-06-17 13:28:48 -04001640
1641 if (mCallbacks == null) {
1642 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001643 }
Joe Onorato36115782010-06-17 13:28:48 -04001644
1645 final Callbacks callbacks = mCallbacks.get();
1646 if (callbacks != oldCallbacks) {
1647 return null;
1648 }
1649 if (callbacks == null) {
1650 Log.w(TAG, "no mCallbacks");
1651 return null;
1652 }
1653
1654 return callbacks;
1655 }
1656 }
1657
1658 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001659 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001660 LauncherAppState app = LauncherAppState.getInstance();
1661 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001662 final int countX = (int) grid.numColumns;
1663 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001664
Adam Cohendcd297f2013-06-18 13:13:40 -07001665 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001666 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001667 // Return early if we detect that an item is under the hotseat button
1668 if (mCallbacks == null ||
1669 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001670 Log.e(TAG, "Error loading shortcut into hotseat " + item
1671 + " into position (" + item.screenId + ":" + item.cellX + ","
1672 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001673 return false;
1674 }
1675
Dan Sandler295ae182013-12-10 16:05:47 -05001676 final ItemInfo[][] hotseatItems =
1677 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1678
Adam Cohenae4409d2013-11-26 10:34:59 -08001679 if (item.screenId >= grid.numHotseatIcons) {
1680 Log.e(TAG, "Error loading shortcut " + item
1681 + " into hotseat position " + item.screenId
1682 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1683 + ")");
1684 return false;
1685 }
1686
Dan Sandler295ae182013-12-10 16:05:47 -05001687 if (hotseatItems != null) {
1688 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001689 Log.e(TAG, "Error loading shortcut into hotseat " + item
1690 + " into position (" + item.screenId + ":" + item.cellX + ","
1691 + item.cellY + ") occupied by "
1692 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1693 [(int) item.screenId][0]);
1694 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001695 } else {
1696 hotseatItems[(int) item.screenId][0] = item;
1697 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001698 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001699 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001700 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001701 items[(int) item.screenId][0] = item;
1702 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001703 return true;
1704 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001705 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1706 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001707 return true;
1708 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001709
Adam Cohendcd297f2013-06-18 13:13:40 -07001710 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001711 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001712 occupied.put(item.screenId, items);
1713 }
1714
Dan Sandler295ae182013-12-10 16:05:47 -05001715 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001716 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1717 item.cellX < 0 || item.cellY < 0 ||
1718 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1719 Log.e(TAG, "Error loading shortcut " + item
1720 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1721 + item.cellX + "," + item.cellY
1722 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1723 return false;
1724 }
1725
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001726 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001727 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1728 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001729 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001730 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001731 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001732 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001733 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001734 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001735 return false;
1736 }
1737 }
1738 }
1739 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1740 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001741 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001742 }
1743 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001744
Joe Onorato36115782010-06-17 13:28:48 -04001745 return true;
1746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747
Winson Chungba9c37f2013-08-30 14:11:37 -07001748 /** Clears all the sBg data structures */
1749 private void clearSBgDataStructures() {
1750 synchronized (sBgLock) {
1751 sBgWorkspaceItems.clear();
1752 sBgAppWidgets.clear();
1753 sBgFolders.clear();
1754 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001755 sBgWorkspaceScreens.clear();
1756 }
1757 }
1758
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001759 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001760 // Log to disk
1761 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1762
Joe Onorato36115782010-06-17 13:28:48 -04001763 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764
Joe Onorato36115782010-06-17 13:28:48 -04001765 final Context context = mContext;
1766 final ContentResolver contentResolver = context.getContentResolver();
1767 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001768 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001769 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1770 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001771 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001772
Winson Chung892c74d2013-08-22 16:15:50 -07001773 LauncherAppState app = LauncherAppState.getInstance();
1774 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1775 int countX = (int) grid.numColumns;
1776 int countY = (int) grid.numRows;
1777
Dan Sandlerd5024042014-01-09 15:01:33 -05001778 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1779 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1780 LauncherAppState.getLauncherProvider().deleteDatabase();
1781 }
1782
1783 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1784 // append the user's Launcher2 shortcuts
1785 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1786 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1787 } else {
1788 // Make sure the default workspace is loaded
1789 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001790 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001791 }
Adam Cohene25af792013-06-06 23:08:25 -07001792
Winson Chung2abf94d2012-07-18 18:16:38 -07001793 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001794 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001795 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001796 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001797
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001799 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001800 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001801 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001802 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001803
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001804 // +1 for the hotseat (it can be larger than the workspace)
1805 // Load workspace in reverse order to ensure that latest items are loaded first (and
1806 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001807 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 try {
1810 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1811 final int intentIndex = c.getColumnIndexOrThrow
1812 (LauncherSettings.Favorites.INTENT);
1813 final int titleIndex = c.getColumnIndexOrThrow
1814 (LauncherSettings.Favorites.TITLE);
1815 final int iconTypeIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.ICON_TYPE);
1817 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1818 final int iconPackageIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.ICON_PACKAGE);
1820 final int iconResourceIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.ICON_RESOURCE);
1822 final int containerIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.CONTAINER);
1824 final int itemTypeIndex = c.getColumnIndexOrThrow(
1825 LauncherSettings.Favorites.ITEM_TYPE);
1826 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1827 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001828 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1829 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 final int screenIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.SCREEN);
1832 final int cellXIndex = c.getColumnIndexOrThrow
1833 (LauncherSettings.Favorites.CELLX);
1834 final int cellYIndex = c.getColumnIndexOrThrow
1835 (LauncherSettings.Favorites.CELLY);
1836 final int spanXIndex = c.getColumnIndexOrThrow
1837 (LauncherSettings.Favorites.SPANX);
1838 final int spanYIndex = c.getColumnIndexOrThrow(
1839 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001840 final int rankIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001842 final int restoredIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001844 final int profileIdIndex = c.getColumnIndexOrThrow(
1845 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001846 final int optionsIndex = c.getColumnIndexOrThrow(
1847 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Sunny Goyal7f834d22015-04-21 10:10:23 -07001849 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1850 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1851 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1852 }
1853
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 ShortcutInfo info;
1855 String intentDescription;
1856 LauncherAppWidgetInfo appWidgetInfo;
1857 int container;
1858 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001859 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001861 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 while (!mStopped && c.moveToNext()) {
1864 try {
1865 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001866 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001867 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 switch (itemType) {
1870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001872 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001874 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001875 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001876 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001877 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001878 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001879 if (user == null) {
1880 // User has been deleted remove the item.
1881 itemsToRemove.add(id);
1882 continue;
1883 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 try {
1885 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001886 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001887 if (cn != null && cn.getPackageName() != null) {
1888 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1889 cn.getPackageName(), user);
1890 boolean validComponent = validPkg &&
1891 launcherApps.isActivityEnabledForProfile(cn, user);
1892
1893 if (validComponent) {
1894 if (restored) {
1895 // no special handling necessary for this item
1896 restoredRows.add(id);
1897 restored = false;
1898 }
1899 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001900 intent = null;
1901 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1902 // We allow auto install apps to have their intent
1903 // updated after an install.
1904 intent = manager.getLaunchIntentForPackage(
1905 cn.getPackageName());
1906 if (intent != null) {
1907 ContentValues values = new ContentValues();
1908 values.put(LauncherSettings.Favorites.INTENT,
1909 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001910 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001911 }
1912 }
1913
1914 if (intent == null) {
1915 // The app is installed but the component is no
1916 // longer available.
1917 Launcher.addDumpLog(TAG,
1918 "Invalid component removed: " + cn, true);
1919 itemsToRemove.add(id);
1920 continue;
1921 } else {
1922 // no special handling necessary for this item
1923 restoredRows.add(id);
1924 restored = false;
1925 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001926 } else if (restored) {
1927 // Package is not yet available but might be
1928 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001929 Launcher.addDumpLog(TAG,
1930 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001931
1932 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1933 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001934 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001935 // App restore has started. Update the flag
1936 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1937 ContentValues values = new ContentValues();
1938 values.put(LauncherSettings.Favorites.RESTORED,
1939 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001940 updateItem(id, values);
1941 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1942 // This is a common app. Try to replace this.
1943 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1944 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1945 if (parser.findDefaultApp()) {
1946 // Default app found. Replace it.
1947 intent = parser.parsedIntent;
1948 cn = intent.getComponent();
1949 ContentValues values = parser.parsedValues;
1950 values.put(LauncherSettings.Favorites.RESTORED, 0);
1951 updateItem(id, values);
1952 restored = false;
1953 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001954
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001955 } else if (REMOVE_UNRESTORED_ICONS) {
1956 Launcher.addDumpLog(TAG,
1957 "Unrestored package removed: " + cn, true);
1958 itemsToRemove.add(id);
1959 continue;
1960 }
Sunny Goyal94485362014-09-18 16:13:58 -07001961 } else if (REMOVE_UNRESTORED_ICONS) {
1962 Launcher.addDumpLog(TAG,
1963 "Unrestored package removed: " + cn, true);
1964 itemsToRemove.add(id);
1965 continue;
1966 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001967 } else if (launcherApps.isAppEnabled(
1968 manager, cn.getPackageName(),
1969 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1970 // Package is present but not available.
1971 allowMissingTarget = true;
1972 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1973 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001974 // SdCard is not ready yet. Package might get available,
1975 // once it is ready.
1976 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1977 + " (check again later)", true);
1978 HashSet<String> pkgs = sPendingPackages.get(user);
1979 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001980 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001981 sPendingPackages.put(user, pkgs);
1982 }
1983 pkgs.add(cn.getPackageName());
1984 allowMissingTarget = true;
1985 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001986
1987 } else {
1988 // Do not wait for external media load anymore.
1989 // Log the invalid package, and remove it
1990 Launcher.addDumpLog(TAG,
1991 "Invalid package removed: " + cn, true);
1992 itemsToRemove.add(id);
1993 continue;
Winson Chungee055712013-07-30 14:46:24 -07001994 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001995 } else if (cn == null) {
1996 // For shortcuts with no component, keep them as they are
1997 restoredRows.add(id);
1998 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001999 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002001 Launcher.addDumpLog(TAG,
2002 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 continue;
2004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005
Sunny Goyal34b65272015-03-11 16:56:52 -07002006 container = c.getInt(containerIndex);
2007 boolean useLowResIcon = container >= 0 &&
2008 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2009
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002010 if (itemReplaced) {
2011 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002012 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002013 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002014 } else {
2015 // Don't replace items for other profiles.
2016 itemsToRemove.add(id);
2017 continue;
2018 }
2019 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002020 if (user.equals(UserHandleCompat.myUserHandle())) {
2021 Launcher.addDumpLog(TAG,
2022 "constructing info for partially restored package",
2023 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002024 info = getRestoredItemInfo(c, titleIndex, intent,
2025 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002026 intent = getRestoredItemIntent(c, context, intent);
2027 } else {
2028 // Don't restore items for other profiles.
2029 itemsToRemove.add(id);
2030 continue;
2031 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002032 } else if (itemType ==
2033 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002034 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002035 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 } else {
2037 info = getShortcutInfo(c, context, iconTypeIndex,
2038 iconPackageIndex, iconResourceIndex, iconIndex,
2039 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002040
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 // App shortcuts that used to be automatically added to Launcher
2042 // didn't always have the correct intent flags set, so do that
2043 // here
2044 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002045 intent.getCategories() != null &&
2046 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002047 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 intent.addFlags(
2049 Intent.FLAG_ACTIVITY_NEW_TASK |
2050 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2051 }
Michael Jurka96879562012-03-22 05:54:33 -07002052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002055 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002058 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 info.cellX = c.getInt(cellXIndex);
2060 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002061 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002062 info.spanX = 1;
2063 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002064 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002065 info.isDisabled = disabledState;
2066 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2067 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2068 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002069
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002071 if (!checkItemPlacement(occupied, info)) {
2072 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 break;
2074 }
2075
Sunny Goyal756adbc2015-04-16 15:20:51 -07002076 if (restored) {
2077 ComponentName cn = info.getTargetComponent();
2078 if (cn != null) {
2079 Integer progress = installingPkgs.get(cn.getPackageName());
2080 if (progress != null) {
2081 info.setInstallProgress(progress);
2082 } else {
2083 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2084 }
2085 }
2086 }
2087
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 switch (container) {
2089 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2090 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2091 sBgWorkspaceItems.add(info);
2092 break;
2093 default:
2094 // Item is in a user folder
2095 FolderInfo folderInfo =
2096 findOrMakeFolder(sBgFolders, container);
2097 folderInfo.add(info);
2098 break;
2099 }
2100 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002101 } else {
2102 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 }
2104 break;
2105
2106 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2107 id = c.getLong(idIndex);
2108 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2109
2110 folderInfo.title = c.getString(titleIndex);
2111 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002112 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 folderInfo.cellX = c.getInt(cellXIndex);
2116 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002117 folderInfo.spanX = 1;
2118 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002119 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120
Daniel Sandler8802e962010-05-26 16:28:16 -04002121 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002122 if (!checkItemPlacement(occupied, folderInfo)) {
2123 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002124 break;
2125 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002126
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2129 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2130 sBgWorkspaceItems.add(folderInfo);
2131 break;
Joe Onorato36115782010-06-17 13:28:48 -04002132 }
Joe Onorato17a89222011-02-08 17:26:11 -08002133
Chris Wrenf4d08112014-01-16 18:13:56 -05002134 if (restored) {
2135 // no special handling required for restored folders
2136 restoredRows.add(id);
2137 }
2138
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2140 sBgFolders.put(folderInfo.id, folderInfo);
2141 break;
2142
2143 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002144 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002146 boolean customWidget = itemType ==
2147 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2148
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002150 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002151 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002152 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002153 user = allUsers.get(serialNumber);
2154 if (user == null) {
2155 itemsToRemove.add(id);
2156 continue;
2157 }
2158
Sunny Goyalff572272014-07-23 13:58:07 -07002159 final ComponentName component =
2160 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002161
Sunny Goyal651077b2014-06-30 14:15:31 -07002162 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002163 final boolean isIdValid = (restoreStatus &
2164 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002165 final boolean wasProviderReady = (restoreStatus &
2166 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002167
Adam Cohen59400422014-03-05 18:07:04 -08002168 final LauncherAppWidgetProviderInfo provider =
2169 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002170 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002171 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002172
2173 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002174 if (!isSafeMode && !customWidget &&
2175 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002177 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002180 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 itemsToRemove.add(id);
2182 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002183 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002184 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2185 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002186
2187 if (!customWidget) {
2188 int[] minSpan =
2189 Launcher.getMinSpanForWidget(context, provider);
2190 appWidgetInfo.minSpanX = minSpan[0];
2191 appWidgetInfo.minSpanY = minSpan[1];
2192 }
Sunny Goyalff572272014-07-23 13:58:07 -07002193
2194 int status = restoreStatus;
2195 if (!wasProviderReady) {
2196 // If provider was not previously ready, update the
2197 // status and UI flag.
2198
2199 // Id would be valid only if the widget restore broadcast was received.
2200 if (isIdValid) {
2201 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2202 } else {
2203 status &= ~LauncherAppWidgetInfo
2204 .FLAG_PROVIDER_NOT_READY;
2205 }
2206 }
2207 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002208 } else {
2209 Log.v(TAG, "Widget restore pending id=" + id
2210 + " appWidgetId=" + appWidgetId
2211 + " status =" + restoreStatus);
2212 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002213 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002214 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002215 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002216
2217 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2218 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002219 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002220 // App restore has started. Update the flag
2221 appWidgetInfo.restoreStatus |=
2222 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002223 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002224 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002225 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002226 itemsToRemove.add(id);
2227 continue;
2228 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002229
2230 appWidgetInfo.installProgress =
2231 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002232 }
Sunny Goyalff572272014-07-23 13:58:07 -07002233
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 appWidgetInfo.cellX = c.getInt(cellXIndex);
2237 appWidgetInfo.cellY = c.getInt(cellYIndex);
2238 appWidgetInfo.spanX = c.getInt(spanXIndex);
2239 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002240
Adam Cohen59400422014-03-05 18:07:04 -08002241 if (!customWidget) {
2242 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2243 appWidgetInfo.minSpanX = minSpan[0];
2244 appWidgetInfo.minSpanY = minSpan[1];
2245 }
2246
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 container = c.getInt(containerIndex);
2248 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2249 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2250 Log.e(TAG, "Widget found where container != " +
2251 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2252 continue;
2253 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254
Adam Cohene25af792013-06-06 23:08:25 -07002255 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002257 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2258 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 break;
2260 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002261
Adam Cohen59400422014-03-05 18:07:04 -08002262 if (!customWidget) {
2263 String providerName =
2264 appWidgetInfo.providerName.flattenToString();
2265 if (!providerName.equals(savedProvider) ||
2266 (appWidgetInfo.restoreStatus != restoreStatus)) {
2267 ContentValues values = new ContentValues();
2268 values.put(
2269 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2270 providerName);
2271 values.put(LauncherSettings.Favorites.RESTORED,
2272 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002273 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002274 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002275 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2277 sBgAppWidgets.add(appWidgetInfo);
2278 }
Joe Onorato36115782010-06-17 13:28:48 -04002279 break;
2280 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002282 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002283 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 }
2285 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002286 if (c != null) {
2287 c.close();
2288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 }
2290
Winson Chungba9c37f2013-08-30 14:11:37 -07002291 // Break early if we've stopped loading
2292 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002293 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002294 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002295 }
2296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 if (itemsToRemove.size() > 0) {
2298 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002299 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 // Remove dead items
2301 for (long id : itemsToRemove) {
2302 if (DEBUG_LOADERS) {
2303 Log.d(TAG, "Removed id = " + id);
2304 }
2305 // Don't notify content observers
2306 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002307 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 } catch (RemoteException e) {
2309 Log.w(TAG, "Could not remove id = " + id);
2310 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002311 }
2312 }
2313
Chris Wrenf4d08112014-01-16 18:13:56 -05002314 if (restoredRows.size() > 0) {
2315 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002316 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002317 // Update restored items that no longer require special handling
2318 try {
2319 StringBuilder selectionBuilder = new StringBuilder();
2320 selectionBuilder.append(LauncherSettings.Favorites._ID);
2321 selectionBuilder.append(" IN (");
2322 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2323 selectionBuilder.append(")");
2324 ContentValues values = new ContentValues();
2325 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002326 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002327 values, selectionBuilder.toString(), null);
2328 } catch (RemoteException e) {
2329 Log.w(TAG, "Could not update restored rows");
2330 }
2331 }
2332
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002333 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2334 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002335 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002336 null, sWorker);
2337 }
2338
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002339 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2340 // Log to disk
2341 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2342 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002343
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002344 // Remove any empty screens
2345 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002346 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002347 long screenId = item.screenId;
2348 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2349 unusedScreens.contains(screenId)) {
2350 unusedScreens.remove(screenId);
2351 }
2352 }
2353
2354 // If there are any empty screens remove them, and update.
2355 if (unusedScreens.size() != 0) {
2356 // Log to disk
2357 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2358 TextUtils.join(", ", unusedScreens), true);
2359
2360 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002361 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 }
2363
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 if (DEBUG_LOADERS) {
2365 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2366 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002367 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002368 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002370
Sunny Goyale2df0622015-04-24 11:27:00 -07002371 for (int i = 0; i < nScreens; i++) {
2372 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002373 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 line += " | ";
2375 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002376 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002377 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002378 if (x < screen.length && y < screen[x].length) {
2379 line += (screen[x][y] != null) ? "#" : ".";
2380 } else {
2381 line += "!";
2382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 }
Joe Onorato36115782010-06-17 13:28:48 -04002384 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 }
Joe Onorato36115782010-06-17 13:28:48 -04002388 }
Adam Cohene25af792013-06-06 23:08:25 -07002389 }
2390
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002391 /**
2392 * Partially updates the item without any notification. Must be called on the worker thread.
2393 */
2394 private void updateItem(long itemId, ContentValues update) {
2395 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002396 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002397 update,
2398 BaseColumns._ID + "= ?",
2399 new String[]{Long.toString(itemId)});
2400 }
2401
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 /** Filters the set of items who are directly or indirectly (via another container) on the
2403 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 ArrayList<ItemInfo> allWorkspaceItems,
2406 ArrayList<ItemInfo> currentScreenItems,
2407 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002408 // Purge any null ItemInfos
2409 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2410 while (iter.hasNext()) {
2411 ItemInfo i = iter.next();
2412 if (i == null) {
2413 iter.remove();
2414 }
2415 }
2416
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 // Order the set of items by their containers first, this allows use to walk through the
2418 // list sequentially, build up a list of containers that are in the specified screen,
2419 // as well as all items in those containers.
2420 Set<Long> itemsOnScreen = new HashSet<Long>();
2421 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2422 @Override
2423 public int compare(ItemInfo lhs, ItemInfo rhs) {
2424 return (int) (lhs.container - rhs.container);
2425 }
2426 });
2427 for (ItemInfo info : allWorkspaceItems) {
2428 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002429 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 currentScreenItems.add(info);
2431 itemsOnScreen.add(info.id);
2432 } else {
2433 otherScreenItems.add(info);
2434 }
2435 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2436 currentScreenItems.add(info);
2437 itemsOnScreen.add(info.id);
2438 } else {
2439 if (itemsOnScreen.contains(info.container)) {
2440 currentScreenItems.add(info);
2441 itemsOnScreen.add(info.id);
2442 } else {
2443 otherScreenItems.add(info);
2444 }
2445 }
2446 }
2447 }
2448
2449 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002450 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 ArrayList<LauncherAppWidgetInfo> appWidgets,
2452 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2453 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454
2455 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002456 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002458 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459 currentScreenWidgets.add(widget);
2460 } else {
2461 otherScreenWidgets.add(widget);
2462 }
2463 }
2464 }
2465
2466 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002467 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002468 LongArrayMap<ItemInfo> itemsIdMap,
2469 LongArrayMap<FolderInfo> folders,
2470 LongArrayMap<FolderInfo> currentScreenFolders,
2471 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472
Sunny Goyale2df0622015-04-24 11:27:00 -07002473 int total = folders.size();
2474 for (int i = 0; i < total; i++) {
2475 long id = folders.keyAt(i);
2476 FolderInfo folder = folders.valueAt(i);
2477
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002479 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002481 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 currentScreenFolders.put(id, folder);
2483 } else {
2484 otherScreenFolders.put(id, folder);
2485 }
2486 }
2487 }
2488
2489 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2490 * right) */
2491 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002492 final LauncherAppState app = LauncherAppState.getInstance();
2493 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 // XXX: review this
2495 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002496 @Override
2497 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002498 int cellCountX = (int) grid.numColumns;
2499 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002500 int screenOffset = cellCountX * cellCountY;
2501 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002502 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002503 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002504 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002505 rhs.cellY * cellCountX + rhs.cellX);
2506 return (int) (lr - rr);
2507 }
2508 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002510
Adam Cohendcd297f2013-06-18 13:13:40 -07002511 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2512 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002513 final Runnable r = new Runnable() {
2514 @Override
2515 public void run() {
2516 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2517 if (callbacks != null) {
2518 callbacks.bindScreens(orderedScreens);
2519 }
2520 }
2521 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002522 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002523 }
2524
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2526 final ArrayList<ItemInfo> workspaceItems,
2527 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002528 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002530
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 final boolean postOnMainThread = (deferredBindRunnables != null);
2532
2533 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002534 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002536 final int start = i;
2537 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 final Runnable r = new Runnable() {
2539 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002541 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002543 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2544 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002545 }
2546 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 };
2548 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002549 synchronized (deferredBindRunnables) {
2550 deferredBindRunnables.add(r);
2551 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002553 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 }
Joe Onorato36115782010-06-17 13:28:48 -04002555 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556
2557 // Bind the folders
2558 if (!folders.isEmpty()) {
2559 final Runnable r = new Runnable() {
2560 public void run() {
2561 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2562 if (callbacks != null) {
2563 callbacks.bindFolders(folders);
2564 }
2565 }
2566 };
2567 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002568 synchronized (deferredBindRunnables) {
2569 deferredBindRunnables.add(r);
2570 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002572 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 }
2574 }
2575
2576 // Bind the widgets, one at a time
2577 N = appWidgets.size();
2578 for (int i = 0; i < N; i++) {
2579 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2580 final Runnable r = new Runnable() {
2581 public void run() {
2582 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2583 if (callbacks != null) {
2584 callbacks.bindAppWidget(widget);
2585 }
2586 }
2587 };
2588 if (postOnMainThread) {
2589 deferredBindRunnables.add(r);
2590 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002591 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 }
2593 }
2594 }
2595
2596 /**
2597 * Binds all loaded data to actual views on the main thread.
2598 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002599 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002600 final long t = SystemClock.uptimeMillis();
2601 Runnable r;
2602
2603 // Don't use these two variables in any of the callback runnables.
2604 // Otherwise we hold a reference to them.
2605 final Callbacks oldCallbacks = mCallbacks.get();
2606 if (oldCallbacks == null) {
2607 // This launcher has exited and nobody bothered to tell us. Just bail.
2608 Log.w(TAG, "LoaderTask running with no launcher");
2609 return;
2610 }
2611
Winson Chung9b9fb962013-11-15 15:39:34 -08002612 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002613 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2614 ArrayList<LauncherAppWidgetInfo> appWidgets =
2615 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002616 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002617
2618 final LongArrayMap<FolderInfo> folders;
2619 final LongArrayMap<ItemInfo> itemsIdMap;
2620
Winson Chung2abf94d2012-07-18 18:16:38 -07002621 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622 workspaceItems.addAll(sBgWorkspaceItems);
2623 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002624 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002625
2626 folders = sBgFolders.clone();
2627 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 }
2629
Derek Prothro7aff3992013-12-10 14:00:37 -05002630 final boolean isLoadingSynchronously =
2631 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002632 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002633 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002634 if (currScreen >= orderedScreenIds.size()) {
2635 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002636 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002637 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002638 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002639 final long currentScreenId = currentScreen < 0
2640 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002641
2642 // Load all the items that are on the current page first (and in the process, unbind
2643 // all the existing workspace items before we call startBinding() below.
2644 unbindWorkspaceItemsOnMainThread();
2645
2646 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002647 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2648 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2649 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2650 new ArrayList<LauncherAppWidgetInfo>();
2651 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2652 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002653 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2654 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002655
Winson Chung9b9fb962013-11-15 15:39:34 -08002656 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002657 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002658 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002659 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002660 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 otherFolders);
2662 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2663 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2664
2665 // Tell the workspace that we're about to start binding items
2666 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002667 public void run() {
2668 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2669 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
2672 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002673 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002674 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002675
Adam Cohendcd297f2013-06-18 13:13:40 -07002676 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678 // Load items on the current page
2679 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2680 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002681 if (isLoadingSynchronously) {
2682 r = new Runnable() {
2683 public void run() {
2684 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002685 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002686 callbacks.onPageBoundSynchronously(currentScreen);
2687 }
2688 }
2689 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002690 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002691 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002692
Winson Chung4a2afa32012-07-19 14:53:05 -07002693 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2694 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002695 synchronized (mDeferredBindRunnables) {
2696 mDeferredBindRunnables.clear();
2697 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002698 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002699 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002700
2701 // Tell the workspace that we're done binding items
2702 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002703 public void run() {
2704 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2705 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002706 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002707 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002708
Winson Chung98e030b2012-05-07 16:01:11 -07002709 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002710 if (DEBUG_LOADERS) {
2711 Log.d(TAG, "bound workspace in "
2712 + (SystemClock.uptimeMillis()-t) + "ms");
2713 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002714
2715 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002716 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002717 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002718 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002719 synchronized (mDeferredBindRunnables) {
2720 mDeferredBindRunnables.add(r);
2721 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002722 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002723 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002724 }
Joe Onorato36115782010-06-17 13:28:48 -04002725 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002726
Joe Onorato36115782010-06-17 13:28:48 -04002727 private void loadAndBindAllApps() {
2728 if (DEBUG_LOADERS) {
2729 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2730 }
2731 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002732 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002733 synchronized (LoaderTask.this) {
2734 if (mStopped) {
2735 return;
2736 }
2737 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002738 }
Joe Onorato36115782010-06-17 13:28:48 -04002739 } else {
2740 onlyBindAllApps();
2741 }
2742 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002743
Joe Onorato36115782010-06-17 13:28:48 -04002744 private void onlyBindAllApps() {
2745 final Callbacks oldCallbacks = mCallbacks.get();
2746 if (oldCallbacks == null) {
2747 // This launcher has exited and nobody bothered to tell us. Just bail.
2748 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2749 return;
2750 }
2751
2752 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002753 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002754 final ArrayList<AppInfo> list
2755 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002756 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002757 public void run() {
2758 final long t = SystemClock.uptimeMillis();
2759 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2760 if (callbacks != null) {
2761 callbacks.bindAllApplications(list);
2762 }
2763 if (DEBUG_LOADERS) {
2764 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2765 + (SystemClock.uptimeMillis()-t) + "ms");
2766 }
2767 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002768 };
2769 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002770 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002771 r.run();
2772 } else {
2773 mHandler.post(r);
2774 }
Joe Onorato36115782010-06-17 13:28:48 -04002775 }
2776
Winson Chung64359a52013-07-08 17:17:08 -07002777 private void loadAllApps() {
2778 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002779
Joe Onorato36115782010-06-17 13:28:48 -04002780 final Callbacks oldCallbacks = mCallbacks.get();
2781 if (oldCallbacks == null) {
2782 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002783 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002784 return;
2785 }
2786
2787 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2788 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2789
Kenny Guyed131872014-04-30 03:02:21 +01002790 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2791
Winson Chung64359a52013-07-08 17:17:08 -07002792 // Clear the list of apps
2793 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002794 for (UserHandleCompat user : profiles) {
2795 // Query for the set of apps
2796 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2797 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2798 if (DEBUG_LOADERS) {
2799 Log.d(TAG, "getActivityList took "
2800 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2801 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2802 }
2803 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002804 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002805 if (apps == null || apps.isEmpty()) {
2806 return;
2807 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002808
2809 // Update icon cache
2810 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2811
2812 // If any package icon has changed (app was updated while launcher was dead),
2813 // update the corresponding shortcuts.
2814 if (!updatedPackages.isEmpty()) {
2815 final ArrayList<ShortcutInfo> updates = new ArrayList<ShortcutInfo>();
2816 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002817 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002818 if (info instanceof ShortcutInfo && user.equals(info.user)
2819 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2820 ShortcutInfo si = (ShortcutInfo) info;
2821 ComponentName cn = si.getTargetComponent();
2822 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2823 si.updateIcon(mIconCache);
2824 updates.add(si);
2825 }
2826 }
2827 }
2828 }
2829
2830 if (!updates.isEmpty()) {
2831 final UserHandleCompat userFinal = user;
2832 mHandler.post(new Runnable() {
2833
2834 public void run() {
2835 Callbacks cb = getCallback();
2836 if (cb != null) {
2837 cb.bindShortcutsChanged(
2838 updates, new ArrayList<ShortcutInfo>(), userFinal);
2839 }
2840 }
2841 });
2842 }
Kenny Guyed131872014-04-30 03:02:21 +01002843 }
Joe Onorato36115782010-06-17 13:28:48 -04002844
Kenny Guyed131872014-04-30 03:02:21 +01002845 // Create the ApplicationInfos
2846 for (int i = 0; i < apps.size(); i++) {
2847 LauncherActivityInfoCompat app = apps.get(i);
2848 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002849 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002850 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002851
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002852 if (!user.equals(UserHandleCompat.myUserHandle())) {
2853 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2854 if (heuristic != null) {
2855 heuristic.processUserApps(apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002856 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002857 }
Winson Chung64359a52013-07-08 17:17:08 -07002858 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002859 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002860 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2861 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002862
2863 // Post callback on main thread
2864 mHandler.post(new Runnable() {
2865 public void run() {
2866 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002867 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002868 if (callbacks != null) {
2869 callbacks.bindAllApplications(added);
Hyunyoung Song8821ca92015-05-04 16:28:20 -07002870 loadAndBindWidgetsAndShortcuts(mContext,callbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002871 if (DEBUG_LOADERS) {
2872 Log.d(TAG, "bound " + added.size() + " apps in "
2873 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2874 }
2875 } else {
2876 Log.i(TAG, "not binding apps: no Launcher activity");
2877 }
2878 }
2879 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002880 // Cleanup any data stored for a deleted user.
2881 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002882
Joe Onorato36115782010-06-17 13:28:48 -04002883 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002884 Log.d(TAG, "Icons processed in "
2885 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002886 }
2887 }
2888
2889 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002890 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002891 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2892 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2893 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2894 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2895 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2896 }
Joe Onorato36115782010-06-17 13:28:48 -04002897 }
2898 }
2899
2900 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002901 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002902 }
2903
Adam Cohen091440a2015-03-18 14:16:05 -07002904 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002905
2906 @Override
2907 public void onReceive(Context context, Intent intent) {
2908 synchronized (sBgLock) {
2909 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2910 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002911 final PackageManager manager = context.getPackageManager();
2912 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2913 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002914 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2915 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002916 packagesRemoved.clear();
2917 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002918 for (String pkg : entry.getValue()) {
2919 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002920 boolean packageOnSdcard = launcherApps.isAppEnabled(
2921 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2922 if (packageOnSdcard) {
2923 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2924 packagesUnavailable.add(pkg);
2925 } else {
2926 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2927 packagesRemoved.add(pkg);
2928 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002929 }
2930 }
2931 if (!packagesRemoved.isEmpty()) {
2932 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2933 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2934 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002935 if (!packagesUnavailable.isEmpty()) {
2936 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2937 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2938 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002939 }
Sunny Goyal34942622014-08-29 17:20:55 -07002940 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002941 }
2942 }
2943 }
2944
Joe Onorato36115782010-06-17 13:28:48 -04002945 private class PackageUpdatedTask implements Runnable {
2946 int mOp;
2947 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002948 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002949
2950 public static final int OP_NONE = 0;
2951 public static final int OP_ADD = 1;
2952 public static final int OP_UPDATE = 2;
2953 public static final int OP_REMOVE = 3; // uninstlled
2954 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2955
2956
Kenny Guyed131872014-04-30 03:02:21 +01002957 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002958 mOp = op;
2959 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002960 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002961 }
2962
2963 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002964 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002965
2966 final String[] packages = mPackages;
2967 final int N = packages.length;
2968 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002969 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002970 for (int i=0; i<N; i++) {
2971 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002972 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002973 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002974 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002975
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002976 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2977 if (heuristic != null) {
2978 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002979 }
Joe Onorato36115782010-06-17 13:28:48 -04002980 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002981 }
Joe Onorato36115782010-06-17 13:28:48 -04002982 case OP_UPDATE:
2983 for (int i=0; i<N; i++) {
2984 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002985 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002986 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002987 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002988 }
2989 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002990 case OP_REMOVE: {
2991 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2992 if (heuristic != null) {
2993 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002994 }
Joe Onorato36115782010-06-17 13:28:48 -04002995 for (int i=0; i<N; i++) {
2996 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002997 mIconCache.removeIconsForPkg(packages[i], mUser);
2998 }
2999 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003000 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003001 case OP_UNAVAILABLE:
3002 for (int i=0; i<N; i++) {
3003 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3004 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003005 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003006 }
3007 break;
3008 }
3009
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003010 ArrayList<AppInfo> added = null;
3011 ArrayList<AppInfo> modified = null;
3012 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003013
Adam Cohen487f7dd2012-06-28 18:12:10 -07003014 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003015 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003016 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003017 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003018 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003019 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003020 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003021 }
Winson Chung5d55f332012-07-16 20:45:03 -07003022 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003023 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003024 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003025 }
3026
Sunny Goyale0f58d72014-11-10 18:05:31 -08003027 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003028 if (callbacks == null) {
3029 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3030 return;
3031 }
3032
Sunny Goyal4390ace2014-10-13 11:33:11 -07003033 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3034 new HashMap<ComponentName, AppInfo>();
3035
Joe Onorato36115782010-06-17 13:28:48 -04003036 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003037 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003038 for (AppInfo ai : added) {
3039 addedOrUpdatedApps.put(ai.componentName, ai);
3040 }
Joe Onorato36115782010-06-17 13:28:48 -04003041 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003042
Joe Onorato36115782010-06-17 13:28:48 -04003043 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003044 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003045 for (AppInfo ai : modified) {
3046 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003047 }
3048
Joe Onorato36115782010-06-17 13:28:48 -04003049 mHandler.post(new Runnable() {
3050 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003051 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003052 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003053 callbacks.bindAppsUpdated(modifiedFinal);
3054 }
3055 }
3056 });
3057 }
Winson Chung83892cc2013-05-01 16:53:33 -07003058
Sunny Goyal4390ace2014-10-13 11:33:11 -07003059 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003060 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3062 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3063 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3064
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003065 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003066 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003067 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003068 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3069 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003070 boolean infoUpdated = false;
3071 boolean shortcutUpdated = false;
3072
3073 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003074 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003076 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3077 si.iconResource.resourceName, mIconCache, context);
3078 if (icon != null) {
3079 si.setIcon(icon);
3080 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003081 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 }
3083 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084
3085 ComponentName cn = si.getTargetComponent();
3086 if (cn != null && packageSet.contains(cn.getPackageName())) {
3087 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3088
3089 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3091 // Auto install icon
3092 PackageManager pm = context.getPackageManager();
3093 ResolveInfo matched = pm.resolveActivity(
3094 new Intent(Intent.ACTION_MAIN)
3095 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3096 PackageManager.MATCH_DEFAULT_ONLY);
3097 if (matched == null) {
3098 // Try to find the best match activity.
3099 Intent intent = pm.getLaunchIntentForPackage(
3100 cn.getPackageName());
3101 if (intent != null) {
3102 cn = intent.getComponent();
3103 appInfo = addedOrUpdatedApps.get(cn);
3104 }
3105
3106 if ((intent == null) || (appInfo == null)) {
3107 removedShortcuts.add(si);
3108 continue;
3109 }
3110 si.promisedIntent = intent;
3111 }
3112 }
3113
3114 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003115 if (appInfo != null) {
3116 si.flags = appInfo.flags;
3117 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118
Sunny Goyal756adbc2015-04-16 15:20:51 -07003119 si.intent = si.promisedIntent;
3120 si.promisedIntent = null;
3121 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 infoUpdated = true;
3123 si.updateIcon(mIconCache);
3124 }
3125
3126 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3127 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3128 si.updateIcon(mIconCache);
3129 si.title = appInfo.title.toString();
3130 si.contentDescription = appInfo.contentDescription;
3131 infoUpdated = true;
3132 }
3133
3134 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3135 // Since package was just updated, the target must be available now.
3136 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3137 shortcutUpdated = true;
3138 }
3139 }
3140
3141 if (infoUpdated || shortcutUpdated) {
3142 updatedShortcuts.add(si);
3143 }
3144 if (infoUpdated) {
3145 updateItemInDatabase(context, si);
3146 }
3147 } else if (info instanceof LauncherAppWidgetInfo) {
3148 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3149 if (mUser.equals(widgetInfo.user)
3150 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3151 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3152 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3153 widgets.add(widgetInfo);
3154 updateItemInDatabase(context, widgetInfo);
3155 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003156 }
3157 }
3158 }
3159
Sunny Goyal4390ace2014-10-13 11:33:11 -07003160 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3161 mHandler.post(new Runnable() {
3162
3163 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003164 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165 if (callbacks == cb && cb != null) {
3166 callbacks.bindShortcutsChanged(
3167 updatedShortcuts, removedShortcuts, mUser);
3168 }
3169 }
3170 });
3171 if (!removedShortcuts.isEmpty()) {
3172 deleteItemsFromDatabase(context, removedShortcuts);
3173 }
3174 }
3175 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003176 mHandler.post(new Runnable() {
3177 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003178 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003179 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003181 }
3182 }
3183 });
3184 }
3185 }
3186
Winson Chungdf95eb12013-10-16 14:57:07 -07003187 final ArrayList<String> removedPackageNames =
3188 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003189 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003190 // Mark all packages in the broadcast to be removed
3191 removedPackageNames.addAll(Arrays.asList(packages));
3192 } else if (mOp == OP_UPDATE) {
3193 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003194 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003195 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003196 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003197 }
3198 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003199 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003200
Winson Chungdf95eb12013-10-16 14:57:07 -07003201 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003202 final int removeReason;
3203 if (mOp == OP_UNAVAILABLE) {
3204 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3205 } else {
3206 // Remove all the components associated with this package
3207 for (String pn : removedPackageNames) {
3208 deletePackageFromDatabase(context, pn, mUser);
3209 }
3210 // Remove all the specific components
3211 for (AppInfo a : removedApps) {
3212 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3213 deleteItemsFromDatabase(context, infos);
3214 }
3215 removeReason = 0;
3216 }
3217
Winson Chungdf95eb12013-10-16 14:57:07 -07003218 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003219 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003221 mHandler.post(new Runnable() {
3222 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003224 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003225 callbacks.bindComponentsRemoved(
3226 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003227 }
3228 }
3229 });
Joe Onoratobe386092009-11-17 17:32:16 -08003230 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003231 if (Build.VERSION.SDK_INT < 17) {
3232 loadAndBindWidgetsAndShortcuts(context, callbacks);
3233 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003234 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003235 mHandler.post(new Runnable() {
3236 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003237 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003238 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003239 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003240 }
3241 }
3242 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 }
3244 }
3245
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003246 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3247 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003248 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003249 if (sBgWidgetProviders == null || refresh) {
3250 sBgWidgetProviders = new HashMap<>();
3251 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3252 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003253
Robin Lee26ace122015-03-16 19:41:43 +00003254 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3255 for (AppWidgetProviderInfo pInfo : widgets) {
3256 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3257 UserHandleCompat user = wm.getUser(info);
3258 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3259 }
3260
3261 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3262 for (CustomAppWidget widget : customWidgets) {
3263 info = new LauncherAppWidgetProviderInfo(context, widget);
3264 UserHandleCompat user = wm.getUser(info);
3265 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3266 }
Adam Cohen59400422014-03-05 18:07:04 -08003267 }
Adam Cohen59400422014-03-05 18:07:04 -08003268 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3269 }
3270 }
3271
Robin Lee26ace122015-03-16 19:41:43 +00003272 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3273 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003274 synchronized (sBgLock) {
3275 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003276 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003277 }
Robin Lee26ace122015-03-16 19:41:43 +00003278 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003279 }
3280 }
3281
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003282 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks) {
3283 runOnWorkerThread(new Runnable(){
3284 @Override
3285 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003286 final ArrayList<Object> list = getWidgetsAndShortcuts(context, true /* refresh */);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003287 mHandler.post(new Runnable() {
3288 @Override
3289 public void run() {
3290 Callbacks cb = getCallback();
3291 if (callbacks == cb && cb != null) {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003292 callbacks.bindAllPackages(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003293 }
3294 }
3295 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003296 // update the Widget entries inside DB on the worker thread.
3297 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003298 }
3299 });
3300 }
3301
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003302 /**
3303 * Returns a list of ResolveInfos/AppWidgetInfos.
3304 *
3305 * @see #loadAndBindWidgetsAndShortcuts
3306 */
3307 private ArrayList<Object> getWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003308 PackageManager packageManager = context.getPackageManager();
3309 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003310 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003311 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3312 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Michael Jurkac402cd92013-05-20 15:49:32 +02003313 return widgetsAndShortcuts;
3314 }
3315
Adam Cohen091440a2015-03-18 14:16:05 -07003316 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003317 UserHandleCompat user) {
3318 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3319 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003320 }
Adam Cohen556f6132014-01-15 15:18:08 -08003321
Kenny Guyed131872014-04-30 03:02:21 +01003322 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3323 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003324 if (cn == null) {
3325 return false;
3326 }
Kenny Guyed131872014-04-30 03:02:21 +01003327 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3328 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003329 return false;
3330 }
Kenny Guyed131872014-04-30 03:02:21 +01003331 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003332 }
3333
Adam Cohena28b78e2014-05-20 17:03:04 -07003334 public static boolean isValidPackage(Context context, String packageName,
3335 UserHandleCompat user) {
3336 if (packageName == null) {
3337 return false;
3338 }
3339 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3340 return launcherApps.isPackageEnabledForProfile(packageName, user);
3341 }
3342
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003344 * Make an ShortcutInfo object for a restored application or shortcut item that points
3345 * to a package that is not yet installed on the system.
3346 */
Sunny Goyal34942622014-08-29 17:20:55 -07003347 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003348 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003349 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003350 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003351 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003352
3353 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3354 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3355 if (!TextUtils.isEmpty(title)) {
3356 info.title = title;
3357 }
Sunny Goyal34942622014-08-29 17:20:55 -07003358 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3359 if (TextUtils.isEmpty(info.title)) {
3360 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3361 }
Sunny Goyal34942622014-08-29 17:20:55 -07003362 } else {
3363 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3364 }
3365
Kenny Guyc2bd8102014-06-30 12:30:31 +01003366 info.contentDescription = mUserManager.getBadgedLabelForUser(
3367 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003368 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003369 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003370 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003371 return info;
3372 }
3373
3374 /**
3375 * Make an Intent object for a restored application or shortcut item that points
3376 * to the market page for the item.
3377 */
Adam Cohen091440a2015-03-18 14:16:05 -07003378 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003379 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003380 return getMarketIntent(componentName.getPackageName());
3381 }
3382
3383 static Intent getMarketIntent(String packageName) {
3384 return new Intent(Intent.ACTION_VIEW)
3385 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003386 .scheme("market")
3387 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003388 .appendQueryParameter("id", packageName)
3389 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003390 }
3391
3392 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003393 * Make an ShortcutInfo object for a shortcut that is an application.
3394 *
3395 * If c is not null, then it will be used to fill in missing data like the title and icon.
3396 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003397 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003398 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003399 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003400 if (user == null) {
3401 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003402 return null;
3403 }
3404
Kenny Guyed131872014-04-30 03:02:21 +01003405 ComponentName componentName = intent.getComponent();
3406 if (componentName == null) {
3407 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3408 return null;
3409 }
3410
3411 Intent newIntent = new Intent(intent.getAction(), null);
3412 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3413 newIntent.setComponent(componentName);
3414 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003415 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003416 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3417 return null;
3418 }
3419
3420 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003421 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003422 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3423 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3424 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003425 }
3426
Joe Onorato56d82912010-03-07 14:32:10 -05003427 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003428 if (TextUtils.isEmpty(info.title) && c != null) {
3429 info.title = c.getString(titleIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003430 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003431
Joe Onorato56d82912010-03-07 14:32:10 -05003432 // fall back to the class name of the activity
3433 if (info.title == null) {
3434 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003435 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003436
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003438 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003439 info.contentDescription = mUserManager.getBadgedLabelForUser(
3440 info.title.toString(), info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003441 if (lai != null) {
3442 info.flags = AppInfo.initFlags(lai);
3443 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003446
Sunny Goyale2df0622015-04-24 11:27:00 -07003447 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003448 ItemInfoFilter f) {
3449 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3450 for (ItemInfo i : infos) {
3451 if (i instanceof ShortcutInfo) {
3452 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003453 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003454 if (cn != null && f.filterItem(null, info, cn)) {
3455 filtered.add(info);
3456 }
3457 } else if (i instanceof FolderInfo) {
3458 FolderInfo info = (FolderInfo) i;
3459 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003460 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003461 if (cn != null && f.filterItem(info, s, cn)) {
3462 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003463 }
3464 }
Winson Chung64359a52013-07-08 17:17:08 -07003465 } else if (i instanceof LauncherAppWidgetInfo) {
3466 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3467 ComponentName cn = info.providerName;
3468 if (cn != null && f.filterItem(null, info, cn)) {
3469 filtered.add(info);
3470 }
Winson Chung8a435102012-08-30 17:16:53 -07003471 }
3472 }
Winson Chung64359a52013-07-08 17:17:08 -07003473 return new ArrayList<ItemInfo>(filtered);
3474 }
3475
Adam Cohen091440a2015-03-18 14:16:05 -07003476 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003477 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003478 ItemInfoFilter filter = new ItemInfoFilter() {
3479 @Override
3480 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003481 if (info.user == null) {
3482 return cn.equals(cname);
3483 } else {
3484 return cn.equals(cname) && info.user.equals(user);
3485 }
Winson Chung64359a52013-07-08 17:17:08 -07003486 }
3487 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003488 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003489 }
3490
Sunny Goyal1a745e82014-10-02 15:58:31 -07003491 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003492 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003493 */
Adam Cohen091440a2015-03-18 14:16:05 -07003494 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003495 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3496 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003497
Joe Onorato56d82912010-03-07 14:32:10 -05003498 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003499 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003500 // Non-app shortcuts are only supported for current user.
3501 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003503
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003504 // TODO: If there's an explicit component and we can't install that, delete it.
3505
Joe Onorato56d82912010-03-07 14:32:10 -05003506 info.title = c.getString(titleIndex);
3507
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 int iconType = c.getInt(iconTypeIndex);
3509 switch (iconType) {
3510 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3511 String packageName = c.getString(iconPackageIndex);
3512 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003513 info.customIcon = false;
3514 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003515 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003516 // the db
3517 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003518 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003519 }
3520 // the fallback icon
3521 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003522 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003523 info.usingFallbackIcon = true;
3524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 break;
3526 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003527 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003528 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003529 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003530 info.customIcon = false;
3531 info.usingFallbackIcon = true;
3532 } else {
3533 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 break;
3536 default:
Kenny Guyed131872014-04-30 03:02:21 +01003537 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003538 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 info.customIcon = false;
3540 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003541 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003542 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 return info;
3544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003545
Sunny Goyal2350bc92014-10-14 16:42:54 -07003546 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003547 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3548 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3549 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3550
Adam Cohend9198822011-11-22 16:42:47 -08003551 if (intent == null) {
3552 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3553 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3554 return null;
3555 }
3556
Joe Onorato0589f0f2010-02-08 13:44:00 -08003557 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003558 boolean customIcon = false;
3559 ShortcutIconResource iconResource = null;
3560
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003561 if (bitmap instanceof Bitmap) {
3562 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003563 customIcon = true;
3564 } else {
3565 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003566 if (extra instanceof ShortcutIconResource) {
3567 iconResource = (ShortcutIconResource) extra;
3568 icon = Utilities.createIconBitmap(iconResource.packageName,
3569 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003570 }
3571 }
3572
Michael Jurkac9d95c52011-08-29 14:03:34 -07003573 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003574
Kenny Guyed131872014-04-30 03:02:21 +01003575 // Only support intents for current user for now. Intents sent from other
3576 // users wouldn't get here without intent forwarding anyway.
3577 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003578 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003579 icon = mIconCache.getDefaultIcon(info.user);
3580 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003581 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003582 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003583
Joe Onorato0589f0f2010-02-08 13:44:00 -08003584 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003585 info.contentDescription = mUserManager.getBadgedLabelForUser(
3586 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003587 info.intent = intent;
3588 info.customIcon = customIcon;
3589 info.iconResource = iconResource;
3590
3591 return info;
3592 }
3593
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003595 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 * or make a new one.
3597 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003598 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 // See if a placeholder was created for us already
3600 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003601 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003603 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 folders.put(id, folderInfo);
3605 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003606 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003607 }
3608
Winson Chung1ed747a2011-05-03 16:18:34 -07003609 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003610 private final AppWidgetManagerCompat mManager;
3611 private final PackageManager mPackageManager;
3612 private final HashMap<Object, String> mLabelCache;
3613 private final Collator mCollator;
3614
Hyunyoung Song3f471442015-04-08 19:01:34 -07003615 public WidgetAndShortcutNameComparator(Context context) {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003616 mManager = AppWidgetManagerCompat.getInstance(context);
3617 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003618 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003619 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003620 }
3621 public final int compare(Object a, Object b) {
3622 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003623 if (mLabelCache.containsKey(a)) {
3624 labelA = mLabelCache.get(a);
3625 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003626 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3627 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003628 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003629 mLabelCache.put(a, labelA);
3630 }
3631 if (mLabelCache.containsKey(b)) {
3632 labelB = mLabelCache.get(b);
3633 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003634 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c165aa2015-01-30 10:28:23 -08003635 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003636 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003637 mLabelCache.put(b, labelB);
3638 }
Winson Chung11904872012-09-17 16:58:46 -07003639 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003640 }
3641 };
Joe Onoratobe386092009-11-17 17:32:16 -08003642
Sunny Goyal651077b2014-06-30 14:15:31 -07003643 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3644 return (provider != null) && (provider.provider != null)
3645 && (provider.provider.getPackageName() != null);
3646 }
3647
Joe Onoratobe386092009-11-17 17:32:16 -08003648 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003649 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003650 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3651 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3652 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3653 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003654 if (mLoaderTask != null) {
3655 mLoaderTask.dumpState();
3656 } else {
3657 Log.d(TAG, "mLoaderTask=null");
3658 }
Joe Onoratobe386092009-11-17 17:32:16 -08003659 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003660
3661 public Callbacks getCallback() {
3662 return mCallbacks != null ? mCallbacks.get() : null;
3663 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003664
3665 /**
3666 * @return {@link FolderInfo} if its already loaded.
3667 */
3668 public FolderInfo findFolderById(Long folderId) {
3669 synchronized (sBgLock) {
3670 return sBgFolders.get(folderId);
3671 }
3672 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003673
3674 /**
3675 * @return the looper for the worker thread which can be used to start background tasks.
3676 */
3677 public static Looper getWorkerLooper() {
3678 return sWorkerThread.getLooper();
3679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680}