blob: 9deee43d4d12adecd199b12e889eb483c16fe5d9 [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.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070070import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070071import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.Collections;
73import java.util.Comparator;
74import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070075import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070076import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070078import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070079import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081/**
82 * Maintains in-memory state of the Launcher. It is expected that there should be only one
83 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070084 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 */
Kenny Guyed131872014-04-30 03:02:21 +010086public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010087 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080088 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040089 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070090 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040091
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070093
Dan Sandlerd5024042014-01-09 15:01:33 -050094 public static final int LOADER_FLAG_NONE = 0;
95 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
96 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
97
Joe Onorato36115782010-06-17 13:28:48 -040098 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050099 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800100
Adam Cohen091440a2015-03-18 14:16:05 -0700101 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800102 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final LauncherAppState mApp;
105 @Thunk final Object mLock = new Object();
106 @Thunk DeferredHandler mHandler = new DeferredHandler();
107 @Thunk LoaderTask mLoaderTask;
108 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700109 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700170 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700171 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
172 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700174 // </ only access in worker thread >
175
Adam Cohen091440a2015-03-18 14:16:05 -0700176 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Reena Lee99a73f32011-10-24 17:27:37 -0700178 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk final LauncherAppsCompat mLauncherApps;
181 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700184 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public int getCurrentWorkspaceScreen();
186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700191 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800192 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700195 public void bindAppsAdded(ArrayList<Long> newScreens,
196 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700197 ArrayList<ItemInfo> addAnimated,
198 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
201 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700204 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700205 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song8821ca92015-05-04 16:28:20 -0700206 public void bindAllPackages(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100207 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700208 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700210 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chung64359a52013-07-08 17:17:08 -0700213 public interface ItemInfoFilter {
214 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
215 }
216
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800218 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219
Winson Chungee055712013-07-30 14:46:24 -0700220 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700221 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400222 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
223 // resource string.
224 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
225 ProviderInfo providerInfo =
226 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
227 ProviderInfo redirectProvider =
228 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400231 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 if (mOldContentProviderExists) {
234 Log.d(TAG, "Old launcher provider exists.");
235 } else {
236 Log.d(TAG, "Old launcher provider does not exist.");
237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700244 Configuration config = res.getConfiguration();
245 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
263 private static void runOnWorkerThread(Runnable r) {
264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Sunny Goyal756a28a2015-04-23 17:07:55 -0700272 /**
273 * Runs the specified runnable after the loader is complete
274 */
275 private void runAfterBindCompletes(Runnable r) {
276 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
277 synchronized (mBindCompleteRunnables) {
278 mBindCompleteRunnables.add(r);
279 }
280 } else {
281 runOnWorkerThread(r);
282 }
283 }
284
Winson Chunge43a1e72014-01-15 10:33:02 -0800285 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
286 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800287 }
288
Sunny Goyal756adbc2015-04-16 15:20:51 -0700289 public void setPackageState(final PackageInstallInfo installInfo) {
290 Runnable updateRunnable = new Runnable() {
291
292 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500293 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700294 synchronized (sBgLock) {
295 final HashSet<ItemInfo> updates = new HashSet<>();
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
298 // Ignore install success events as they are handled by Package add events.
299 return;
300 }
301
Sunny Goyale2df0622015-04-24 11:27:00 -0700302 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700303 if (info instanceof ShortcutInfo) {
304 ShortcutInfo si = (ShortcutInfo) info;
305 ComponentName cn = si.getTargetComponent();
306 if (si.isPromise() && (cn != null)
307 && installInfo.packageName.equals(cn.getPackageName())) {
308 si.setInstallProgress(installInfo.progress);
309
310 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
311 // Mark this info as broken.
312 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
313 }
314 updates.add(si);
315 }
316 }
317 }
318
319 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
320 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
321 widget.installProgress = installInfo.progress;
322 updates.add(widget);
323 }
324 }
325
326 if (!updates.isEmpty()) {
327 // Push changes to the callback.
328 Runnable r = new Runnable() {
329 public void run() {
330 Callbacks callbacks = getCallback();
331 if (callbacks != null) {
332 callbacks.bindRestoreItemsChange(updates);
333 }
334 }
335 };
336 mHandler.post(r);
337 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500338 }
339 }
340 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500342 }
343
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 /**
345 * Updates the icons and label of all pending icons for the provided package name.
346 */
347 public void updateSessionDisplayInfo(final String packageName) {
348 Runnable updateRunnable = new Runnable() {
349
350 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700351 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700352 synchronized (sBgLock) {
353 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
354 final UserHandleCompat user = UserHandleCompat.myUserHandle();
355
Sunny Goyale2df0622015-04-24 11:27:00 -0700356 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700357 if (info instanceof ShortcutInfo) {
358 ShortcutInfo si = (ShortcutInfo) info;
359 ComponentName cn = si.getTargetComponent();
360 if (si.isPromise() && (cn != null)
361 && packageName.equals(cn.getPackageName())) {
362 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
363 // For auto install apps update the icon as well as label.
364 mIconCache.getTitleAndIcon(si,
365 si.promisedIntent, user,
366 si.shouldUseLowResIcon());
367 } else {
368 // Only update the icon for restored apps.
369 si.updateIcon(mIconCache);
370 }
371 updates.add(si);
372 }
373 }
374 }
375
376 if (!updates.isEmpty()) {
377 // Push changes to the callback.
378 Runnable r = new Runnable() {
379 public void run() {
380 Callbacks callbacks = getCallback();
381 if (callbacks != null) {
382 callbacks.bindShortcutsChanged(updates,
383 new ArrayList<ShortcutInfo>(), user);
384 }
385 }
386 };
387 mHandler.post(r);
388 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700389 }
390 }
391 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700392 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700393 }
394
Adam Cohen76a47a12014-02-05 11:47:43 -0800395 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800396 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800397
398 if (allAppsApps == null) {
399 throw new RuntimeException("allAppsApps must not be null");
400 }
401 if (allAppsApps.isEmpty()) {
402 return;
403 }
404
405 // Process the newly added applications and add them to the database first
406 Runnable r = new Runnable() {
407 public void run() {
408 runOnMainThread(new Runnable() {
409 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800410 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800411 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500412 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800413 }
414 }
415 });
416 }
417 };
418 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700419 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800420
Sunny Goyala9116722015-04-29 13:55:58 -0700421 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 int[] xy, int spanX, int spanY) {
423 LauncherAppState app = LauncherAppState.getInstance();
424 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
425 final int xCount = (int) grid.numColumns;
426 final int yCount = (int) grid.numRows;
427 boolean[][] occupied = new boolean[xCount][yCount];
428 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700429 for (ItemInfo r : occupiedPos) {
430 int right = r.cellX + r.spanX;
431 int bottom = r.cellY + r.spanY;
432 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
433 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 occupied[x][y] = true;
435 }
436 }
437 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800438 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700439 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 }
441
442 /**
443 * Find a position on the screen for the given size or adds a new screen.
444 * @return screenId and the coordinates for the item.
445 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700446 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 ArrayList<Long> workspaceScreens,
449 ArrayList<Long> addedWorkspaceScreensFinal,
450 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700451 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452
Sunny Goyala9116722015-04-29 13:55:58 -0700453 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700454 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700455 synchronized (sBgLock) {
456 for (ItemInfo info : sBgItemsIdMap) {
457 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
458 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
459 if (items == null) {
460 items = new ArrayList<>();
461 screenItems.put(info.screenId, items);
462 }
463 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
467
468 // Find appropriate space for the item.
469 long screenId = 0;
470 int[] cordinates = new int[2];
471 boolean found = false;
472
473 int screenCount = workspaceScreens.size();
474 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700475 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800476 if (preferredScreenIndex < screenCount) {
477 screenId = workspaceScreens.get(preferredScreenIndex);
478 found = findNextAvailableIconSpaceInScreen(
479 screenItems.get(screenId), cordinates, spanX, spanY);
480 }
481
482 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700483 // Search on any of the screens starting from the first screen.
484 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 screenId = workspaceScreens.get(screen);
486 if (findNextAvailableIconSpaceInScreen(
487 screenItems.get(screenId), cordinates, spanX, spanY)) {
488 // We found a space for it
489 found = true;
490 break;
491 }
492 }
493 }
494
495 if (!found) {
496 // Still no position found. Add a new screen to the end.
497 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
498
499 // Save the screen id for binding in the workspace
500 workspaceScreens.add(screenId);
501 addedWorkspaceScreensFinal.add(screenId);
502
503 // If we still can't find an empty space, then God help us all!!!
504 if (!findNextAvailableIconSpaceInScreen(
505 screenItems.get(screenId), cordinates, spanX, spanY)) {
506 throw new RuntimeException("Can't find space to add the item");
507 }
508 }
509 return Pair.create(screenId, cordinates);
510 }
511
512 /**
513 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700516 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800518 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700519 return;
Winson Chung997a9232013-07-24 15:33:46 -0700520 }
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Process the newly added applications and add them to the database first
522 Runnable r = new Runnable() {
523 public void run() {
524 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
525 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
526
Winson Chung76828c82013-08-19 15:43:29 -0700527 // Get the list of workspace screens. We need to append to this list and
528 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
529 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800530 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700531 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700533 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700535 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700537 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 }
Winson Chung76828c82013-08-19 15:43:29 -0700539
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700541 Pair<Long, int[]> coords = findSpaceForItem(context,
542 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800543 1, 1);
544 long screenId = coords.first;
545 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700546
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700547 ItemInfo itemInfo;
548 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
549 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800550 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700551 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700552 } else {
553 throw new RuntimeException("Unexpected info type");
554 }
Winson Chung94d67682013-09-25 16:29:40 -0700555
Winson Chung64359a52013-07-08 17:17:08 -0700556 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700557 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700558 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700559 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700560 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700561 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700562 }
563 }
564
Winson Chung76828c82013-08-19 15:43:29 -0700565 // Update the workspace screens
566 updateWorkspaceScreenOrder(context, workspaceScreens);
567
Adam Cohen76a47a12014-02-05 11:47:43 -0800568 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700569 runOnMainThread(new Runnable() {
570 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800571 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700572 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700573 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
574 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700575 if (!addedShortcutsFinal.isEmpty()) {
576 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
577 long lastScreenId = info.screenId;
578 for (ItemInfo i : addedShortcutsFinal) {
579 if (i.screenId == lastScreenId) {
580 addAnimated.add(i);
581 } else {
582 addNotAnimated.add(i);
583 }
Winson Chung997a9232013-07-24 15:33:46 -0700584 }
585 }
Winson Chungd64d1762013-08-20 14:37:16 -0700586 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800587 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700588 }
Winson Chung64359a52013-07-08 17:17:08 -0700589 }
Winson Chung997a9232013-07-24 15:33:46 -0700590 });
591 }
Winson Chung64359a52013-07-08 17:17:08 -0700592 }
593 };
594 runOnWorkerThread(r);
595 }
596
Sunny Goyald33860f2015-04-23 16:02:20 -0700597 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700598 if (sWorkerThread.getThreadId() == Process.myTid()) {
599 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
600 "main thread");
601 }
602
603 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400604 synchronized (mDeferredBindRunnables) {
605 mDeferredBindRunnables.clear();
606 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700607
608 // Remove any queued UI runnables
609 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700610 // Unbind all the workspace items
611 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700612 }
613
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700615 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700616 // Ensure that we don't use the same workspace items data structure on the main thread
617 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700618 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700619 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700620 tmpItems.addAll(sBgWorkspaceItems);
621 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700622 }
623 Runnable r = new Runnable() {
624 @Override
625 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700626 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700627 item.unbind();
628 }
629 }
630 };
631 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700632 }
633
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634 /**
635 * Adds an item to the DB if it was not created previously, or move it to a new
636 * <container, screen, cellX, cellY>
637 */
638 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 if (item.container == ItemInfo.NO_ID) {
641 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700642 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 } else {
644 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
647 }
648
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700649 static void checkItemInfoLocked(
650 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
651 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
652 if (modelItem != null && item != modelItem) {
653 // check all the data is consistent
654 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
655 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
656 ShortcutInfo shortcut = (ShortcutInfo) item;
657 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
658 modelShortcut.intent.filterEquals(shortcut.intent) &&
659 modelShortcut.id == shortcut.id &&
660 modelShortcut.itemType == shortcut.itemType &&
661 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700662 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700663 modelShortcut.cellX == shortcut.cellX &&
664 modelShortcut.cellY == shortcut.cellY &&
665 modelShortcut.spanX == shortcut.spanX &&
666 modelShortcut.spanY == shortcut.spanY &&
667 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
668 (modelShortcut.dropPos != null &&
669 shortcut.dropPos != null &&
670 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
671 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
672 // For all intents and purposes, this is the same object
673 return;
674 }
675 }
676
677 // the modelItem needs to match up perfectly with item if our model is
678 // to be consistent with the database-- for now, just require
679 // modelItem == item or the equality check above
680 String msg = "item: " + ((item != null) ? item.toString() : "null") +
681 "modelItem: " +
682 ((modelItem != null) ? modelItem.toString() : "null") +
683 "Error: ItemInfo passed to checkItemInfo doesn't match original";
684 RuntimeException e = new RuntimeException(msg);
685 if (stackTrace != null) {
686 e.setStackTrace(stackTrace);
687 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800688 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700689 }
690 }
691
Michael Jurka816474f2012-06-25 14:49:02 -0700692 static void checkItemInfo(final ItemInfo item) {
693 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
694 final long itemId = item.id;
695 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700696 public void run() {
697 synchronized (sBgLock) {
698 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700699 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700700 }
701 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700702 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700703 }
704
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
706 final ItemInfo item, final String callingFunction) {
707 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700708 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709 final ContentResolver cr = context.getContentResolver();
710
Adam Cohen487f7dd2012-06-28 18:12:10 -0700711 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712 Runnable r = new Runnable() {
713 public void run() {
714 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700715 updateItemArrays(item, itemId, stackTrace);
716 }
717 };
718 runOnWorkerThread(r);
719 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700721 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
722 final ArrayList<ItemInfo> items, final String callingFunction) {
723 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700724
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
726 Runnable r = new Runnable() {
727 public void run() {
728 ArrayList<ContentProviderOperation> ops =
729 new ArrayList<ContentProviderOperation>();
730 int count = items.size();
731 for (int i = 0; i < count; i++) {
732 ItemInfo item = items.get(i);
733 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700734 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700735 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700736
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
738 updateItemArrays(item, itemId, stackTrace);
739
740 }
741 try {
742 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
743 } catch (Exception e) {
744 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700745 }
746 }
747 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700748 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700749 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700750
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
752 // Lock on mBgLock *after* the db operation
753 synchronized (sBgLock) {
754 checkItemInfoLocked(itemId, item, stackTrace);
755
756 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
757 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
758 // Item is in a folder, make sure this folder exists
759 if (!sBgFolders.containsKey(item.container)) {
760 // An items container is being set to a that of an item which is not in
761 // the list of Folders.
762 String msg = "item: " + item + " container being set to: " +
763 item.container + ", not in the list of folders";
764 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 }
766 }
767
768 // Items are added/removed from the corresponding FolderInfo elsewhere, such
769 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
770 // that are on the desktop, as appropriate
771 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800772 if (modelItem != null &&
773 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
774 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775 switch (modelItem.itemType) {
776 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
777 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
778 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
779 if (!sBgWorkspaceItems.contains(modelItem)) {
780 sBgWorkspaceItems.add(modelItem);
781 }
782 break;
783 default:
784 break;
785 }
786 } else {
787 sBgWorkspaceItems.remove(modelItem);
788 }
789 }
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700794 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700795 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 item.cellX = cellX;
799 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 // We store hotseat items in canonical form which is this orientation invariant position
802 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700803 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700805 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809
810 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
813 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800814 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816
Michael Jurkac9d95c52011-08-29 14:03:34 -0700817 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700818 }
819
820 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
822 * cellX, cellY have already been updated on the ItemInfos.
823 */
824 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
825 final long container, final int screen) {
826
827 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
828 int count = items.size();
829
830 for (int i = 0; i < count; i++) {
831 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832 item.container = container;
833
834 // We store hotseat items in canonical form which is this orientation invariant position
835 // in the hotseat
836 if (context instanceof Launcher && screen < 0 &&
837 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 item.cellY);
840 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 }
843
844 final ContentValues values = new ContentValues();
845 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
846 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
847 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800848 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700850
851 contentValues.add(values);
852 }
853 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
854 }
855
856 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800858 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700861 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800862 item.cellX = cellX;
863 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 item.spanX = spanX;
865 item.spanY = spanY;
866
867 // We store hotseat items in canonical form which is this orientation invariant position
868 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700871 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700874 }
Adam Cohend4844c32011-02-18 19:25:06 -0800875
Adam Cohend4844c32011-02-18 19:25:06 -0800876 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800877 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
879 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800880 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700881 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
882 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700883 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800884
Michael Jurka816474f2012-06-25 14:49:02 -0700885 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700886 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700887
888 /**
889 * Update an item to the database in a specified container.
890 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700891 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700892 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100893 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700894 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800895 }
896
Sunny Goyal756a28a2015-04-23 17:07:55 -0700897 private void assertWorkspaceLoaded() {
898 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
899 throw new RuntimeException("Trying to add shortcut while loader is running");
900 }
901 }
902
Adam Cohend4844c32011-02-18 19:25:06 -0800903 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700904 * Returns true if the shortcuts already exists on the workspace. This must be called after
905 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700907 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
908 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700910 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 if (intent.getComponent() != null) {
912 // If component is not null, an intent with null package will produce
913 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700914 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700915 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 intentWithPkg = intent.toUri(0);
917 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700919 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
920 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700921 }
922 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700923 intentWithPkg = intent.toUri(0);
924 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700925 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927
928 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700929 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700930 if (item instanceof ShortcutInfo) {
931 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700932 if (info.getIntent() != null && info.user.equals(user)) {
933 String s = info.getIntent().toUri(0);
934 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
935 return true;
936 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700937 }
938 }
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700941 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700942 }
943
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 /**
945 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
946 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700947 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 final ContentResolver cr = context.getContentResolver();
949 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
950 "_id=? and (itemType=? or itemType=?)",
951 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700952 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700953
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 try {
955 if (c.moveToFirst()) {
956 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
957 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
958 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
959 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
960 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
961 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700962 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 FolderInfo folderInfo = null;
965 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700966 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
967 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700969 }
970
Winson Chung82b016c2015-05-08 17:00:10 -0700971 folderInfo.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 folderInfo.id = id;
973 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700975 folderInfo.cellX = c.getInt(cellXIndex);
976 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700977 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978
979 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 } finally {
982 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700983 }
984
985 return null;
986 }
987
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 /**
989 * Add an item to the database in a specified container. Sets the container, screen, cellX and
990 * cellY fields of the item. Also assigns an ID to the item.
991 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700992 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700993 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 item.cellX = cellX;
996 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 // We store hotseat items in canonical form which is this orientation invariant position
998 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005
1006 final ContentValues values = new ContentValues();
1007 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001008 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009
Michael Jurka414300a2013-08-27 15:42:35 +02001010 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001011 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001012
Jason Monk8e19cf22014-03-20 15:06:57 -04001013 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001014 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001016 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001019 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001020 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 sBgItemsIdMap.put(item.id, item);
1022 switch (item.itemType) {
1023 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1024 sBgFolders.put(item.id, (FolderInfo) item);
1025 // Fall through
1026 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1027 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1028 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1029 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1030 sBgWorkspaceItems.add(item);
1031 } else {
1032 if (!sBgFolders.containsKey(item.container)) {
1033 // Adding an item to a folder that doesn't exist.
1034 String msg = "adding item: " + item + " to a folder that " +
1035 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001036 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001038 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 break;
1040 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1041 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1042 break;
1043 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001044 }
1045 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001046 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001048 }
1049
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001051 * Creates a new unique child id, for a given cell span across all layouts.
1052 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001053 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001057 }
1058
Sunny Goyal34942622014-08-29 17:20:55 -07001059 private static ArrayList<ItemInfo> getItemsByPackageName(
1060 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001061 ItemInfoFilter filter = new ItemInfoFilter() {
1062 @Override
1063 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1064 return cn.getPackageName().equals(pn) && info.user.equals(user);
1065 }
1066 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001067 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001068 }
1069
1070 /**
1071 * Removes all the items from the database corresponding to the specified package.
1072 */
1073 static void deletePackageFromDatabase(Context context, final String pn,
1074 final UserHandleCompat user) {
1075 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001076 }
1077
1078 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001079 * Removes the specified item from the database
1080 * @param context
1081 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001083 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001084 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1085 items.add(item);
1086 deleteItemsFromDatabase(context, items);
1087 }
1088
1089 /**
1090 * Removes the specified items from the database
1091 * @param context
1092 * @param item
1093 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001094 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001096 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001097 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001098 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001099 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001101
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001102 // Lock on mBgLock *after* the db operation
1103 synchronized (sBgLock) {
1104 switch (item.itemType) {
1105 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1106 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001107 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001108 if (info.container == item.id) {
1109 // We are deleting a folder which still contains items that
1110 // think they are contained by that folder.
1111 String msg = "deleting a folder (" + item + ") which still " +
1112 "contains items (" + info + ")";
1113 Log.e(TAG, msg);
1114 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001116 sBgWorkspaceItems.remove(item);
1117 break;
1118 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1119 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1120 sBgWorkspaceItems.remove(item);
1121 break;
1122 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1123 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1124 break;
1125 }
1126 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001128 }
1129 }
Michael Jurka83df1882011-08-31 20:59:26 -07001130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 * Update the order of the workspace screens in the database. The array list contains
1136 * a list of screen ids in the order that they should appear.
1137 */
Winson Chungc9168342013-06-26 14:54:55 -07001138 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001139 // Log to disk
1140 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1141 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1142
Winson Chung64359a52013-07-08 17:17:08 -07001143 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001144 final ContentResolver cr = context.getContentResolver();
1145 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1146
1147 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001148 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 while (iter.hasNext()) {
1150 long id = iter.next();
1151 if (id < 0) {
1152 iter.remove();
1153 }
1154 }
1155
1156 Runnable r = new Runnable() {
1157 @Override
1158 public void run() {
Yura085c8532014-02-11 15:15:29 +00001159 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001161 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001162 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001163 for (int i = 0; i < count; i++) {
1164 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001165 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1167 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001168 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001169 }
Yura085c8532014-02-11 15:15:29 +00001170
1171 try {
1172 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1173 } catch (Exception ex) {
1174 throw new RuntimeException(ex);
1175 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001176
Winson Chungba9c37f2013-08-30 14:11:37 -07001177 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001178 sBgWorkspaceScreens.clear();
1179 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001180 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001181 }
1182 };
1183 runOnWorkerThread(r);
1184 }
1185
1186 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001187 * Remove the contents of the specified folder from the database
1188 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001189 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190 final ContentResolver cr = context.getContentResolver();
1191
Michael Jurkac9d95c52011-08-29 14:03:34 -07001192 Runnable r = new Runnable() {
1193 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001194 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001196 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 sBgItemsIdMap.remove(info.id);
1198 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 sBgWorkspaceItems.remove(info);
1200 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001201
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001202 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001203 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001205 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 for (ItemInfo childInfo : info.contents) {
1207 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001209 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001210 }
1211 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 }
1214
1215 /**
1216 * Set this as the current Launcher activity object for the loader.
1217 */
1218 public void initialize(Callbacks callbacks) {
1219 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001220 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1221 // workspace to prevent leaking Launcher activities on orientation change.
1222 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 mCallbacks = new WeakReference<Callbacks>(callbacks);
1224 }
1225 }
1226
Kenny Guyed131872014-04-30 03:02:21 +01001227 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001228 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001229 int op = PackageUpdatedTask.OP_UPDATE;
1230 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1231 user));
1232 }
1233
1234 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001235 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001236 int op = PackageUpdatedTask.OP_REMOVE;
1237 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1238 user));
1239 }
1240
1241 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001242 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001243 int op = PackageUpdatedTask.OP_ADD;
1244 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1245 user));
1246 }
1247
1248 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001249 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001250 boolean replacing) {
1251 if (!replacing) {
1252 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1253 user));
1254 if (mAppsCanBeOnRemoveableStorage) {
1255 // Only rebind if we support removable storage. It catches the
1256 // case where
1257 // apps on the external sd card need to be reloaded
1258 startLoaderFromBackground();
1259 }
1260 } else {
1261 // If we are replacing then just update the packages in the list
1262 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1263 packageNames, user));
1264 }
1265 }
1266
1267 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001268 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001269 boolean replacing) {
1270 if (!replacing) {
1271 enqueuePackageUpdated(new PackageUpdatedTask(
1272 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1273 user));
1274 }
Kenny Guyed131872014-04-30 03:02:21 +01001275 }
1276
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001277 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1279 * ACTION_PACKAGE_CHANGED.
1280 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001281 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001282 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001283 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001284
Joe Onorato36115782010-06-17 13:28:48 -04001285 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001286 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001287 // If we have changed locale we need to clear out the labels in all apps/workspace.
1288 forceReload();
1289 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1290 // Check if configuration change was an mcc/mnc change which would affect app resources
1291 // and we would need to clear out the labels in all apps/workspace. Same handling as
1292 // above for ACTION_LOCALE_CHANGED
1293 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001294 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001295 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001296 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001297 forceReload();
1298 }
1299 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001300 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001301 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1302 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001303 Callbacks callbacks = getCallback();
1304 if (callbacks != null) {
1305 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001306 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001307 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1308 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1309 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001310 }
1311 }
1312
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001313 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001314 resetLoadedState(true, true);
1315
Reena Lee93f824a2011-09-23 17:20:28 -07001316 // Do this here because if the launcher activity is running it will be restarted.
1317 // If it's not running startLoaderFromBackground will merely tell it that it needs
1318 // to reload.
1319 startLoaderFromBackground();
1320 }
1321
Winson Chungf0c6ae02012-03-21 16:10:31 -07001322 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1323 synchronized (mLock) {
1324 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1325 // mWorkspaceLoaded to true later
1326 stopLoaderLocked();
1327 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1328 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1329 }
1330 }
1331
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001332 /**
1333 * When the launcher is in the background, it's possible for it to miss paired
1334 * configuration changes. So whenever we trigger the loader from the background
1335 * tell the launcher that it needs to re-run the loader when it comes back instead
1336 * of doing it now.
1337 */
1338 public void startLoaderFromBackground() {
1339 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001340 Callbacks callbacks = getCallback();
1341 if (callbacks != null) {
1342 // Only actually run the loader if they're not paused.
1343 if (!callbacks.setLoadOnResume()) {
1344 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001345 }
1346 }
1347 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001348 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001351
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001352 /**
1353 * If there is already a loader task running, tell it to stop.
1354 */
1355 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001356 LoaderTask oldTask = mLoaderTask;
1357 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001358 oldTask.stopLocked();
1359 }
Reena Lee93f824a2011-09-23 17:20:28 -07001360 }
1361
Adam Cohen1a85c582014-09-30 09:48:49 -07001362 public boolean isCurrentCallbacks(Callbacks callbacks) {
1363 return (mCallbacks != null && mCallbacks.get() == callbacks);
1364 }
1365
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001366 public void startLoader(int synchronousBindPage) {
1367 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001368 }
1369
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001370 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001371 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1372 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001373 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 // Clear any deferred bind-runnables from the synchronized load process
1375 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001376 synchronized (mDeferredBindRunnables) {
1377 mDeferredBindRunnables.clear();
1378 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379
Joe Onorato36115782010-06-17 13:28:48 -04001380 // Don't bother to start the thread if we know it's not going to do anything
1381 if (mCallbacks != null && mCallbacks.get() != null) {
1382 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001383 stopLoaderLocked();
1384 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001385 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001386 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1388 } else {
1389 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1390 sWorker.post(mLoaderTask);
1391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 }
1394 }
1395
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001396 void bindRemainingSynchronousPages() {
1397 // Post the remaining side pages to be loaded
1398 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001399 Runnable[] deferredBindRunnables = null;
1400 synchronized (mDeferredBindRunnables) {
1401 deferredBindRunnables = mDeferredBindRunnables.toArray(
1402 new Runnable[mDeferredBindRunnables.size()]);
1403 mDeferredBindRunnables.clear();
1404 }
1405 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001406 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001409
1410 // Run all the bind complete runnables after workspace is bound.
1411 if (!mBindCompleteRunnables.isEmpty()) {
1412 synchronized (mBindCompleteRunnables) {
1413 for (final Runnable r : mBindCompleteRunnables) {
1414 runOnWorkerThread(r);
1415 }
1416 mBindCompleteRunnables.clear();
1417 }
1418 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001419 }
1420
Joe Onorato36115782010-06-17 13:28:48 -04001421 public void stopLoader() {
1422 synchronized (mLock) {
1423 if (mLoaderTask != null) {
1424 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 }
1426 }
Joe Onorato36115782010-06-17 13:28:48 -04001427 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001429 /**
1430 * Loads the workspace screen ids in an ordered list.
1431 */
Adam Cohen091440a2015-03-18 14:16:05 -07001432 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001433 final ContentResolver contentResolver = context.getContentResolver();
1434 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001435
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001436 // Get screens ordered by rank.
1437 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1438 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1439 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001440 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001441 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001442 while (sc.moveToNext()) {
1443 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001444 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001445 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001446 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1447 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001448 }
1449 }
1450 } finally {
1451 sc.close();
1452 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001453 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001454 }
1455
Michael Jurkac57b7a82011-08-09 22:02:20 -07001456 public boolean isAllAppsLoaded() {
1457 return mAllAppsLoaded;
1458 }
1459
Winson Chung36a62fe2012-05-06 18:04:42 -07001460 boolean isLoadingWorkspace() {
1461 synchronized (mLock) {
1462 if (mLoaderTask != null) {
1463 return mLoaderTask.isLoadingWorkspace();
1464 }
1465 }
1466 return false;
1467 }
1468
Joe Onorato36115782010-06-17 13:28:48 -04001469 /**
1470 * Runnable for the thread that loads the contents of the launcher:
1471 * - workspace icons
1472 * - widgets
1473 * - all apps icons
1474 */
1475 private class LoaderTask implements Runnable {
1476 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001477 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001478 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001479 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001480 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001482 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001483 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001484 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 }
1486
Winson Chung36a62fe2012-05-06 18:04:42 -07001487 boolean isLoadingWorkspace() {
1488 return mIsLoadingAndBindingWorkspace;
1489 }
1490
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001491 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001492 mIsLoadingAndBindingWorkspace = true;
1493
Joe Onorato36115782010-06-17 13:28:48 -04001494 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001495 if (DEBUG_LOADERS) {
1496 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001498
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001499 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001500 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001501 synchronized (LoaderTask.this) {
1502 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001503 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001504 }
1505 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 }
1507 }
1508
Joe Onorato36115782010-06-17 13:28:48 -04001509 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001510 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001511 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001512
Joe Onorato36115782010-06-17 13:28:48 -04001513 private void waitForIdle() {
1514 // Wait until the either we're stopped or the other threads are done.
1515 // This way we don't start loading all apps until the workspace has settled
1516 // down.
1517 synchronized (LoaderTask.this) {
1518 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001519
Joe Onorato36115782010-06-17 13:28:48 -04001520 mHandler.postIdle(new Runnable() {
1521 public void run() {
1522 synchronized (LoaderTask.this) {
1523 mLoadAndBindStepFinished = true;
1524 if (DEBUG_LOADERS) {
1525 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001526 }
Joe Onorato36115782010-06-17 13:28:48 -04001527 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001528 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001529 }
Joe Onorato36115782010-06-17 13:28:48 -04001530 });
1531
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001532 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001533 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001534 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1535 // wait no longer than 1sec at a time
1536 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001537 } catch (InterruptedException ex) {
1538 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001539 }
1540 }
Joe Onorato36115782010-06-17 13:28:48 -04001541 if (DEBUG_LOADERS) {
1542 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001543 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001544 + "ms for previous step to finish binding");
1545 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001546 }
Joe Onorato36115782010-06-17 13:28:48 -04001547 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001548
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001549 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001550 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551 // Ensure that we have a valid page index to load synchronously
1552 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1553 "valid page index");
1554 }
1555 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1556 // Ensure that we don't try and bind a specified page when the pages have not been
1557 // loaded already (we should load everything asynchronously in that case)
1558 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1559 }
1560 synchronized (mLock) {
1561 if (mIsLoaderTaskRunning) {
1562 // Ensure that we are never running the background loading at this point since
1563 // we also touch the background collections
1564 throw new RuntimeException("Error! Background loading is already running");
1565 }
1566 }
1567
1568 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1569 // data structures, we can't allow any other thread to touch that data, but because
1570 // this call is synchronous, we can get away with not locking).
1571
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001572 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001573 // operations from the previous activity. We need to ensure that all queued operations
1574 // are executed before any synchronous binding work is done.
1575 mHandler.flush();
1576
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001577 // Divide the set of loaded items into those that we are binding synchronously, and
1578 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001579 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001580 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1581 // arise from that.
1582 onlyBindAllApps();
1583 }
1584
Joe Onorato36115782010-06-17 13:28:48 -04001585 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001586 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001587 if (mStopped) {
1588 return;
1589 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001590 mIsLoaderTaskRunning = true;
1591 }
Joe Onorato36115782010-06-17 13:28:48 -04001592 // Optimize for end-user experience: if the Launcher is up and // running with the
1593 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1594 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001595 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001596 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001597 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001598
Joe Onorato36115782010-06-17 13:28:48 -04001599 if (mStopped) {
1600 break keep_running;
1601 }
1602
Joe Onorato36115782010-06-17 13:28:48 -04001603 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001604
1605 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001606 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1607 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001608 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609
Joe Onorato36115782010-06-17 13:28:48 -04001610 // Clear out this reference, otherwise we end up holding it until all of the
1611 // callback runnables are done.
1612 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613
Joe Onorato36115782010-06-17 13:28:48 -04001614 synchronized (mLock) {
1615 // If we are still the last one to be scheduled, remove ourselves.
1616 if (mLoaderTask == this) {
1617 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001618 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001619 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001620 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
1623
1624 public void stopLocked() {
1625 synchronized (LoaderTask.this) {
1626 mStopped = true;
1627 this.notify();
1628 }
1629 }
1630
1631 /**
1632 * Gets the callbacks object. If we've been stopped, or if the launcher object
1633 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1634 * object that was around when the deferred message was scheduled, and if there's
1635 * a new Callbacks object around then also return null. This will save us from
1636 * calling onto it with data that will be ignored.
1637 */
1638 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1639 synchronized (mLock) {
1640 if (mStopped) {
1641 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001642 }
Joe Onorato36115782010-06-17 13:28:48 -04001643
1644 if (mCallbacks == null) {
1645 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001646 }
Joe Onorato36115782010-06-17 13:28:48 -04001647
1648 final Callbacks callbacks = mCallbacks.get();
1649 if (callbacks != oldCallbacks) {
1650 return null;
1651 }
1652 if (callbacks == null) {
1653 Log.w(TAG, "no mCallbacks");
1654 return null;
1655 }
1656
1657 return callbacks;
1658 }
1659 }
1660
1661 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001662 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001663 LauncherAppState app = LauncherAppState.getInstance();
1664 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001665 final int countX = (int) grid.numColumns;
1666 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001667
Adam Cohendcd297f2013-06-18 13:13:40 -07001668 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001669 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001670 // Return early if we detect that an item is under the hotseat button
1671 if (mCallbacks == null ||
1672 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001673 Log.e(TAG, "Error loading shortcut into hotseat " + item
1674 + " into position (" + item.screenId + ":" + item.cellX + ","
1675 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001676 return false;
1677 }
1678
Dan Sandler295ae182013-12-10 16:05:47 -05001679 final ItemInfo[][] hotseatItems =
1680 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1681
Adam Cohenae4409d2013-11-26 10:34:59 -08001682 if (item.screenId >= grid.numHotseatIcons) {
1683 Log.e(TAG, "Error loading shortcut " + item
1684 + " into hotseat position " + item.screenId
1685 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1686 + ")");
1687 return false;
1688 }
1689
Dan Sandler295ae182013-12-10 16:05:47 -05001690 if (hotseatItems != null) {
1691 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 Log.e(TAG, "Error loading shortcut into hotseat " + item
1693 + " into position (" + item.screenId + ":" + item.cellX + ","
1694 + item.cellY + ") occupied by "
1695 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1696 [(int) item.screenId][0]);
1697 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001698 } else {
1699 hotseatItems[(int) item.screenId][0] = item;
1700 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001701 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001702 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001703 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001704 items[(int) item.screenId][0] = item;
1705 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001706 return true;
1707 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001708 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1709 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001710 return true;
1711 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001712
Adam Cohendcd297f2013-06-18 13:13:40 -07001713 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001714 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001715 occupied.put(item.screenId, items);
1716 }
1717
Dan Sandler295ae182013-12-10 16:05:47 -05001718 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001719 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1720 item.cellX < 0 || item.cellY < 0 ||
1721 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1722 Log.e(TAG, "Error loading shortcut " + item
1723 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1724 + item.cellX + "," + item.cellY
1725 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1726 return false;
1727 }
1728
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001729 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001730 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1731 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001733 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001734 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001735 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001736 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001737 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001738 return false;
1739 }
1740 }
1741 }
1742 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1743 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001744 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001745 }
1746 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001747
Joe Onorato36115782010-06-17 13:28:48 -04001748 return true;
1749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750
Winson Chungba9c37f2013-08-30 14:11:37 -07001751 /** Clears all the sBg data structures */
1752 private void clearSBgDataStructures() {
1753 synchronized (sBgLock) {
1754 sBgWorkspaceItems.clear();
1755 sBgAppWidgets.clear();
1756 sBgFolders.clear();
1757 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001758 sBgWorkspaceScreens.clear();
1759 }
1760 }
1761
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001762 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001763 // Log to disk
1764 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1765
Joe Onorato36115782010-06-17 13:28:48 -04001766 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767
Joe Onorato36115782010-06-17 13:28:48 -04001768 final Context context = mContext;
1769 final ContentResolver contentResolver = context.getContentResolver();
1770 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001771 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001772 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1773 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001774 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001775
Winson Chung892c74d2013-08-22 16:15:50 -07001776 LauncherAppState app = LauncherAppState.getInstance();
1777 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1778 int countX = (int) grid.numColumns;
1779 int countY = (int) grid.numRows;
1780
Dan Sandlerd5024042014-01-09 15:01:33 -05001781 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1782 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1783 LauncherAppState.getLauncherProvider().deleteDatabase();
1784 }
1785
1786 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1787 // append the user's Launcher2 shortcuts
1788 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1789 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1790 } else {
1791 // Make sure the default workspace is loaded
1792 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001793 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001794 }
Adam Cohene25af792013-06-06 23:08:25 -07001795
Winson Chung2abf94d2012-07-18 18:16:38 -07001796 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001797 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001798 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001799 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001800
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001802 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001803 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001804 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001805 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001806
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 // +1 for the hotseat (it can be larger than the workspace)
1808 // Load workspace in reverse order to ensure that latest items are loaded first (and
1809 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001810 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 try {
1813 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1814 final int intentIndex = c.getColumnIndexOrThrow
1815 (LauncherSettings.Favorites.INTENT);
1816 final int titleIndex = c.getColumnIndexOrThrow
1817 (LauncherSettings.Favorites.TITLE);
1818 final int iconTypeIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.ICON_TYPE);
1820 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1821 final int iconPackageIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.ICON_PACKAGE);
1823 final int iconResourceIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.ICON_RESOURCE);
1825 final int containerIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.CONTAINER);
1827 final int itemTypeIndex = c.getColumnIndexOrThrow(
1828 LauncherSettings.Favorites.ITEM_TYPE);
1829 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001831 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 final int screenIndex = c.getColumnIndexOrThrow(
1834 LauncherSettings.Favorites.SCREEN);
1835 final int cellXIndex = c.getColumnIndexOrThrow
1836 (LauncherSettings.Favorites.CELLX);
1837 final int cellYIndex = c.getColumnIndexOrThrow
1838 (LauncherSettings.Favorites.CELLY);
1839 final int spanXIndex = c.getColumnIndexOrThrow
1840 (LauncherSettings.Favorites.SPANX);
1841 final int spanYIndex = c.getColumnIndexOrThrow(
1842 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001843 final int rankIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001845 final int restoredIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001847 final int profileIdIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001849 final int optionsIndex = c.getColumnIndexOrThrow(
1850 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851
Sunny Goyal7f834d22015-04-21 10:10:23 -07001852 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1853 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1854 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1855 }
1856
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 ShortcutInfo info;
1858 String intentDescription;
1859 LauncherAppWidgetInfo appWidgetInfo;
1860 int container;
1861 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001862 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001864 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 while (!mStopped && c.moveToNext()) {
1867 try {
1868 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001869 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001870 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 switch (itemType) {
1873 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1874 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001875 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001877 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001878 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001879 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001880 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001881 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001882 if (user == null) {
1883 // User has been deleted remove the item.
1884 itemsToRemove.add(id);
1885 continue;
1886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 try {
1888 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001889 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001890 if (cn != null && cn.getPackageName() != null) {
1891 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1892 cn.getPackageName(), user);
1893 boolean validComponent = validPkg &&
1894 launcherApps.isActivityEnabledForProfile(cn, user);
1895
1896 if (validComponent) {
1897 if (restored) {
1898 // no special handling necessary for this item
1899 restoredRows.add(id);
1900 restored = false;
1901 }
1902 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001903 intent = null;
1904 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1905 // We allow auto install apps to have their intent
1906 // updated after an install.
1907 intent = manager.getLaunchIntentForPackage(
1908 cn.getPackageName());
1909 if (intent != null) {
1910 ContentValues values = new ContentValues();
1911 values.put(LauncherSettings.Favorites.INTENT,
1912 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001914 }
1915 }
1916
1917 if (intent == null) {
1918 // The app is installed but the component is no
1919 // longer available.
1920 Launcher.addDumpLog(TAG,
1921 "Invalid component removed: " + cn, true);
1922 itemsToRemove.add(id);
1923 continue;
1924 } else {
1925 // no special handling necessary for this item
1926 restoredRows.add(id);
1927 restored = false;
1928 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001929 } else if (restored) {
1930 // Package is not yet available but might be
1931 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001932 Launcher.addDumpLog(TAG,
1933 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001934
1935 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1936 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001937 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001938 // App restore has started. Update the flag
1939 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1940 ContentValues values = new ContentValues();
1941 values.put(LauncherSettings.Favorites.RESTORED,
1942 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001943 updateItem(id, values);
1944 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1945 // This is a common app. Try to replace this.
1946 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1947 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1948 if (parser.findDefaultApp()) {
1949 // Default app found. Replace it.
1950 intent = parser.parsedIntent;
1951 cn = intent.getComponent();
1952 ContentValues values = parser.parsedValues;
1953 values.put(LauncherSettings.Favorites.RESTORED, 0);
1954 updateItem(id, values);
1955 restored = false;
1956 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001957
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001958 } else if (REMOVE_UNRESTORED_ICONS) {
1959 Launcher.addDumpLog(TAG,
1960 "Unrestored package removed: " + cn, true);
1961 itemsToRemove.add(id);
1962 continue;
1963 }
Sunny Goyal94485362014-09-18 16:13:58 -07001964 } else if (REMOVE_UNRESTORED_ICONS) {
1965 Launcher.addDumpLog(TAG,
1966 "Unrestored package removed: " + cn, true);
1967 itemsToRemove.add(id);
1968 continue;
1969 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001970 } else if (launcherApps.isAppEnabled(
1971 manager, cn.getPackageName(),
1972 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1973 // Package is present but not available.
1974 allowMissingTarget = true;
1975 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1976 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001977 // SdCard is not ready yet. Package might get available,
1978 // once it is ready.
1979 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1980 + " (check again later)", true);
1981 HashSet<String> pkgs = sPendingPackages.get(user);
1982 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001983 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001984 sPendingPackages.put(user, pkgs);
1985 }
1986 pkgs.add(cn.getPackageName());
1987 allowMissingTarget = true;
1988 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001989
1990 } else {
1991 // Do not wait for external media load anymore.
1992 // Log the invalid package, and remove it
1993 Launcher.addDumpLog(TAG,
1994 "Invalid package removed: " + cn, true);
1995 itemsToRemove.add(id);
1996 continue;
Winson Chungee055712013-07-30 14:46:24 -07001997 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001998 } else if (cn == null) {
1999 // For shortcuts with no component, keep them as they are
2000 restoredRows.add(id);
2001 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002002 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002004 Launcher.addDumpLog(TAG,
2005 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 continue;
2007 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008
Sunny Goyal34b65272015-03-11 16:56:52 -07002009 container = c.getInt(containerIndex);
2010 boolean useLowResIcon = container >= 0 &&
2011 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2012
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002013 if (itemReplaced) {
2014 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002015 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002016 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002017 } else {
2018 // Don't replace items for other profiles.
2019 itemsToRemove.add(id);
2020 continue;
2021 }
2022 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002023 if (user.equals(UserHandleCompat.myUserHandle())) {
2024 Launcher.addDumpLog(TAG,
2025 "constructing info for partially restored package",
2026 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002027 info = getRestoredItemInfo(c, titleIndex, intent,
2028 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002029 intent = getRestoredItemIntent(c, context, intent);
2030 } else {
2031 // Don't restore items for other profiles.
2032 itemsToRemove.add(id);
2033 continue;
2034 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002035 } else if (itemType ==
2036 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002037 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002038 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 } else {
2040 info = getShortcutInfo(c, context, iconTypeIndex,
2041 iconPackageIndex, iconResourceIndex, iconIndex,
2042 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 // App shortcuts that used to be automatically added to Launcher
2045 // didn't always have the correct intent flags set, so do that
2046 // here
2047 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002048 intent.getCategories() != null &&
2049 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002050 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 intent.addFlags(
2052 Intent.FLAG_ACTIVITY_NEW_TASK |
2053 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2054 }
Michael Jurka96879562012-03-22 05:54:33 -07002055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002058 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 info.cellX = c.getInt(cellXIndex);
2063 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002064 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002065 info.spanX = 1;
2066 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002067 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002068 info.isDisabled = disabledState;
2069 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2070 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2071 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002072
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002074 if (!checkItemPlacement(occupied, info)) {
2075 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 break;
2077 }
2078
Sunny Goyal756adbc2015-04-16 15:20:51 -07002079 if (restored) {
2080 ComponentName cn = info.getTargetComponent();
2081 if (cn != null) {
2082 Integer progress = installingPkgs.get(cn.getPackageName());
2083 if (progress != null) {
2084 info.setInstallProgress(progress);
2085 } else {
2086 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2087 }
2088 }
2089 }
2090
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 switch (container) {
2092 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2093 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2094 sBgWorkspaceItems.add(info);
2095 break;
2096 default:
2097 // Item is in a user folder
2098 FolderInfo folderInfo =
2099 findOrMakeFolder(sBgFolders, container);
2100 folderInfo.add(info);
2101 break;
2102 }
2103 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002104 } else {
2105 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 }
2107 break;
2108
2109 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2110 id = c.getLong(idIndex);
2111 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2112
Winson Chung82b016c2015-05-08 17:00:10 -07002113 folderInfo.title = Utilities.trim(c.getString(titleIndex));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 folderInfo.cellX = c.getInt(cellXIndex);
2119 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002120 folderInfo.spanX = 1;
2121 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002122 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123
Daniel Sandler8802e962010-05-26 16:28:16 -04002124 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002125 if (!checkItemPlacement(occupied, folderInfo)) {
2126 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002127 break;
2128 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002129
Joe Onorato9c1289c2009-08-17 11:03:03 -04002130 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002131 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2132 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2133 sBgWorkspaceItems.add(folderInfo);
2134 break;
Joe Onorato36115782010-06-17 13:28:48 -04002135 }
Joe Onorato17a89222011-02-08 17:26:11 -08002136
Chris Wrenf4d08112014-01-16 18:13:56 -05002137 if (restored) {
2138 // no special handling required for restored folders
2139 restoredRows.add(id);
2140 }
2141
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2143 sBgFolders.put(folderInfo.id, folderInfo);
2144 break;
2145
2146 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002147 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002149 boolean customWidget = itemType ==
2150 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2151
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002153 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002154 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002155 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002156 user = allUsers.get(serialNumber);
2157 if (user == null) {
2158 itemsToRemove.add(id);
2159 continue;
2160 }
2161
Sunny Goyalff572272014-07-23 13:58:07 -07002162 final ComponentName component =
2163 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002164
Sunny Goyal651077b2014-06-30 14:15:31 -07002165 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002166 final boolean isIdValid = (restoreStatus &
2167 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002168 final boolean wasProviderReady = (restoreStatus &
2169 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002170
Adam Cohen59400422014-03-05 18:07:04 -08002171 final LauncherAppWidgetProviderInfo provider =
2172 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002173 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002174 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002175
2176 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002177 if (!isSafeMode && !customWidget &&
2178 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002179 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002180 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002181
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002183 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 itemsToRemove.add(id);
2185 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002186 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002187 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2188 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002189
2190 if (!customWidget) {
2191 int[] minSpan =
2192 Launcher.getMinSpanForWidget(context, provider);
2193 appWidgetInfo.minSpanX = minSpan[0];
2194 appWidgetInfo.minSpanY = minSpan[1];
2195 }
Sunny Goyalff572272014-07-23 13:58:07 -07002196
2197 int status = restoreStatus;
2198 if (!wasProviderReady) {
2199 // If provider was not previously ready, update the
2200 // status and UI flag.
2201
2202 // Id would be valid only if the widget restore broadcast was received.
2203 if (isIdValid) {
2204 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2205 } else {
2206 status &= ~LauncherAppWidgetInfo
2207 .FLAG_PROVIDER_NOT_READY;
2208 }
2209 }
2210 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002211 } else {
2212 Log.v(TAG, "Widget restore pending id=" + id
2213 + " appWidgetId=" + appWidgetId
2214 + " status =" + restoreStatus);
2215 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002216 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002217 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002218 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002219
2220 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2221 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002222 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002223 // App restore has started. Update the flag
2224 appWidgetInfo.restoreStatus |=
2225 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002226 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002227 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002228 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002229 itemsToRemove.add(id);
2230 continue;
2231 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002232
2233 appWidgetInfo.installProgress =
2234 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002235 }
Sunny Goyalff572272014-07-23 13:58:07 -07002236
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002238 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 appWidgetInfo.cellX = c.getInt(cellXIndex);
2240 appWidgetInfo.cellY = c.getInt(cellYIndex);
2241 appWidgetInfo.spanX = c.getInt(spanXIndex);
2242 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002243
Adam Cohen59400422014-03-05 18:07:04 -08002244 if (!customWidget) {
2245 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2246 appWidgetInfo.minSpanX = minSpan[0];
2247 appWidgetInfo.minSpanY = minSpan[1];
2248 }
2249
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 container = c.getInt(containerIndex);
2251 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2252 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2253 Log.e(TAG, "Widget found where container != " +
2254 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2255 continue;
2256 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257
Adam Cohene25af792013-06-06 23:08:25 -07002258 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002260 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2261 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002262 break;
2263 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002264
Adam Cohen59400422014-03-05 18:07:04 -08002265 if (!customWidget) {
2266 String providerName =
2267 appWidgetInfo.providerName.flattenToString();
2268 if (!providerName.equals(savedProvider) ||
2269 (appWidgetInfo.restoreStatus != restoreStatus)) {
2270 ContentValues values = new ContentValues();
2271 values.put(
2272 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2273 providerName);
2274 values.put(LauncherSettings.Favorites.RESTORED,
2275 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002276 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002277 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002278 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2280 sBgAppWidgets.add(appWidgetInfo);
2281 }
Joe Onorato36115782010-06-17 13:28:48 -04002282 break;
2283 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002285 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002286 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 }
2288 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002289 if (c != null) {
2290 c.close();
2291 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 }
2293
Winson Chungba9c37f2013-08-30 14:11:37 -07002294 // Break early if we've stopped loading
2295 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002296 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002297 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002298 }
2299
Sunny Goyal1936ec02015-05-12 11:03:04 -07002300 // Remove any empty folder
2301 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2302 for (ItemInfo item: sBgItemsIdMap) {
2303 long container = item.container;
2304 if (emptyFolders.containsKey(container)) {
2305 emptyFolders.remove(container);
2306 }
2307 }
2308 for (FolderInfo folder : emptyFolders) {
2309 long folderId = folder.id;
2310 sBgFolders.remove(folderId);
2311 sBgItemsIdMap.remove(folderId);
2312 sBgWorkspaceItems.remove(folder);
2313 itemsToRemove.add(folderId);
2314 }
2315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 if (itemsToRemove.size() > 0) {
2317 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002318 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 // Remove dead items
2320 for (long id : itemsToRemove) {
2321 if (DEBUG_LOADERS) {
2322 Log.d(TAG, "Removed id = " + id);
2323 }
2324 // Don't notify content observers
2325 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002326 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 } catch (RemoteException e) {
2328 Log.w(TAG, "Could not remove id = " + id);
2329 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002330 }
2331 }
2332
Chris Wrenf4d08112014-01-16 18:13:56 -05002333 if (restoredRows.size() > 0) {
2334 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002335 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002336 // Update restored items that no longer require special handling
2337 try {
2338 StringBuilder selectionBuilder = new StringBuilder();
2339 selectionBuilder.append(LauncherSettings.Favorites._ID);
2340 selectionBuilder.append(" IN (");
2341 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2342 selectionBuilder.append(")");
2343 ContentValues values = new ContentValues();
2344 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002345 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002346 values, selectionBuilder.toString(), null);
2347 } catch (RemoteException e) {
2348 Log.w(TAG, "Could not update restored rows");
2349 }
2350 }
2351
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002352 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2353 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002354 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002355 null, sWorker);
2356 }
2357
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002358 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2359 // Log to disk
2360 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2361 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002362
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002363 // Remove any empty screens
2364 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002365 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002366 long screenId = item.screenId;
2367 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2368 unusedScreens.contains(screenId)) {
2369 unusedScreens.remove(screenId);
2370 }
2371 }
2372
2373 // If there are any empty screens remove them, and update.
2374 if (unusedScreens.size() != 0) {
2375 // Log to disk
2376 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2377 TextUtils.join(", ", unusedScreens), true);
2378
2379 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002381 }
2382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 if (DEBUG_LOADERS) {
2384 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2385 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002387 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002389
Sunny Goyale2df0622015-04-24 11:27:00 -07002390 for (int i = 0; i < nScreens; i++) {
2391 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002392 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 line += " | ";
2394 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002395 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002396 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002397 if (x < screen.length && y < screen[x].length) {
2398 line += (screen[x][y] != null) ? "#" : ".";
2399 } else {
2400 line += "!";
2401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 }
Joe Onorato36115782010-06-17 13:28:48 -04002403 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406 }
Joe Onorato36115782010-06-17 13:28:48 -04002407 }
Adam Cohene25af792013-06-06 23:08:25 -07002408 }
2409
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002410 /**
2411 * Partially updates the item without any notification. Must be called on the worker thread.
2412 */
2413 private void updateItem(long itemId, ContentValues update) {
2414 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002415 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002416 update,
2417 BaseColumns._ID + "= ?",
2418 new String[]{Long.toString(itemId)});
2419 }
2420
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 /** Filters the set of items who are directly or indirectly (via another container) on the
2422 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002423 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 ArrayList<ItemInfo> allWorkspaceItems,
2425 ArrayList<ItemInfo> currentScreenItems,
2426 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002427 // Purge any null ItemInfos
2428 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2429 while (iter.hasNext()) {
2430 ItemInfo i = iter.next();
2431 if (i == null) {
2432 iter.remove();
2433 }
2434 }
2435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 // Order the set of items by their containers first, this allows use to walk through the
2437 // list sequentially, build up a list of containers that are in the specified screen,
2438 // as well as all items in those containers.
2439 Set<Long> itemsOnScreen = new HashSet<Long>();
2440 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2441 @Override
2442 public int compare(ItemInfo lhs, ItemInfo rhs) {
2443 return (int) (lhs.container - rhs.container);
2444 }
2445 });
2446 for (ItemInfo info : allWorkspaceItems) {
2447 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002448 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 currentScreenItems.add(info);
2450 itemsOnScreen.add(info.id);
2451 } else {
2452 otherScreenItems.add(info);
2453 }
2454 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2455 currentScreenItems.add(info);
2456 itemsOnScreen.add(info.id);
2457 } else {
2458 if (itemsOnScreen.contains(info.container)) {
2459 currentScreenItems.add(info);
2460 itemsOnScreen.add(info.id);
2461 } else {
2462 otherScreenItems.add(info);
2463 }
2464 }
2465 }
2466 }
2467
2468 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002469 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 ArrayList<LauncherAppWidgetInfo> appWidgets,
2471 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2472 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473
2474 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002475 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002477 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 currentScreenWidgets.add(widget);
2479 } else {
2480 otherScreenWidgets.add(widget);
2481 }
2482 }
2483 }
2484
2485 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002486 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002487 LongArrayMap<ItemInfo> itemsIdMap,
2488 LongArrayMap<FolderInfo> folders,
2489 LongArrayMap<FolderInfo> currentScreenFolders,
2490 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491
Sunny Goyale2df0622015-04-24 11:27:00 -07002492 int total = folders.size();
2493 for (int i = 0; i < total; i++) {
2494 long id = folders.keyAt(i);
2495 FolderInfo folder = folders.valueAt(i);
2496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002498 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002500 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 currentScreenFolders.put(id, folder);
2502 } else {
2503 otherScreenFolders.put(id, folder);
2504 }
2505 }
2506 }
2507
2508 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2509 * right) */
2510 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002511 final LauncherAppState app = LauncherAppState.getInstance();
2512 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 // XXX: review this
2514 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002515 @Override
2516 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002517 int cellCountX = (int) grid.numColumns;
2518 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002519 int screenOffset = cellCountX * cellCountY;
2520 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002521 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002522 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002523 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002524 rhs.cellY * cellCountX + rhs.cellX);
2525 return (int) (lr - rr);
2526 }
2527 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002529
Adam Cohendcd297f2013-06-18 13:13:40 -07002530 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2531 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002532 final Runnable r = new Runnable() {
2533 @Override
2534 public void run() {
2535 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2536 if (callbacks != null) {
2537 callbacks.bindScreens(orderedScreens);
2538 }
2539 }
2540 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002541 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002542 }
2543
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2545 final ArrayList<ItemInfo> workspaceItems,
2546 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002547 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 final boolean postOnMainThread = (deferredBindRunnables != null);
2551
2552 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002553 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002555 final int start = i;
2556 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 final Runnable r = new Runnable() {
2558 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002559 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002560 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002562 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2563 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002564 }
2565 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566 };
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 }
Joe Onorato36115782010-06-17 13:28:48 -04002574 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575
2576 // Bind the folders
2577 if (!folders.isEmpty()) {
2578 final Runnable r = new Runnable() {
2579 public void run() {
2580 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2581 if (callbacks != null) {
2582 callbacks.bindFolders(folders);
2583 }
2584 }
2585 };
2586 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002587 synchronized (deferredBindRunnables) {
2588 deferredBindRunnables.add(r);
2589 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002590 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002591 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 }
2593 }
2594
2595 // Bind the widgets, one at a time
2596 N = appWidgets.size();
2597 for (int i = 0; i < N; i++) {
2598 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2599 final Runnable r = new Runnable() {
2600 public void run() {
2601 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2602 if (callbacks != null) {
2603 callbacks.bindAppWidget(widget);
2604 }
2605 }
2606 };
2607 if (postOnMainThread) {
2608 deferredBindRunnables.add(r);
2609 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002610 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 }
2612 }
2613 }
2614
2615 /**
2616 * Binds all loaded data to actual views on the main thread.
2617 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002618 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002619 final long t = SystemClock.uptimeMillis();
2620 Runnable r;
2621
2622 // Don't use these two variables in any of the callback runnables.
2623 // Otherwise we hold a reference to them.
2624 final Callbacks oldCallbacks = mCallbacks.get();
2625 if (oldCallbacks == null) {
2626 // This launcher has exited and nobody bothered to tell us. Just bail.
2627 Log.w(TAG, "LoaderTask running with no launcher");
2628 return;
2629 }
2630
Winson Chung9b9fb962013-11-15 15:39:34 -08002631 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2633 ArrayList<LauncherAppWidgetInfo> appWidgets =
2634 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002635 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002636
2637 final LongArrayMap<FolderInfo> folders;
2638 final LongArrayMap<ItemInfo> itemsIdMap;
2639
Winson Chung2abf94d2012-07-18 18:16:38 -07002640 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641 workspaceItems.addAll(sBgWorkspaceItems);
2642 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002643 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002644
2645 folders = sBgFolders.clone();
2646 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002647 }
2648
Derek Prothro7aff3992013-12-10 14:00:37 -05002649 final boolean isLoadingSynchronously =
2650 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002651 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002652 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002653 if (currScreen >= orderedScreenIds.size()) {
2654 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002655 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002656 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002657 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002658 final long currentScreenId = currentScreen < 0
2659 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002660
2661 // Load all the items that are on the current page first (and in the process, unbind
2662 // all the existing workspace items before we call startBinding() below.
2663 unbindWorkspaceItemsOnMainThread();
2664
2665 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2667 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2668 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2669 new ArrayList<LauncherAppWidgetInfo>();
2670 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2671 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002672 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2673 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002674
Winson Chung9b9fb962013-11-15 15:39:34 -08002675 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002676 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002677 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002679 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 otherFolders);
2681 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2682 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2683
2684 // Tell the workspace that we're about to start binding items
2685 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002686 public void run() {
2687 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2688 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002689 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002690 }
2691 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002692 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002693 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694
Adam Cohendcd297f2013-06-18 13:13:40 -07002695 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2696
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002697 // Load items on the current page
2698 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2699 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002700 if (isLoadingSynchronously) {
2701 r = new Runnable() {
2702 public void run() {
2703 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002704 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002705 callbacks.onPageBoundSynchronously(currentScreen);
2706 }
2707 }
2708 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002709 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002710 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002711
Winson Chung4a2afa32012-07-19 14:53:05 -07002712 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2713 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002714 synchronized (mDeferredBindRunnables) {
2715 mDeferredBindRunnables.clear();
2716 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002717 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002718 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002719
2720 // Tell the workspace that we're done binding items
2721 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002722 public void run() {
2723 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2724 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002725 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002726 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002727
Winson Chung98e030b2012-05-07 16:01:11 -07002728 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002729 if (DEBUG_LOADERS) {
2730 Log.d(TAG, "bound workspace in "
2731 + (SystemClock.uptimeMillis()-t) + "ms");
2732 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002733
2734 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002735 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002736 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002737 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002738 synchronized (mDeferredBindRunnables) {
2739 mDeferredBindRunnables.add(r);
2740 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002741 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002742 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002743 }
Joe Onorato36115782010-06-17 13:28:48 -04002744 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002745
Joe Onorato36115782010-06-17 13:28:48 -04002746 private void loadAndBindAllApps() {
2747 if (DEBUG_LOADERS) {
2748 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2749 }
2750 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002751 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002752 synchronized (LoaderTask.this) {
2753 if (mStopped) {
2754 return;
2755 }
2756 }
Sunny Goyal75b0f552015-05-20 21:57:06 -07002757 mIconCache.updateDbIcons();
Reena Lee93f824a2011-09-23 17:20:28 -07002758 synchronized (LoaderTask.this) {
2759 if (mStopped) {
2760 return;
2761 }
2762 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002763 }
Joe Onorato36115782010-06-17 13:28:48 -04002764 } else {
2765 onlyBindAllApps();
2766 }
2767 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002768
Joe Onorato36115782010-06-17 13:28:48 -04002769 private void onlyBindAllApps() {
2770 final Callbacks oldCallbacks = mCallbacks.get();
2771 if (oldCallbacks == null) {
2772 // This launcher has exited and nobody bothered to tell us. Just bail.
2773 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2774 return;
2775 }
2776
2777 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002778 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002779 final ArrayList<AppInfo> list
2780 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002781 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002782 public void run() {
2783 final long t = SystemClock.uptimeMillis();
2784 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2785 if (callbacks != null) {
2786 callbacks.bindAllApplications(list);
2787 }
2788 if (DEBUG_LOADERS) {
2789 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2790 + (SystemClock.uptimeMillis()-t) + "ms");
2791 }
2792 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002793 };
2794 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002795 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002796 r.run();
2797 } else {
2798 mHandler.post(r);
2799 }
Hyunyoung Song219d0482015-05-07 12:51:42 -07002800 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2801 false /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002802 }
2803
Winson Chung64359a52013-07-08 17:17:08 -07002804 private void loadAllApps() {
2805 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002806
Joe Onorato36115782010-06-17 13:28:48 -04002807 final Callbacks oldCallbacks = mCallbacks.get();
2808 if (oldCallbacks == null) {
2809 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002810 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002811 return;
2812 }
2813
Kenny Guyed131872014-04-30 03:02:21 +01002814 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2815
Winson Chung64359a52013-07-08 17:17:08 -07002816 // Clear the list of apps
2817 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002818 for (UserHandleCompat user : profiles) {
2819 // Query for the set of apps
2820 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002821 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002822 if (DEBUG_LOADERS) {
2823 Log.d(TAG, "getActivityList took "
2824 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2825 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2826 }
2827 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002828 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002829 if (apps == null || apps.isEmpty()) {
2830 return;
2831 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002832
Kenny Guyed131872014-04-30 03:02:21 +01002833 // Create the ApplicationInfos
2834 for (int i = 0; i < apps.size(); i++) {
2835 LauncherActivityInfoCompat app = apps.get(i);
2836 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002837 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002838 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002839
Sunny Goyal756a28a2015-04-23 17:07:55 -07002840 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2841 if (heuristic != null) {
2842 runAfterBindCompletes(new Runnable() {
2843
2844 @Override
2845 public void run() {
2846 heuristic.processUserApps(apps);
2847 }
2848 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002849 }
Winson Chung64359a52013-07-08 17:17:08 -07002850 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002851 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002852 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2853 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002854
2855 // Post callback on main thread
2856 mHandler.post(new Runnable() {
2857 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002858
Winson Chung64359a52013-07-08 17:17:08 -07002859 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002860 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002861 if (callbacks != null) {
2862 callbacks.bindAllApplications(added);
2863 if (DEBUG_LOADERS) {
2864 Log.d(TAG, "bound " + added.size() + " apps in "
2865 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2866 }
2867 } else {
2868 Log.i(TAG, "not binding apps: no Launcher activity");
2869 }
2870 }
2871 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002872 // Cleanup any data stored for a deleted user.
2873 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002874
Hyunyoung Song219d0482015-05-07 12:51:42 -07002875 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2876 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002877 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002878 Log.d(TAG, "Icons processed in "
2879 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002880 }
2881 }
2882
2883 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002884 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002885 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002886 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2887 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2888 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2889 }
Joe Onorato36115782010-06-17 13:28:48 -04002890 }
2891 }
2892
Sunny Goyal75b0f552015-05-20 21:57:06 -07002893 /**
2894 * Called when the icons for packages have been updated in the icon cache.
2895 */
2896 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2897 final Callbacks callbacks = getCallback();
2898 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2899 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2900
2901 // If any package icon has changed (app was updated while launcher was dead),
2902 // update the corresponding shortcuts.
2903 synchronized (sBgLock) {
2904 for (ItemInfo info : sBgItemsIdMap) {
2905 if (info instanceof ShortcutInfo && user.equals(info.user)
2906 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2907 ShortcutInfo si = (ShortcutInfo) info;
2908 ComponentName cn = si.getTargetComponent();
2909 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2910 si.updateIcon(mIconCache);
2911 updatedShortcuts.add(si);
2912 }
2913 }
2914 }
2915 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2916 }
2917
2918 if (!updatedShortcuts.isEmpty()) {
2919 final UserHandleCompat userFinal = user;
2920 mHandler.post(new Runnable() {
2921
2922 public void run() {
2923 Callbacks cb = getCallback();
2924 if (cb != null && callbacks == cb) {
2925 cb.bindShortcutsChanged(updatedShortcuts,
2926 new ArrayList<ShortcutInfo>(), userFinal);
2927 }
2928 }
2929 });
2930 }
2931
2932 if (!updatedApps.isEmpty()) {
2933 mHandler.post(new Runnable() {
2934
2935 public void run() {
2936 Callbacks cb = getCallback();
2937 if (cb != null && callbacks == cb) {
2938 cb.bindAppsUpdated(updatedApps);
2939 }
2940 }
2941 });
2942 }
2943 }
2944
Joe Onorato36115782010-06-17 13:28:48 -04002945 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002946 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002947 }
2948
Adam Cohen091440a2015-03-18 14:16:05 -07002949 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002950
2951 @Override
2952 public void onReceive(Context context, Intent intent) {
2953 synchronized (sBgLock) {
2954 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2955 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002956 final PackageManager manager = context.getPackageManager();
2957 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2958 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2960 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002961 packagesRemoved.clear();
2962 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002963 for (String pkg : entry.getValue()) {
2964 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002965 boolean packageOnSdcard = launcherApps.isAppEnabled(
2966 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2967 if (packageOnSdcard) {
2968 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2969 packagesUnavailable.add(pkg);
2970 } else {
2971 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2972 packagesRemoved.add(pkg);
2973 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002974 }
2975 }
2976 if (!packagesRemoved.isEmpty()) {
2977 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2978 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2979 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002980 if (!packagesUnavailable.isEmpty()) {
2981 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2982 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2983 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002984 }
Sunny Goyal34942622014-08-29 17:20:55 -07002985 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002986 }
2987 }
2988 }
2989
Joe Onorato36115782010-06-17 13:28:48 -04002990 private class PackageUpdatedTask implements Runnable {
2991 int mOp;
2992 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002993 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002994
2995 public static final int OP_NONE = 0;
2996 public static final int OP_ADD = 1;
2997 public static final int OP_UPDATE = 2;
2998 public static final int OP_REMOVE = 3; // uninstlled
2999 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3000
3001
Kenny Guyed131872014-04-30 03:02:21 +01003002 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003003 mOp = op;
3004 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003005 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003006 }
3007
3008 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003009 if (!mHasLoaderCompletedOnce) {
3010 // Loader has not yet run.
3011 return;
3012 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003013 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003014
3015 final String[] packages = mPackages;
3016 final int N = packages.length;
3017 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003018 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003019 for (int i=0; i<N; i++) {
3020 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003021 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003022 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003023 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003025 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3026 if (heuristic != null) {
3027 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003028 }
Joe Onorato36115782010-06-17 13:28:48 -04003029 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 }
Joe Onorato36115782010-06-17 13:28:48 -04003031 case OP_UPDATE:
3032 for (int i=0; i<N; i++) {
3033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003034 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003035 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003036 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
3038 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003039 case OP_REMOVE: {
3040 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3041 if (heuristic != null) {
3042 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003043 }
Joe Onorato36115782010-06-17 13:28:48 -04003044 for (int i=0; i<N; i++) {
3045 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003046 mIconCache.removeIconsForPkg(packages[i], mUser);
3047 }
3048 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003049 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003050 case OP_UNAVAILABLE:
3051 for (int i=0; i<N; i++) {
3052 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3053 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003054 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
3056 break;
3057 }
3058
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003059 ArrayList<AppInfo> added = null;
3060 ArrayList<AppInfo> modified = null;
3061 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003062
Adam Cohen487f7dd2012-06-28 18:12:10 -07003063 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003065 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003066 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003067 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003068 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003069 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003070 }
Winson Chung5d55f332012-07-16 20:45:03 -07003071 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003072 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003073 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003074 }
3075
Sunny Goyale0f58d72014-11-10 18:05:31 -08003076 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003077 if (callbacks == null) {
3078 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3079 return;
3080 }
3081
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3083 new HashMap<ComponentName, AppInfo>();
3084
Joe Onorato36115782010-06-17 13:28:48 -04003085 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003086 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 for (AppInfo ai : added) {
3088 addedOrUpdatedApps.put(ai.componentName, ai);
3089 }
Joe Onorato36115782010-06-17 13:28:48 -04003090 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003091
Joe Onorato36115782010-06-17 13:28:48 -04003092 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 for (AppInfo ai : modified) {
3095 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003096 }
3097
Joe Onorato36115782010-06-17 13:28:48 -04003098 mHandler.post(new Runnable() {
3099 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003100 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003101 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003102 callbacks.bindAppsUpdated(modifiedFinal);
3103 }
3104 }
3105 });
3106 }
Winson Chung83892cc2013-05-01 16:53:33 -07003107
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3111 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3113
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003116 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3118 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 boolean infoUpdated = false;
3120 boolean shortcutUpdated = false;
3121
3122 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003125 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3126 si.iconResource.resourceName, mIconCache, context);
3127 if (icon != null) {
3128 si.setIcon(icon);
3129 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003131 }
3132 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133
3134 ComponentName cn = si.getTargetComponent();
3135 if (cn != null && packageSet.contains(cn.getPackageName())) {
3136 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3137
3138 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3140 // Auto install icon
3141 PackageManager pm = context.getPackageManager();
3142 ResolveInfo matched = pm.resolveActivity(
3143 new Intent(Intent.ACTION_MAIN)
3144 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3145 PackageManager.MATCH_DEFAULT_ONLY);
3146 if (matched == null) {
3147 // Try to find the best match activity.
3148 Intent intent = pm.getLaunchIntentForPackage(
3149 cn.getPackageName());
3150 if (intent != null) {
3151 cn = intent.getComponent();
3152 appInfo = addedOrUpdatedApps.get(cn);
3153 }
3154
3155 if ((intent == null) || (appInfo == null)) {
3156 removedShortcuts.add(si);
3157 continue;
3158 }
3159 si.promisedIntent = intent;
3160 }
3161 }
3162
3163 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003164 if (appInfo != null) {
3165 si.flags = appInfo.flags;
3166 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167
Sunny Goyal756adbc2015-04-16 15:20:51 -07003168 si.intent = si.promisedIntent;
3169 si.promisedIntent = null;
3170 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 infoUpdated = true;
3172 si.updateIcon(mIconCache);
3173 }
3174
3175 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3176 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3177 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003178 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179 si.contentDescription = appInfo.contentDescription;
3180 infoUpdated = true;
3181 }
3182
3183 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3184 // Since package was just updated, the target must be available now.
3185 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3186 shortcutUpdated = true;
3187 }
3188 }
3189
3190 if (infoUpdated || shortcutUpdated) {
3191 updatedShortcuts.add(si);
3192 }
3193 if (infoUpdated) {
3194 updateItemInDatabase(context, si);
3195 }
3196 } else if (info instanceof LauncherAppWidgetInfo) {
3197 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3198 if (mUser.equals(widgetInfo.user)
3199 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3200 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3201 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3202 widgets.add(widgetInfo);
3203 updateItemInDatabase(context, widgetInfo);
3204 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 }
3206 }
3207 }
3208
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3210 mHandler.post(new Runnable() {
3211
3212 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003213 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003214 if (callbacks == cb && cb != null) {
3215 callbacks.bindShortcutsChanged(
3216 updatedShortcuts, removedShortcuts, mUser);
3217 }
3218 }
3219 });
3220 if (!removedShortcuts.isEmpty()) {
3221 deleteItemsFromDatabase(context, removedShortcuts);
3222 }
3223 }
3224 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003225 mHandler.post(new Runnable() {
3226 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003227 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003228 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003229 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003230 }
3231 }
3232 });
3233 }
3234 }
3235
Winson Chungdf95eb12013-10-16 14:57:07 -07003236 final ArrayList<String> removedPackageNames =
3237 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003238 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 // Mark all packages in the broadcast to be removed
3240 removedPackageNames.addAll(Arrays.asList(packages));
3241 } else if (mOp == OP_UPDATE) {
3242 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003243 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003244 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003246 }
3247 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003249
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003251 final int removeReason;
3252 if (mOp == OP_UNAVAILABLE) {
3253 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3254 } else {
3255 // Remove all the components associated with this package
3256 for (String pn : removedPackageNames) {
3257 deletePackageFromDatabase(context, pn, mUser);
3258 }
3259 // Remove all the specific components
3260 for (AppInfo a : removedApps) {
3261 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3262 deleteItemsFromDatabase(context, infos);
3263 }
3264 removeReason = 0;
3265 }
3266
Winson Chungdf95eb12013-10-16 14:57:07 -07003267 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003268 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003269 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003270 mHandler.post(new Runnable() {
3271 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003272 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003273 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003274 callbacks.bindComponentsRemoved(
3275 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003276 }
3277 }
3278 });
Joe Onoratobe386092009-11-17 17:32:16 -08003279 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003280
3281 // onProvidersChanged method (API >= 17) already refreshed the widget list
3282 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3283
Adam Cohen4caf2982013-08-20 18:54:31 -07003284 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003285 mHandler.post(new Runnable() {
3286 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003287 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003288 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003289 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003290 }
3291 }
3292 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 }
3294 }
3295
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003296 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3297 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003298 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003299 if (sBgWidgetProviders == null || refresh) {
3300 sBgWidgetProviders = new HashMap<>();
3301 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3302 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003303
Robin Lee26ace122015-03-16 19:41:43 +00003304 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3305 for (AppWidgetProviderInfo pInfo : widgets) {
3306 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3307 UserHandleCompat user = wm.getUser(info);
3308 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3309 }
3310
3311 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3312 for (CustomAppWidget widget : customWidgets) {
3313 info = new LauncherAppWidgetProviderInfo(context, widget);
3314 UserHandleCompat user = wm.getUser(info);
3315 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3316 }
Adam Cohen59400422014-03-05 18:07:04 -08003317 }
Adam Cohen59400422014-03-05 18:07:04 -08003318 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3319 }
3320 }
3321
Robin Lee26ace122015-03-16 19:41:43 +00003322 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3323 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003324 synchronized (sBgLock) {
3325 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003326 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003327 }
Robin Lee26ace122015-03-16 19:41:43 +00003328 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003329 }
3330 }
3331
Hyunyoung Song227239e2015-05-04 18:17:35 -07003332 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3333 final boolean refresh) {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003334 runOnWorkerThread(new Runnable(){
3335 @Override
3336 public void run() {
Hyunyoung Song227239e2015-05-04 18:17:35 -07003337 final ArrayList<Object> list = getWidgetsAndShortcuts(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003338 mHandler.post(new Runnable() {
3339 @Override
3340 public void run() {
3341 Callbacks cb = getCallback();
3342 if (callbacks == cb && cb != null) {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003343 callbacks.bindAllPackages(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003344 }
3345 }
3346 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003347 // update the Widget entries inside DB on the worker thread.
3348 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(list);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003349 }
3350 });
3351 }
3352
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003353 /**
3354 * Returns a list of ResolveInfos/AppWidgetInfos.
3355 *
3356 * @see #loadAndBindWidgetsAndShortcuts
3357 */
3358 private ArrayList<Object> getWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003359 PackageManager packageManager = context.getPackageManager();
3360 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003361 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003362 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3363 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Michael Jurkac402cd92013-05-20 15:49:32 +02003364 return widgetsAndShortcuts;
3365 }
3366
Adam Cohen091440a2015-03-18 14:16:05 -07003367 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003368 UserHandleCompat user) {
3369 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3370 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003371 }
Adam Cohen556f6132014-01-15 15:18:08 -08003372
Kenny Guyed131872014-04-30 03:02:21 +01003373 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3374 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003375 if (cn == null) {
3376 return false;
3377 }
Kenny Guyed131872014-04-30 03:02:21 +01003378 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3379 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003380 return false;
3381 }
Kenny Guyed131872014-04-30 03:02:21 +01003382 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003383 }
3384
Adam Cohena28b78e2014-05-20 17:03:04 -07003385 public static boolean isValidPackage(Context context, String packageName,
3386 UserHandleCompat user) {
3387 if (packageName == null) {
3388 return false;
3389 }
3390 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3391 return launcherApps.isPackageEnabledForProfile(packageName, user);
3392 }
3393
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003395 * Make an ShortcutInfo object for a restored application or shortcut item that points
3396 * to a package that is not yet installed on the system.
3397 */
Sunny Goyal34942622014-08-29 17:20:55 -07003398 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003399 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003400 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003401 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003402 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003403
3404 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3405 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3406 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003407 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003408 }
Sunny Goyal34942622014-08-29 17:20:55 -07003409 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3410 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003411 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003412 }
Sunny Goyal34942622014-08-29 17:20:55 -07003413 } else {
3414 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3415 }
3416
Winson Chung82b016c2015-05-08 17:00:10 -07003417 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003418 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003419 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003420 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003421 return info;
3422 }
3423
3424 /**
3425 * Make an Intent object for a restored application or shortcut item that points
3426 * to the market page for the item.
3427 */
Adam Cohen091440a2015-03-18 14:16:05 -07003428 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003429 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003430 return getMarketIntent(componentName.getPackageName());
3431 }
3432
3433 static Intent getMarketIntent(String packageName) {
3434 return new Intent(Intent.ACTION_VIEW)
3435 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003436 .scheme("market")
3437 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003438 .appendQueryParameter("id", packageName)
3439 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 }
3441
3442 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003443 * Make an ShortcutInfo object for a shortcut that is an application.
3444 *
3445 * If c is not null, then it will be used to fill in missing data like the title and icon.
3446 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003447 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003448 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003449 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003450 if (user == null) {
3451 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003452 return null;
3453 }
3454
Kenny Guyed131872014-04-30 03:02:21 +01003455 ComponentName componentName = intent.getComponent();
3456 if (componentName == null) {
3457 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3458 return null;
3459 }
3460
3461 Intent newIntent = new Intent(intent.getAction(), null);
3462 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3463 newIntent.setComponent(componentName);
3464 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003465 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003466 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3467 return null;
3468 }
3469
3470 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003471 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003472 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3473 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3474 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003475 }
3476
Joe Onorato56d82912010-03-07 14:32:10 -05003477 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003478 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003479 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003480 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003481
Joe Onorato56d82912010-03-07 14:32:10 -05003482 // fall back to the class name of the activity
3483 if (info.title == null) {
3484 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003485 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003486
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003488 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003489 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003490 if (lai != null) {
3491 info.flags = AppInfo.initFlags(lai);
3492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003494 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003495
Sunny Goyale2df0622015-04-24 11:27:00 -07003496 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003497 ItemInfoFilter f) {
3498 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3499 for (ItemInfo i : infos) {
3500 if (i instanceof ShortcutInfo) {
3501 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003502 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003503 if (cn != null && f.filterItem(null, info, cn)) {
3504 filtered.add(info);
3505 }
3506 } else if (i instanceof FolderInfo) {
3507 FolderInfo info = (FolderInfo) i;
3508 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003509 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003510 if (cn != null && f.filterItem(info, s, cn)) {
3511 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003512 }
3513 }
Winson Chung64359a52013-07-08 17:17:08 -07003514 } else if (i instanceof LauncherAppWidgetInfo) {
3515 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3516 ComponentName cn = info.providerName;
3517 if (cn != null && f.filterItem(null, info, cn)) {
3518 filtered.add(info);
3519 }
Winson Chung8a435102012-08-30 17:16:53 -07003520 }
3521 }
Winson Chung64359a52013-07-08 17:17:08 -07003522 return new ArrayList<ItemInfo>(filtered);
3523 }
3524
Adam Cohen091440a2015-03-18 14:16:05 -07003525 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003526 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003527 ItemInfoFilter filter = new ItemInfoFilter() {
3528 @Override
3529 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003530 if (info.user == null) {
3531 return cn.equals(cname);
3532 } else {
3533 return cn.equals(cname) && info.user.equals(user);
3534 }
Winson Chung64359a52013-07-08 17:17:08 -07003535 }
3536 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003537 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003538 }
3539
Sunny Goyal1a745e82014-10-02 15:58:31 -07003540 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003541 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003542 */
Adam Cohen091440a2015-03-18 14:16:05 -07003543 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003544 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3545 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003546
Joe Onorato56d82912010-03-07 14:32:10 -05003547 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003548 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003549 // Non-app shortcuts are only supported for current user.
3550 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003553 // TODO: If there's an explicit component and we can't install that, delete it.
3554
Winson Chung82b016c2015-05-08 17:00:10 -07003555 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003556
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 int iconType = c.getInt(iconTypeIndex);
3558 switch (iconType) {
3559 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3560 String packageName = c.getString(iconPackageIndex);
3561 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003562 info.customIcon = false;
3563 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003564 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003565 // the db
3566 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003567 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003568 }
3569 // the fallback icon
3570 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003571 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003572 info.usingFallbackIcon = true;
3573 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 break;
3575 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003576 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003577 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003578 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003579 info.customIcon = false;
3580 info.usingFallbackIcon = true;
3581 } else {
3582 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 break;
3585 default:
Kenny Guyed131872014-04-30 03:02:21 +01003586 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003587 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003588 info.customIcon = false;
3589 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003590 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003591 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 return info;
3593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594
Sunny Goyal2350bc92014-10-14 16:42:54 -07003595 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003596 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3597 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3598 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3599
Adam Cohend9198822011-11-22 16:42:47 -08003600 if (intent == null) {
3601 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3602 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3603 return null;
3604 }
3605
Joe Onorato0589f0f2010-02-08 13:44:00 -08003606 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 boolean customIcon = false;
3608 ShortcutIconResource iconResource = null;
3609
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003610 if (bitmap instanceof Bitmap) {
3611 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003612 customIcon = true;
3613 } else {
3614 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003615 if (extra instanceof ShortcutIconResource) {
3616 iconResource = (ShortcutIconResource) extra;
3617 icon = Utilities.createIconBitmap(iconResource.packageName,
3618 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003619 }
3620 }
3621
Michael Jurkac9d95c52011-08-29 14:03:34 -07003622 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003623
Kenny Guyed131872014-04-30 03:02:21 +01003624 // Only support intents for current user for now. Intents sent from other
3625 // users wouldn't get here without intent forwarding anyway.
3626 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003627 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003628 icon = mIconCache.getDefaultIcon(info.user);
3629 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003630 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003631 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003632
Winson Chung82b016c2015-05-08 17:00:10 -07003633 info.title = Utilities.trim(name);
3634 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003635 info.intent = intent;
3636 info.customIcon = customIcon;
3637 info.iconResource = iconResource;
3638
3639 return info;
3640 }
3641
Joe Onorato9c1289c2009-08-17 11:03:03 -04003642 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003643 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 * or make a new one.
3645 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003646 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 // See if a placeholder was created for us already
3648 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003649 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003651 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 folders.put(id, folderInfo);
3653 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003654 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003655 }
3656
Joe Onoratobe386092009-11-17 17:32:16 -08003657
Sunny Goyal651077b2014-06-30 14:15:31 -07003658 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3659 return (provider != null) && (provider.provider != null)
3660 && (provider.provider.getPackageName() != null);
3661 }
3662
Joe Onoratobe386092009-11-17 17:32:16 -08003663 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003664 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003665 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3666 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3667 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3668 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003669 if (mLoaderTask != null) {
3670 mLoaderTask.dumpState();
3671 } else {
3672 Log.d(TAG, "mLoaderTask=null");
3673 }
Joe Onoratobe386092009-11-17 17:32:16 -08003674 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003675
3676 public Callbacks getCallback() {
3677 return mCallbacks != null ? mCallbacks.get() : null;
3678 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003679
3680 /**
3681 * @return {@link FolderInfo} if its already loaded.
3682 */
3683 public FolderInfo findFolderById(Long folderId) {
3684 synchronized (sBgLock) {
3685 return sBgFolders.get(folderId);
3686 }
3687 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003688
3689 /**
3690 * @return the looper for the worker thread which can be used to start background tasks.
3691 */
3692 public static Looper getWorkerLooper() {
3693 return sWorkerThread.getLooper();
3694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003695}