blob: e1425121a9525a8cb1a60abe61a0e6ee97861ef9 [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;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070033import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.res.Resources;
35import android.database.Cursor;
36import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070038import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080039import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040040import android.os.Handler;
41import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070042import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080043import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040045import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070046import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040047import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080048import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080050import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070051import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010052
Sunny Goyalffe83f12014-08-14 17:39:34 -070053import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010054import com.android.launcher3.compat.LauncherActivityInfoCompat;
55import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070056import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070057import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010058import com.android.launcher3.compat.UserHandleCompat;
59import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny 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;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
142 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700152 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700164 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 // sBgWorkspaceScreens is the ordered set of workspace screens.
167 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
168
Adam Cohen59400422014-03-05 18:07:04 -0800169 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000170 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800171
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700172 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700173 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
174 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700175
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176 // </ only access in worker thread >
177
Adam Cohen091440a2015-03-18 14:16:05 -0700178 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Reena Lee99a73f32011-10-24 17:27:37 -0700180 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700181
Adam Cohen091440a2015-03-18 14:16:05 -0700182 @Thunk final LauncherAppsCompat mLauncherApps;
183 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700186 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public int getCurrentWorkspaceScreen();
188 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
190 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700191 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700193 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800194 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700197 public void bindAppsAdded(ArrayList<Long> newScreens,
198 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700199 ArrayList<ItemInfo> addAnimated,
200 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
203 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
204 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700205 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700208 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100209 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700212 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 mIconCache = iconCache;
244
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400245 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700246 Configuration config = res.getConfiguration();
247 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100248 mLauncherApps = LauncherAppsCompat.getInstance(context);
249 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 }
251
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
253 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700255 if (sWorkerThread.getThreadId() == Process.myTid()) {
256 // If we are on the worker thread, post onto the main handler
257 mHandler.post(r);
258 } else {
259 r.run();
260 }
261 }
262
263 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
264 * posted on the worker thread handler. */
265 private static void runOnWorkerThread(Runnable r) {
266 if (sWorkerThread.getThreadId() == Process.myTid()) {
267 r.run();
268 } else {
269 // If we are not on the worker thread, then post to the worker handler
270 sWorker.post(r);
271 }
272 }
273
Sunny Goyal756a28a2015-04-23 17:07:55 -0700274 /**
275 * Runs the specified runnable after the loader is complete
276 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700277 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700278 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
279 synchronized (mBindCompleteRunnables) {
280 mBindCompleteRunnables.add(r);
281 }
282 } else {
283 runOnWorkerThread(r);
284 }
285 }
286
Winson Chunge43a1e72014-01-15 10:33:02 -0800287 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
288 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800289 }
290
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 public void setPackageState(final PackageInstallInfo installInfo) {
292 Runnable updateRunnable = new Runnable() {
293
294 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500295 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700296 synchronized (sBgLock) {
297 final HashSet<ItemInfo> updates = new HashSet<>();
298
299 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
300 // Ignore install success events as they are handled by Package add events.
301 return;
302 }
303
Sunny Goyale2df0622015-04-24 11:27:00 -0700304 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700305 if (info instanceof ShortcutInfo) {
306 ShortcutInfo si = (ShortcutInfo) info;
307 ComponentName cn = si.getTargetComponent();
308 if (si.isPromise() && (cn != null)
309 && installInfo.packageName.equals(cn.getPackageName())) {
310 si.setInstallProgress(installInfo.progress);
311
312 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
313 // Mark this info as broken.
314 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
315 }
316 updates.add(si);
317 }
318 }
319 }
320
321 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
322 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
323 widget.installProgress = installInfo.progress;
324 updates.add(widget);
325 }
326 }
327
328 if (!updates.isEmpty()) {
329 // Push changes to the callback.
330 Runnable r = new Runnable() {
331 public void run() {
332 Callbacks callbacks = getCallback();
333 if (callbacks != null) {
334 callbacks.bindRestoreItemsChange(updates);
335 }
336 }
337 };
338 mHandler.post(r);
339 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500340 }
341 }
342 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500344 }
345
Sunny Goyal756adbc2015-04-16 15:20:51 -0700346 /**
347 * Updates the icons and label of all pending icons for the provided package name.
348 */
349 public void updateSessionDisplayInfo(final String packageName) {
350 Runnable updateRunnable = new Runnable() {
351
352 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700353 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700354 synchronized (sBgLock) {
355 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
356 final UserHandleCompat user = UserHandleCompat.myUserHandle();
357
Sunny Goyale2df0622015-04-24 11:27:00 -0700358 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700359 if (info instanceof ShortcutInfo) {
360 ShortcutInfo si = (ShortcutInfo) info;
361 ComponentName cn = si.getTargetComponent();
362 if (si.isPromise() && (cn != null)
363 && packageName.equals(cn.getPackageName())) {
364 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
365 // For auto install apps update the icon as well as label.
366 mIconCache.getTitleAndIcon(si,
367 si.promisedIntent, user,
368 si.shouldUseLowResIcon());
369 } else {
370 // Only update the icon for restored apps.
371 si.updateIcon(mIconCache);
372 }
373 updates.add(si);
374 }
375 }
376 }
377
378 if (!updates.isEmpty()) {
379 // Push changes to the callback.
380 Runnable r = new Runnable() {
381 public void run() {
382 Callbacks callbacks = getCallback();
383 if (callbacks != null) {
384 callbacks.bindShortcutsChanged(updates,
385 new ArrayList<ShortcutInfo>(), user);
386 }
387 }
388 };
389 mHandler.post(r);
390 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700391 }
392 }
393 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700394 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700395 }
396
Adam Cohen76a47a12014-02-05 11:47:43 -0800397 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800398 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800399
400 if (allAppsApps == null) {
401 throw new RuntimeException("allAppsApps must not be null");
402 }
403 if (allAppsApps.isEmpty()) {
404 return;
405 }
406
407 // Process the newly added applications and add them to the database first
408 Runnable r = new Runnable() {
409 public void run() {
410 runOnMainThread(new Runnable() {
411 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800412 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800413 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500414 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800415 }
416 }
417 });
418 }
419 };
420 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700421 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800422
Sunny Goyala9116722015-04-29 13:55:58 -0700423 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 int[] xy, int spanX, int spanY) {
425 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700426 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
427 final int xCount = (int) profile.numColumns;
428 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 boolean[][] occupied = new boolean[xCount][yCount];
430 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700431 for (ItemInfo r : occupiedPos) {
432 int right = r.cellX + r.spanX;
433 int bottom = r.cellY + r.spanY;
434 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
435 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436 occupied[x][y] = true;
437 }
438 }
439 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800440 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700441 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442 }
443
444 /**
445 * Find a position on the screen for the given size or adds a new screen.
446 * @return screenId and the coordinates for the item.
447 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700448 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 ArrayList<Long> workspaceScreens,
451 ArrayList<Long> addedWorkspaceScreensFinal,
452 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700453 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454
Sunny Goyala9116722015-04-29 13:55:58 -0700455 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700456 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700457 synchronized (sBgLock) {
458 for (ItemInfo info : sBgItemsIdMap) {
459 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
460 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
461 if (items == null) {
462 items = new ArrayList<>();
463 screenItems.put(info.screenId, items);
464 }
465 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 }
469
470 // Find appropriate space for the item.
471 long screenId = 0;
472 int[] cordinates = new int[2];
473 boolean found = false;
474
475 int screenCount = workspaceScreens.size();
476 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700477 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800478 if (preferredScreenIndex < screenCount) {
479 screenId = workspaceScreens.get(preferredScreenIndex);
480 found = findNextAvailableIconSpaceInScreen(
481 screenItems.get(screenId), cordinates, spanX, spanY);
482 }
483
484 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700485 // Search on any of the screens starting from the first screen.
486 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487 screenId = workspaceScreens.get(screen);
488 if (findNextAvailableIconSpaceInScreen(
489 screenItems.get(screenId), cordinates, spanX, spanY)) {
490 // We found a space for it
491 found = true;
492 break;
493 }
494 }
495 }
496
497 if (!found) {
498 // Still no position found. Add a new screen to the end.
499 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
500
501 // Save the screen id for binding in the workspace
502 workspaceScreens.add(screenId);
503 addedWorkspaceScreensFinal.add(screenId);
504
505 // If we still can't find an empty space, then God help us all!!!
506 if (!findNextAvailableIconSpaceInScreen(
507 screenItems.get(screenId), cordinates, spanX, spanY)) {
508 throw new RuntimeException("Can't find space to add the item");
509 }
510 }
511 return Pair.create(screenId, cordinates);
512 }
513
514 /**
515 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700517 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700518 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800519 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800520 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700521 return;
Winson Chung997a9232013-07-24 15:33:46 -0700522 }
Winson Chung64359a52013-07-08 17:17:08 -0700523 // Process the newly added applications and add them to the database first
524 Runnable r = new Runnable() {
525 public void run() {
526 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
527 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
528
Winson Chung76828c82013-08-19 15:43:29 -0700529 // Get the list of workspace screens. We need to append to this list and
530 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
531 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800532 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700533 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700535 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700537 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700539 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 }
Winson Chung76828c82013-08-19 15:43:29 -0700541
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800542 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700543 Pair<Long, int[]> coords = findSpaceForItem(context,
544 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800545 1, 1);
546 long screenId = coords.first;
547 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700548
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700549 ItemInfo itemInfo;
550 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
551 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800552 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700553 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700554 } else {
555 throw new RuntimeException("Unexpected info type");
556 }
Winson Chung94d67682013-09-25 16:29:40 -0700557
Winson Chung64359a52013-07-08 17:17:08 -0700558 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700559 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700560 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700561 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700562 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700563 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700564 }
565 }
566
Winson Chung76828c82013-08-19 15:43:29 -0700567 // Update the workspace screens
568 updateWorkspaceScreenOrder(context, workspaceScreens);
569
Adam Cohen76a47a12014-02-05 11:47:43 -0800570 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700571 runOnMainThread(new Runnable() {
572 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800573 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700574 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700575 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
576 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700577 if (!addedShortcutsFinal.isEmpty()) {
578 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
579 long lastScreenId = info.screenId;
580 for (ItemInfo i : addedShortcutsFinal) {
581 if (i.screenId == lastScreenId) {
582 addAnimated.add(i);
583 } else {
584 addNotAnimated.add(i);
585 }
Winson Chung997a9232013-07-24 15:33:46 -0700586 }
587 }
Winson Chungd64d1762013-08-20 14:37:16 -0700588 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800589 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700590 }
Winson Chung64359a52013-07-08 17:17:08 -0700591 }
Winson Chung997a9232013-07-24 15:33:46 -0700592 });
593 }
Winson Chung64359a52013-07-08 17:17:08 -0700594 }
595 };
596 runOnWorkerThread(r);
597 }
598
Sunny Goyald33860f2015-04-23 16:02:20 -0700599 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700600 if (sWorkerThread.getThreadId() == Process.myTid()) {
601 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
602 "main thread");
603 }
604
605 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400606 synchronized (mDeferredBindRunnables) {
607 mDeferredBindRunnables.clear();
608 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700609
610 // Remove any queued UI runnables
611 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700612 // Unbind all the workspace items
613 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700614 }
615
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700616 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700617 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700618 // Ensure that we don't use the same workspace items data structure on the main thread
619 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700620 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700621 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700622 tmpItems.addAll(sBgWorkspaceItems);
623 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 }
625 Runnable r = new Runnable() {
626 @Override
627 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700628 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700629 item.unbind();
630 }
631 }
632 };
633 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700634 }
635
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 /**
637 * Adds an item to the DB if it was not created previously, or move it to a new
638 * <container, screen, cellX, cellY>
639 */
640 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 if (item.container == ItemInfo.NO_ID) {
643 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700644 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 } else {
646 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
649 }
650
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700651 static void checkItemInfoLocked(
652 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
653 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
654 if (modelItem != null && item != modelItem) {
655 // check all the data is consistent
656 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
657 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
658 ShortcutInfo shortcut = (ShortcutInfo) item;
659 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
660 modelShortcut.intent.filterEquals(shortcut.intent) &&
661 modelShortcut.id == shortcut.id &&
662 modelShortcut.itemType == shortcut.itemType &&
663 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700664 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700665 modelShortcut.cellX == shortcut.cellX &&
666 modelShortcut.cellY == shortcut.cellY &&
667 modelShortcut.spanX == shortcut.spanX &&
668 modelShortcut.spanY == shortcut.spanY &&
669 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
670 (modelShortcut.dropPos != null &&
671 shortcut.dropPos != null &&
672 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
673 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
674 // For all intents and purposes, this is the same object
675 return;
676 }
677 }
678
679 // the modelItem needs to match up perfectly with item if our model is
680 // to be consistent with the database-- for now, just require
681 // modelItem == item or the equality check above
682 String msg = "item: " + ((item != null) ? item.toString() : "null") +
683 "modelItem: " +
684 ((modelItem != null) ? modelItem.toString() : "null") +
685 "Error: ItemInfo passed to checkItemInfo doesn't match original";
686 RuntimeException e = new RuntimeException(msg);
687 if (stackTrace != null) {
688 e.setStackTrace(stackTrace);
689 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800690 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700691 }
692 }
693
Michael Jurka816474f2012-06-25 14:49:02 -0700694 static void checkItemInfo(final ItemInfo item) {
695 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
696 final long itemId = item.id;
697 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700698 public void run() {
699 synchronized (sBgLock) {
700 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700701 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700702 }
703 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700704 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700705 }
706
Michael Jurkac9d95c52011-08-29 14:03:34 -0700707 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
708 final ItemInfo item, final String callingFunction) {
709 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700710 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700711 final ContentResolver cr = context.getContentResolver();
712
Adam Cohen487f7dd2012-06-28 18:12:10 -0700713 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700714 Runnable r = new Runnable() {
715 public void run() {
716 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700717 updateItemArrays(item, itemId, stackTrace);
718 }
719 };
720 runOnWorkerThread(r);
721 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
724 final ArrayList<ItemInfo> items, final String callingFunction) {
725 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700726
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700727 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
728 Runnable r = new Runnable() {
729 public void run() {
730 ArrayList<ContentProviderOperation> ops =
731 new ArrayList<ContentProviderOperation>();
732 int count = items.size();
733 for (int i = 0; i < count; i++) {
734 ItemInfo item = items.get(i);
735 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700736 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700738
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700739 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
740 updateItemArrays(item, itemId, stackTrace);
741
742 }
743 try {
744 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
745 } catch (Exception e) {
746 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700747 }
748 }
749 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700750 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700751 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700752
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
754 // Lock on mBgLock *after* the db operation
755 synchronized (sBgLock) {
756 checkItemInfoLocked(itemId, item, stackTrace);
757
758 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
759 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
760 // Item is in a folder, make sure this folder exists
761 if (!sBgFolders.containsKey(item.container)) {
762 // An items container is being set to a that of an item which is not in
763 // the list of Folders.
764 String msg = "item: " + item + " container being set to: " +
765 item.container + ", not in the list of folders";
766 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 }
768 }
769
770 // Items are added/removed from the corresponding FolderInfo elsewhere, such
771 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
772 // that are on the desktop, as appropriate
773 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800774 if (modelItem != null &&
775 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
776 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700777 switch (modelItem.itemType) {
778 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
779 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
780 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
781 if (!sBgWorkspaceItems.contains(modelItem)) {
782 sBgWorkspaceItems.add(modelItem);
783 }
784 break;
785 default:
786 break;
787 }
788 } else {
789 sBgWorkspaceItems.remove(modelItem);
790 }
791 }
792 }
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700796 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700797 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 item.cellX = cellX;
801 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700802
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 // We store hotseat items in canonical form which is this orientation invariant position
804 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700805 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811
812 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700814 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
815 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800816 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818
Michael Jurkac9d95c52011-08-29 14:03:34 -0700819 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700820 }
821
822 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
824 * cellX, cellY have already been updated on the ItemInfos.
825 */
826 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
827 final long container, final int screen) {
828
829 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
830 int count = items.size();
831
832 for (int i = 0; i < count; i++) {
833 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700834 item.container = container;
835
836 // We store hotseat items in canonical form which is this orientation invariant position
837 // in the hotseat
838 if (context instanceof Launcher && screen < 0 &&
839 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700841 item.cellY);
842 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700844 }
845
846 final ContentValues values = new ContentValues();
847 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
848 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
849 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800850 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700852
853 contentValues.add(values);
854 }
855 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
856 }
857
858 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800860 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700863 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800864 item.cellX = cellX;
865 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 item.spanX = spanX;
867 item.spanY = spanY;
868
869 // We store hotseat items in canonical form which is this orientation invariant position
870 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700871 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700872 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700874 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700876 }
Adam Cohend4844c32011-02-18 19:25:06 -0800877
Adam Cohend4844c32011-02-18 19:25:06 -0800878 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800879 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700880 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
881 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800882 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700883 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
884 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800886
Michael Jurka816474f2012-06-25 14:49:02 -0700887 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700888 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700889
890 /**
891 * Update an item to the database in a specified container.
892 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700893 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700894 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100895 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700896 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800897 }
898
Sunny Goyal756a28a2015-04-23 17:07:55 -0700899 private void assertWorkspaceLoaded() {
900 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
901 throw new RuntimeException("Trying to add shortcut while loader is running");
902 }
903 }
904
Adam Cohend4844c32011-02-18 19:25:06 -0800905 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700906 * Returns true if the shortcuts already exists on the workspace. This must be called after
907 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700909 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
910 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700911 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700912 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700913 if (intent.getComponent() != null) {
914 // If component is not null, an intent with null package will produce
915 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700917 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700918 intentWithPkg = intent.toUri(0);
919 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700920 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700921 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
922 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700923 }
924 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700925 intentWithPkg = intent.toUri(0);
926 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700928 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700929
930 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700931 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700932 if (item instanceof ShortcutInfo) {
933 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700934 if (info.getIntent() != null && info.user.equals(user)) {
935 String s = info.getIntent().toUri(0);
936 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
937 return true;
938 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700939 }
940 }
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700943 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700944 }
945
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 /**
947 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
948 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700949 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 final ContentResolver cr = context.getContentResolver();
951 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
952 "_id=? and (itemType=? or itemType=?)",
953 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700954 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700955
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 try {
957 if (c.moveToFirst()) {
958 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
959 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
960 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
961 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
962 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
963 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700964 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 FolderInfo folderInfo = null;
967 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700968 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
969 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700971 }
972
Sunny Goyala508e4f2015-05-21 09:33:57 -0700973 // Do not trim the folder label, as is was set by the user.
974 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 folderInfo.id = id;
976 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700978 folderInfo.cellX = c.getInt(cellXIndex);
979 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700980 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981
982 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 } finally {
985 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700986 }
987
988 return null;
989 }
990
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 /**
992 * Add an item to the database in a specified container. Sets the container, screen, cellX and
993 * cellY fields of the item. Also assigns an ID to the item.
994 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700995 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700996 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 item.cellX = cellX;
999 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 // We store hotseat items in canonical form which is this orientation invariant position
1001 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
1009 final ContentValues values = new ContentValues();
1010 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001011 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Michael Jurka414300a2013-08-27 15:42:35 +02001013 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001015
Jason Monk8e19cf22014-03-20 15:06:57 -04001016 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001017 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001018 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001019 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001022 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001023 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 sBgItemsIdMap.put(item.id, item);
1025 switch (item.itemType) {
1026 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1027 sBgFolders.put(item.id, (FolderInfo) item);
1028 // Fall through
1029 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1030 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1031 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1032 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1033 sBgWorkspaceItems.add(item);
1034 } else {
1035 if (!sBgFolders.containsKey(item.container)) {
1036 // Adding an item to a folder that doesn't exist.
1037 String msg = "adding item: " + item + " to a folder that " +
1038 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001039 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001041 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 break;
1043 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1044 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1045 break;
1046 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001047 }
1048 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001049 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001051 }
1052
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001054 * Creates a new unique child id, for a given cell span across all layouts.
1055 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001056 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001060 }
1061
Sunny Goyal34942622014-08-29 17:20:55 -07001062 private static ArrayList<ItemInfo> getItemsByPackageName(
1063 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001064 ItemInfoFilter filter = new ItemInfoFilter() {
1065 @Override
1066 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1067 return cn.getPackageName().equals(pn) && info.user.equals(user);
1068 }
1069 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001070 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001071 }
1072
1073 /**
1074 * Removes all the items from the database corresponding to the specified package.
1075 */
1076 static void deletePackageFromDatabase(Context context, final String pn,
1077 final UserHandleCompat user) {
1078 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001079 }
1080
1081 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001082 * Removes the specified item from the database
1083 * @param context
1084 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001086 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001087 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1088 items.add(item);
1089 deleteItemsFromDatabase(context, items);
1090 }
1091
1092 /**
1093 * Removes the specified items from the database
1094 * @param context
1095 * @param item
1096 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001097 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001099 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001100 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001101 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001102 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001104
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001105 // Lock on mBgLock *after* the db operation
1106 synchronized (sBgLock) {
1107 switch (item.itemType) {
1108 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1109 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001110 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001111 if (info.container == item.id) {
1112 // We are deleting a folder which still contains items that
1113 // think they are contained by that folder.
1114 String msg = "deleting a folder (" + item + ") which still " +
1115 "contains items (" + info + ")";
1116 Log.e(TAG, msg);
1117 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001119 sBgWorkspaceItems.remove(item);
1120 break;
1121 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1122 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1123 sBgWorkspaceItems.remove(item);
1124 break;
1125 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1126 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1127 break;
1128 }
1129 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001131 }
1132 }
Michael Jurka83df1882011-08-31 20:59:26 -07001133 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001134 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 }
1136
1137 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001138 * Update the order of the workspace screens in the database. The array list contains
1139 * a list of screen ids in the order that they should appear.
1140 */
Winson Chungc9168342013-06-26 14:54:55 -07001141 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001142 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 final ContentResolver cr = context.getContentResolver();
1144 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1145
1146 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001147 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 while (iter.hasNext()) {
1149 long id = iter.next();
1150 if (id < 0) {
1151 iter.remove();
1152 }
1153 }
1154
1155 Runnable r = new Runnable() {
1156 @Override
1157 public void run() {
Yura085c8532014-02-11 15:15:29 +00001158 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001159 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001160 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001161 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 for (int i = 0; i < count; i++) {
1163 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001164 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001165 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1166 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001167 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 }
Yura085c8532014-02-11 15:15:29 +00001169
1170 try {
1171 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1172 } catch (Exception ex) {
1173 throw new RuntimeException(ex);
1174 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001175
Winson Chungba9c37f2013-08-30 14:11:37 -07001176 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001177 sBgWorkspaceScreens.clear();
1178 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001179 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001180 }
1181 };
1182 runOnWorkerThread(r);
1183 }
1184
1185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 * Remove the contents of the specified folder from the database
1187 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001188 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 final ContentResolver cr = context.getContentResolver();
1190
Michael Jurkac9d95c52011-08-29 14:03:34 -07001191 Runnable r = new Runnable() {
1192 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001193 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001195 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 sBgItemsIdMap.remove(info.id);
1197 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 sBgWorkspaceItems.remove(info);
1199 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001200
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001201 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001202 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001204 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 for (ItemInfo childInfo : info.contents) {
1206 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001208 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001209 }
1210 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001211 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
1213
1214 /**
1215 * Set this as the current Launcher activity object for the loader.
1216 */
1217 public void initialize(Callbacks callbacks) {
1218 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001219 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1220 // workspace to prevent leaking Launcher activities on orientation change.
1221 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 mCallbacks = new WeakReference<Callbacks>(callbacks);
1223 }
1224 }
1225
Kenny Guyed131872014-04-30 03:02:21 +01001226 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001227 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001228 int op = PackageUpdatedTask.OP_UPDATE;
1229 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1230 user));
1231 }
1232
1233 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001234 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001235 int op = PackageUpdatedTask.OP_REMOVE;
1236 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1237 user));
1238 }
1239
1240 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001241 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001242 int op = PackageUpdatedTask.OP_ADD;
1243 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1244 user));
1245 }
1246
1247 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001248 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001249 boolean replacing) {
1250 if (!replacing) {
1251 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1252 user));
1253 if (mAppsCanBeOnRemoveableStorage) {
1254 // Only rebind if we support removable storage. It catches the
1255 // case where
1256 // apps on the external sd card need to be reloaded
1257 startLoaderFromBackground();
1258 }
1259 } else {
1260 // If we are replacing then just update the packages in the list
1261 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1262 packageNames, user));
1263 }
1264 }
1265
1266 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001267 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001268 boolean replacing) {
1269 if (!replacing) {
1270 enqueuePackageUpdated(new PackageUpdatedTask(
1271 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1272 user));
1273 }
Kenny Guyed131872014-04-30 03:02:21 +01001274 }
1275
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001276 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1278 * ACTION_PACKAGE_CHANGED.
1279 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001280 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001281 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001282 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001283
Joe Onorato36115782010-06-17 13:28:48 -04001284 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001285 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001286 // If we have changed locale we need to clear out the labels in all apps/workspace.
1287 forceReload();
1288 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1289 // Check if configuration change was an mcc/mnc change which would affect app resources
1290 // and we would need to clear out the labels in all apps/workspace. Same handling as
1291 // above for ACTION_LOCALE_CHANGED
1292 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001293 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001294 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001295 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001296 forceReload();
1297 }
1298 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001299 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001300 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1301 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001302 Callbacks callbacks = getCallback();
1303 if (callbacks != null) {
1304 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001305 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001306 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1307 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1308 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001309 }
1310 }
1311
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001312 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001313 resetLoadedState(true, true);
1314
Reena Lee93f824a2011-09-23 17:20:28 -07001315 // Do this here because if the launcher activity is running it will be restarted.
1316 // If it's not running startLoaderFromBackground will merely tell it that it needs
1317 // to reload.
1318 startLoaderFromBackground();
1319 }
1320
Winson Chungf0c6ae02012-03-21 16:10:31 -07001321 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1322 synchronized (mLock) {
1323 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1324 // mWorkspaceLoaded to true later
1325 stopLoaderLocked();
1326 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1327 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1328 }
1329 }
1330
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001331 /**
1332 * When the launcher is in the background, it's possible for it to miss paired
1333 * configuration changes. So whenever we trigger the loader from the background
1334 * tell the launcher that it needs to re-run the loader when it comes back instead
1335 * of doing it now.
1336 */
1337 public void startLoaderFromBackground() {
1338 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001339 Callbacks callbacks = getCallback();
1340 if (callbacks != null) {
1341 // Only actually run the loader if they're not paused.
1342 if (!callbacks.setLoadOnResume()) {
1343 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001344 }
1345 }
1346 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001347 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001348 }
Joe Onorato36115782010-06-17 13:28:48 -04001349 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001350
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001351 /**
1352 * If there is already a loader task running, tell it to stop.
1353 */
1354 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001355 LoaderTask oldTask = mLoaderTask;
1356 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001357 oldTask.stopLocked();
1358 }
Reena Lee93f824a2011-09-23 17:20:28 -07001359 }
1360
Adam Cohen1a85c582014-09-30 09:48:49 -07001361 public boolean isCurrentCallbacks(Callbacks callbacks) {
1362 return (mCallbacks != null && mCallbacks.get() == callbacks);
1363 }
1364
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001365 public void startLoader(int synchronousBindPage) {
1366 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001367 }
1368
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001369 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001370 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1371 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001372 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 // Clear any deferred bind-runnables from the synchronized load process
1374 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001375 synchronized (mDeferredBindRunnables) {
1376 mDeferredBindRunnables.clear();
1377 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378
Joe Onorato36115782010-06-17 13:28:48 -04001379 // Don't bother to start the thread if we know it's not going to do anything
1380 if (mCallbacks != null && mCallbacks.get() != null) {
1381 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001382 stopLoaderLocked();
1383 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001384 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001385 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1387 } else {
1388 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1389 sWorker.post(mLoaderTask);
1390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 }
1393 }
1394
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001395 void bindRemainingSynchronousPages() {
1396 // Post the remaining side pages to be loaded
1397 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001398 Runnable[] deferredBindRunnables = null;
1399 synchronized (mDeferredBindRunnables) {
1400 deferredBindRunnables = mDeferredBindRunnables.toArray(
1401 new Runnable[mDeferredBindRunnables.size()]);
1402 mDeferredBindRunnables.clear();
1403 }
1404 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001405 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001406 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001408
1409 // Run all the bind complete runnables after workspace is bound.
1410 if (!mBindCompleteRunnables.isEmpty()) {
1411 synchronized (mBindCompleteRunnables) {
1412 for (final Runnable r : mBindCompleteRunnables) {
1413 runOnWorkerThread(r);
1414 }
1415 mBindCompleteRunnables.clear();
1416 }
1417 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 }
1419
Joe Onorato36115782010-06-17 13:28:48 -04001420 public void stopLoader() {
1421 synchronized (mLock) {
1422 if (mLoaderTask != null) {
1423 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 }
1425 }
Joe Onorato36115782010-06-17 13:28:48 -04001426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001428 /**
1429 * Loads the workspace screen ids in an ordered list.
1430 */
Adam Cohen091440a2015-03-18 14:16:05 -07001431 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001432 final ContentResolver contentResolver = context.getContentResolver();
1433 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001434
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001435 // Get screens ordered by rank.
1436 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1437 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1438 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001439 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001440 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001441 while (sc.moveToNext()) {
1442 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001443 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001444 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001445 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1446 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001447 }
1448 }
1449 } finally {
1450 sc.close();
1451 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001452 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001453 }
1454
Michael Jurkac57b7a82011-08-09 22:02:20 -07001455 public boolean isAllAppsLoaded() {
1456 return mAllAppsLoaded;
1457 }
1458
Winson Chung36a62fe2012-05-06 18:04:42 -07001459 boolean isLoadingWorkspace() {
1460 synchronized (mLock) {
1461 if (mLoaderTask != null) {
1462 return mLoaderTask.isLoadingWorkspace();
1463 }
1464 }
1465 return false;
1466 }
1467
Joe Onorato36115782010-06-17 13:28:48 -04001468 /**
1469 * Runnable for the thread that loads the contents of the launcher:
1470 * - workspace icons
1471 * - widgets
1472 * - all apps icons
1473 */
1474 private class LoaderTask implements Runnable {
1475 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001476 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001477 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001478 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001479 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001480
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001481 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001482 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001483 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 }
1485
Winson Chung36a62fe2012-05-06 18:04:42 -07001486 boolean isLoadingWorkspace() {
1487 return mIsLoadingAndBindingWorkspace;
1488 }
1489
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001490 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001491 mIsLoadingAndBindingWorkspace = true;
1492
Joe Onorato36115782010-06-17 13:28:48 -04001493 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001494 if (DEBUG_LOADERS) {
1495 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001497
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001498 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001499 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001500 synchronized (LoaderTask.this) {
1501 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001502 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001503 }
1504 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505 }
1506 }
1507
Joe Onorato36115782010-06-17 13:28:48 -04001508 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001509 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001510 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001511
Joe Onorato36115782010-06-17 13:28:48 -04001512 private void waitForIdle() {
1513 // Wait until the either we're stopped or the other threads are done.
1514 // This way we don't start loading all apps until the workspace has settled
1515 // down.
1516 synchronized (LoaderTask.this) {
1517 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001518
Joe Onorato36115782010-06-17 13:28:48 -04001519 mHandler.postIdle(new Runnable() {
1520 public void run() {
1521 synchronized (LoaderTask.this) {
1522 mLoadAndBindStepFinished = true;
1523 if (DEBUG_LOADERS) {
1524 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001525 }
Joe Onorato36115782010-06-17 13:28:48 -04001526 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001527 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001528 }
Joe Onorato36115782010-06-17 13:28:48 -04001529 });
1530
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001531 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001532 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001533 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1534 // wait no longer than 1sec at a time
1535 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001536 } catch (InterruptedException ex) {
1537 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001538 }
1539 }
Joe Onorato36115782010-06-17 13:28:48 -04001540 if (DEBUG_LOADERS) {
1541 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001542 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001543 + "ms for previous step to finish binding");
1544 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001545 }
Joe Onorato36115782010-06-17 13:28:48 -04001546 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001547
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001548 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001549 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 // Ensure that we have a valid page index to load synchronously
1551 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1552 "valid page index");
1553 }
1554 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1555 // Ensure that we don't try and bind a specified page when the pages have not been
1556 // loaded already (we should load everything asynchronously in that case)
1557 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1558 }
1559 synchronized (mLock) {
1560 if (mIsLoaderTaskRunning) {
1561 // Ensure that we are never running the background loading at this point since
1562 // we also touch the background collections
1563 throw new RuntimeException("Error! Background loading is already running");
1564 }
1565 }
1566
1567 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1568 // data structures, we can't allow any other thread to touch that data, but because
1569 // this call is synchronous, we can get away with not locking).
1570
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001571 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001572 // operations from the previous activity. We need to ensure that all queued operations
1573 // are executed before any synchronous binding work is done.
1574 mHandler.flush();
1575
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001576 // Divide the set of loaded items into those that we are binding synchronously, and
1577 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001578 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1580 // arise from that.
1581 onlyBindAllApps();
1582 }
1583
Joe Onorato36115782010-06-17 13:28:48 -04001584 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001585 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001586 if (mStopped) {
1587 return;
1588 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001589 mIsLoaderTaskRunning = true;
1590 }
Joe Onorato36115782010-06-17 13:28:48 -04001591 // Optimize for end-user experience: if the Launcher is up and // running with the
1592 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1593 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001594 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001595 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001596 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001597
Joe Onorato36115782010-06-17 13:28:48 -04001598 if (mStopped) {
1599 break keep_running;
1600 }
1601
Joe Onorato36115782010-06-17 13:28:48 -04001602 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001603
1604 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001605 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1606 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001607 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608
Joe Onorato36115782010-06-17 13:28:48 -04001609 // Clear out this reference, otherwise we end up holding it until all of the
1610 // callback runnables are done.
1611 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612
Joe Onorato36115782010-06-17 13:28:48 -04001613 synchronized (mLock) {
1614 // If we are still the last one to be scheduled, remove ourselves.
1615 if (mLoaderTask == this) {
1616 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001618 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001619 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001620 }
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
1622
1623 public void stopLocked() {
1624 synchronized (LoaderTask.this) {
1625 mStopped = true;
1626 this.notify();
1627 }
1628 }
1629
1630 /**
1631 * Gets the callbacks object. If we've been stopped, or if the launcher object
1632 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1633 * object that was around when the deferred message was scheduled, and if there's
1634 * a new Callbacks object around then also return null. This will save us from
1635 * calling onto it with data that will be ignored.
1636 */
1637 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1638 synchronized (mLock) {
1639 if (mStopped) {
1640 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001641 }
Joe Onorato36115782010-06-17 13:28:48 -04001642
1643 if (mCallbacks == null) {
1644 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001645 }
Joe Onorato36115782010-06-17 13:28:48 -04001646
1647 final Callbacks callbacks = mCallbacks.get();
1648 if (callbacks != oldCallbacks) {
1649 return null;
1650 }
1651 if (callbacks == null) {
1652 Log.w(TAG, "no mCallbacks");
1653 return null;
1654 }
1655
1656 return callbacks;
1657 }
1658 }
1659
1660 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001661 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001662 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001663 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1664 final int countX = (int) profile.numColumns;
1665 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001666
Adam Cohendcd297f2013-06-18 13:13:40 -07001667 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001668 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001669 // Return early if we detect that an item is under the hotseat button
1670 if (mCallbacks == null ||
1671 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001672 Log.e(TAG, "Error loading shortcut into hotseat " + item
1673 + " into position (" + item.screenId + ":" + item.cellX + ","
1674 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001675 return false;
1676 }
1677
Dan Sandler295ae182013-12-10 16:05:47 -05001678 final ItemInfo[][] hotseatItems =
1679 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1680
Adam Cohen2e6da152015-05-06 11:42:25 -07001681 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001682 Log.e(TAG, "Error loading shortcut " + item
1683 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001684 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001685 + ")");
1686 return false;
1687 }
1688
Dan Sandler295ae182013-12-10 16:05:47 -05001689 if (hotseatItems != null) {
1690 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 Log.e(TAG, "Error loading shortcut into hotseat " + item
1692 + " into position (" + item.screenId + ":" + item.cellX + ","
1693 + item.cellY + ") occupied by "
1694 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1695 [(int) item.screenId][0]);
1696 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001697 } else {
1698 hotseatItems[(int) item.screenId][0] = item;
1699 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001700 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001701 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001702 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 items[(int) item.screenId][0] = item;
1704 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001705 return true;
1706 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001707 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1708 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001709 return true;
1710 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001711
Adam Cohendcd297f2013-06-18 13:13:40 -07001712 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001713 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 occupied.put(item.screenId, items);
1715 }
1716
Dan Sandler295ae182013-12-10 16:05:47 -05001717 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001718 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1719 item.cellX < 0 || item.cellY < 0 ||
1720 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1721 Log.e(TAG, "Error loading shortcut " + item
1722 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1723 + item.cellX + "," + item.cellY
1724 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1725 return false;
1726 }
1727
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001728 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001729 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1730 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001731 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001732 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001734 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001735 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001737 return false;
1738 }
1739 }
1740 }
1741 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1742 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001743 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001744 }
1745 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001746
Joe Onorato36115782010-06-17 13:28:48 -04001747 return true;
1748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749
Winson Chungba9c37f2013-08-30 14:11:37 -07001750 /** Clears all the sBg data structures */
1751 private void clearSBgDataStructures() {
1752 synchronized (sBgLock) {
1753 sBgWorkspaceItems.clear();
1754 sBgAppWidgets.clear();
1755 sBgFolders.clear();
1756 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001757 sBgWorkspaceScreens.clear();
1758 }
1759 }
1760
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001761 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001762 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763
Joe Onorato36115782010-06-17 13:28:48 -04001764 final Context context = mContext;
1765 final ContentResolver contentResolver = context.getContentResolver();
1766 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001767 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001768 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1769 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001770 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001771
Winson Chung892c74d2013-08-22 16:15:50 -07001772 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001773 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1774 int countX = (int) profile.numColumns;
1775 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001776
Dan Sandlerd5024042014-01-09 15:01:33 -05001777 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1778 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1779 LauncherAppState.getLauncherProvider().deleteDatabase();
1780 }
1781
1782 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1783 // append the user's Launcher2 shortcuts
1784 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1785 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1786 } else {
1787 // Make sure the default workspace is loaded
1788 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001789 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001790 }
Adam Cohene25af792013-06-06 23:08:25 -07001791
Winson Chung2abf94d2012-07-18 18:16:38 -07001792 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001793 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001794 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001795 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001796
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001798 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001799 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001800 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001801 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001802
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001803 // +1 for the hotseat (it can be larger than the workspace)
1804 // Load workspace in reverse order to ensure that latest items are loaded first (and
1805 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001806 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 try {
1809 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1810 final int intentIndex = c.getColumnIndexOrThrow
1811 (LauncherSettings.Favorites.INTENT);
1812 final int titleIndex = c.getColumnIndexOrThrow
1813 (LauncherSettings.Favorites.TITLE);
1814 final int iconTypeIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.ICON_TYPE);
1816 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1817 final int iconPackageIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.ICON_PACKAGE);
1819 final int iconResourceIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.ICON_RESOURCE);
1821 final int containerIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.CONTAINER);
1823 final int itemTypeIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.ITEM_TYPE);
1825 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001827 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1828 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 final int screenIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.SCREEN);
1831 final int cellXIndex = c.getColumnIndexOrThrow
1832 (LauncherSettings.Favorites.CELLX);
1833 final int cellYIndex = c.getColumnIndexOrThrow
1834 (LauncherSettings.Favorites.CELLY);
1835 final int spanXIndex = c.getColumnIndexOrThrow
1836 (LauncherSettings.Favorites.SPANX);
1837 final int spanYIndex = c.getColumnIndexOrThrow(
1838 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001839 final int rankIndex = c.getColumnIndexOrThrow(
1840 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001841 final int restoredIndex = c.getColumnIndexOrThrow(
1842 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001843 final int profileIdIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001845 final int optionsIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847
Sunny Goyal7f834d22015-04-21 10:10:23 -07001848 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1849 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1850 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1851 }
1852
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 ShortcutInfo info;
1854 String intentDescription;
1855 LauncherAppWidgetInfo appWidgetInfo;
1856 int container;
1857 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001858 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001860 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 while (!mStopped && c.moveToNext()) {
1863 try {
1864 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001865 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001866 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001867 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 switch (itemType) {
1870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001872 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001874 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001875 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001876 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001877 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001878 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001879 if (user == null) {
1880 // User has been deleted remove the item.
1881 itemsToRemove.add(id);
1882 continue;
1883 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 try {
1885 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001886 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001887 if (cn != null && cn.getPackageName() != null) {
1888 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1889 cn.getPackageName(), user);
1890 boolean validComponent = validPkg &&
1891 launcherApps.isActivityEnabledForProfile(cn, user);
1892
1893 if (validComponent) {
1894 if (restored) {
1895 // no special handling necessary for this item
1896 restoredRows.add(id);
1897 restored = false;
1898 }
1899 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001900 intent = null;
1901 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1902 // We allow auto install apps to have their intent
1903 // updated after an install.
1904 intent = manager.getLaunchIntentForPackage(
1905 cn.getPackageName());
1906 if (intent != null) {
1907 ContentValues values = new ContentValues();
1908 values.put(LauncherSettings.Favorites.INTENT,
1909 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001910 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001911 }
1912 }
1913
1914 if (intent == null) {
1915 // The app is installed but the component is no
1916 // longer available.
1917 Launcher.addDumpLog(TAG,
1918 "Invalid component removed: " + cn, true);
1919 itemsToRemove.add(id);
1920 continue;
1921 } else {
1922 // no special handling necessary for this item
1923 restoredRows.add(id);
1924 restored = false;
1925 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001926 } else if (restored) {
1927 // Package is not yet available but might be
1928 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001929 Launcher.addDumpLog(TAG,
1930 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001931
1932 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1933 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001934 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001935 // App restore has started. Update the flag
1936 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1937 ContentValues values = new ContentValues();
1938 values.put(LauncherSettings.Favorites.RESTORED,
1939 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001940 updateItem(id, values);
1941 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1942 // This is a common app. Try to replace this.
1943 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1944 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1945 if (parser.findDefaultApp()) {
1946 // Default app found. Replace it.
1947 intent = parser.parsedIntent;
1948 cn = intent.getComponent();
1949 ContentValues values = parser.parsedValues;
1950 values.put(LauncherSettings.Favorites.RESTORED, 0);
1951 updateItem(id, values);
1952 restored = false;
1953 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001954
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001955 } else if (REMOVE_UNRESTORED_ICONS) {
1956 Launcher.addDumpLog(TAG,
1957 "Unrestored package removed: " + cn, true);
1958 itemsToRemove.add(id);
1959 continue;
1960 }
Sunny Goyal94485362014-09-18 16:13:58 -07001961 } else if (REMOVE_UNRESTORED_ICONS) {
1962 Launcher.addDumpLog(TAG,
1963 "Unrestored package removed: " + cn, true);
1964 itemsToRemove.add(id);
1965 continue;
1966 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001967 } else if (launcherApps.isAppEnabled(
1968 manager, cn.getPackageName(),
1969 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1970 // Package is present but not available.
1971 allowMissingTarget = true;
1972 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1973 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001974 // SdCard is not ready yet. Package might get available,
1975 // once it is ready.
1976 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1977 + " (check again later)", true);
1978 HashSet<String> pkgs = sPendingPackages.get(user);
1979 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001980 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001981 sPendingPackages.put(user, pkgs);
1982 }
1983 pkgs.add(cn.getPackageName());
1984 allowMissingTarget = true;
1985 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001986
1987 } else {
1988 // Do not wait for external media load anymore.
1989 // Log the invalid package, and remove it
1990 Launcher.addDumpLog(TAG,
1991 "Invalid package removed: " + cn, true);
1992 itemsToRemove.add(id);
1993 continue;
Winson Chungee055712013-07-30 14:46:24 -07001994 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001995 } else if (cn == null) {
1996 // For shortcuts with no component, keep them as they are
1997 restoredRows.add(id);
1998 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001999 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002001 Launcher.addDumpLog(TAG,
2002 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 continue;
2004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005
Sunny Goyal34b65272015-03-11 16:56:52 -07002006 boolean useLowResIcon = container >= 0 &&
2007 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2008
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002009 if (itemReplaced) {
2010 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002011 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002012 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002013 } else {
2014 // Don't replace items for other profiles.
2015 itemsToRemove.add(id);
2016 continue;
2017 }
2018 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002019 if (user.equals(UserHandleCompat.myUserHandle())) {
2020 Launcher.addDumpLog(TAG,
2021 "constructing info for partially restored package",
2022 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002023 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07002024 promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002025 intent = getRestoredItemIntent(c, context, intent);
2026 } else {
2027 // Don't restore items for other profiles.
2028 itemsToRemove.add(id);
2029 continue;
2030 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002031 } else if (itemType ==
2032 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002033 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002034 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 } else {
2036 info = getShortcutInfo(c, context, iconTypeIndex,
2037 iconPackageIndex, iconResourceIndex, iconIndex,
2038 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002039
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 // App shortcuts that used to be automatically added to Launcher
2041 // didn't always have the correct intent flags set, so do that
2042 // here
2043 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002044 intent.getCategories() != null &&
2045 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002046 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 intent.addFlags(
2048 Intent.FLAG_ACTIVITY_NEW_TASK |
2049 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2050 }
Michael Jurka96879562012-03-22 05:54:33 -07002051 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002054 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002057 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 info.cellX = c.getInt(cellXIndex);
2059 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002060 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002061 info.spanX = 1;
2062 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002063 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002064 info.isDisabled = disabledState;
2065 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2066 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2067 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002068
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002070 if (!checkItemPlacement(occupied, info)) {
2071 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 break;
2073 }
2074
Sunny Goyal756adbc2015-04-16 15:20:51 -07002075 if (restored) {
2076 ComponentName cn = info.getTargetComponent();
2077 if (cn != null) {
2078 Integer progress = installingPkgs.get(cn.getPackageName());
2079 if (progress != null) {
2080 info.setInstallProgress(progress);
2081 } else {
2082 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2083 }
2084 }
2085 }
2086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 switch (container) {
2088 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2089 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2090 sBgWorkspaceItems.add(info);
2091 break;
2092 default:
2093 // Item is in a user folder
2094 FolderInfo folderInfo =
2095 findOrMakeFolder(sBgFolders, container);
2096 folderInfo.add(info);
2097 break;
2098 }
2099 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002100 } else {
2101 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 }
2103 break;
2104
2105 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2106 id = c.getLong(idIndex);
2107 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2108
Sunny Goyala508e4f2015-05-21 09:33:57 -07002109 // Do not trim the folder label, as is was set by the user.
2110 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 folderInfo.cellX = c.getInt(cellXIndex);
2115 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002116 folderInfo.spanX = 1;
2117 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002118 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002119
Daniel Sandler8802e962010-05-26 16:28:16 -04002120 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002121 if (!checkItemPlacement(occupied, folderInfo)) {
2122 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002123 break;
2124 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002125
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2128 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2129 sBgWorkspaceItems.add(folderInfo);
2130 break;
Joe Onorato36115782010-06-17 13:28:48 -04002131 }
Joe Onorato17a89222011-02-08 17:26:11 -08002132
Chris Wrenf4d08112014-01-16 18:13:56 -05002133 if (restored) {
2134 // no special handling required for restored folders
2135 restoredRows.add(id);
2136 }
2137
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2139 sBgFolders.put(folderInfo.id, folderInfo);
2140 break;
2141
2142 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002143 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002145 boolean customWidget = itemType ==
2146 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2147
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002149 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002150 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002151 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002152 user = allUsers.get(serialNumber);
2153 if (user == null) {
2154 itemsToRemove.add(id);
2155 continue;
2156 }
2157
Sunny Goyalff572272014-07-23 13:58:07 -07002158 final ComponentName component =
2159 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002160
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002162 final boolean isIdValid = (restoreStatus &
2163 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002164 final boolean wasProviderReady = (restoreStatus &
2165 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002166
Adam Cohen59400422014-03-05 18:07:04 -08002167 final LauncherAppWidgetProviderInfo provider =
2168 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002169 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002170 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002171
2172 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002173 if (!isSafeMode && !customWidget &&
2174 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002175 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002176 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002177
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002179 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 itemsToRemove.add(id);
2181 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002182 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002183 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2184 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002185
Sunny Goyalff572272014-07-23 13:58:07 -07002186 int status = restoreStatus;
2187 if (!wasProviderReady) {
2188 // If provider was not previously ready, update the
2189 // status and UI flag.
2190
2191 // Id would be valid only if the widget restore broadcast was received.
2192 if (isIdValid) {
2193 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2194 } else {
2195 status &= ~LauncherAppWidgetInfo
2196 .FLAG_PROVIDER_NOT_READY;
2197 }
2198 }
2199 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002200 } else {
2201 Log.v(TAG, "Widget restore pending id=" + id
2202 + " appWidgetId=" + appWidgetId
2203 + " status =" + restoreStatus);
2204 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002205 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002206 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002207 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002208
2209 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2210 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002211 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002212 // App restore has started. Update the flag
2213 appWidgetInfo.restoreStatus |=
2214 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002215 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002216 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002217 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002218 itemsToRemove.add(id);
2219 continue;
2220 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002221
2222 appWidgetInfo.installProgress =
2223 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002224 }
Sunny Goyalff572272014-07-23 13:58:07 -07002225
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 appWidgetInfo.cellX = c.getInt(cellXIndex);
2229 appWidgetInfo.cellY = c.getInt(cellYIndex);
2230 appWidgetInfo.spanX = c.getInt(spanXIndex);
2231 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002232
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2234 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2235 Log.e(TAG, "Widget found where container != " +
2236 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2237 continue;
2238 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002240 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002242 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2243 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 break;
2245 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002246
Adam Cohen59400422014-03-05 18:07:04 -08002247 if (!customWidget) {
2248 String providerName =
2249 appWidgetInfo.providerName.flattenToString();
2250 if (!providerName.equals(savedProvider) ||
2251 (appWidgetInfo.restoreStatus != restoreStatus)) {
2252 ContentValues values = new ContentValues();
2253 values.put(
2254 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2255 providerName);
2256 values.put(LauncherSettings.Favorites.RESTORED,
2257 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002258 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002259 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002260 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2262 sBgAppWidgets.add(appWidgetInfo);
2263 }
Joe Onorato36115782010-06-17 13:28:48 -04002264 break;
2265 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002267 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002268 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 }
2270 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002271 if (c != null) {
2272 c.close();
2273 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 }
2275
Winson Chungba9c37f2013-08-30 14:11:37 -07002276 // Break early if we've stopped loading
2277 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002278 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002279 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002280 }
2281
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002284 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2285 Utilities.createDbSelectionQuery(
2286 LauncherSettings.Favorites._ID, itemsToRemove), null);
2287 if (DEBUG_LOADERS) {
2288 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2289 LauncherSettings.Favorites._ID, itemsToRemove));
2290 }
2291
2292 // Remove any empty folder
2293 for (long folderId : LauncherAppState.getLauncherProvider()
2294 .deleteEmptyFolders()) {
2295 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2296 sBgFolders.remove(folderId);
2297 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002298 }
2299 }
2300
Chris Wrenf4d08112014-01-16 18:13:56 -05002301 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002302 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002303 ContentValues values = new ContentValues();
2304 values.put(LauncherSettings.Favorites.RESTORED, 0);
2305 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2306 Utilities.createDbSelectionQuery(
2307 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002308 }
2309
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002310 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2311 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002312 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002313 null, sWorker);
2314 }
2315
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002316 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Winson Chung9e6a0a22013-08-27 11:58:12 -07002317
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002318 // Remove any empty screens
2319 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002320 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002321 long screenId = item.screenId;
2322 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2323 unusedScreens.contains(screenId)) {
2324 unusedScreens.remove(screenId);
2325 }
2326 }
2327
2328 // If there are any empty screens remove them, and update.
2329 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002330 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 }
2333
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 if (DEBUG_LOADERS) {
2335 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2336 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002338 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002340
Sunny Goyale2df0622015-04-24 11:27:00 -07002341 for (int i = 0; i < nScreens; i++) {
2342 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002343 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 line += " | ";
2345 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002346 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002347 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002348 if (x < screen.length && y < screen[x].length) {
2349 line += (screen[x][y] != null) ? "#" : ".";
2350 } else {
2351 line += "!";
2352 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002357 }
Joe Onorato36115782010-06-17 13:28:48 -04002358 }
Adam Cohene25af792013-06-06 23:08:25 -07002359 }
2360
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002361 /**
2362 * Partially updates the item without any notification. Must be called on the worker thread.
2363 */
2364 private void updateItem(long itemId, ContentValues update) {
2365 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002366 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002367 update,
2368 BaseColumns._ID + "= ?",
2369 new String[]{Long.toString(itemId)});
2370 }
2371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 /** Filters the set of items who are directly or indirectly (via another container) on the
2373 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 ArrayList<ItemInfo> allWorkspaceItems,
2376 ArrayList<ItemInfo> currentScreenItems,
2377 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002378 // Purge any null ItemInfos
2379 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2380 while (iter.hasNext()) {
2381 ItemInfo i = iter.next();
2382 if (i == null) {
2383 iter.remove();
2384 }
2385 }
2386
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 // Order the set of items by their containers first, this allows use to walk through the
2388 // list sequentially, build up a list of containers that are in the specified screen,
2389 // as well as all items in those containers.
2390 Set<Long> itemsOnScreen = new HashSet<Long>();
2391 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2392 @Override
2393 public int compare(ItemInfo lhs, ItemInfo rhs) {
2394 return (int) (lhs.container - rhs.container);
2395 }
2396 });
2397 for (ItemInfo info : allWorkspaceItems) {
2398 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002399 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 currentScreenItems.add(info);
2401 itemsOnScreen.add(info.id);
2402 } else {
2403 otherScreenItems.add(info);
2404 }
2405 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2406 currentScreenItems.add(info);
2407 itemsOnScreen.add(info.id);
2408 } else {
2409 if (itemsOnScreen.contains(info.container)) {
2410 currentScreenItems.add(info);
2411 itemsOnScreen.add(info.id);
2412 } else {
2413 otherScreenItems.add(info);
2414 }
2415 }
2416 }
2417 }
2418
2419 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002420 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 ArrayList<LauncherAppWidgetInfo> appWidgets,
2422 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2423 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424
2425 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002426 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002428 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 currentScreenWidgets.add(widget);
2430 } else {
2431 otherScreenWidgets.add(widget);
2432 }
2433 }
2434 }
2435
2436 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002437 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002438 LongArrayMap<ItemInfo> itemsIdMap,
2439 LongArrayMap<FolderInfo> folders,
2440 LongArrayMap<FolderInfo> currentScreenFolders,
2441 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442
Sunny Goyale2df0622015-04-24 11:27:00 -07002443 int total = folders.size();
2444 for (int i = 0; i < total; i++) {
2445 long id = folders.keyAt(i);
2446 FolderInfo folder = folders.valueAt(i);
2447
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002449 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002451 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 currentScreenFolders.put(id, folder);
2453 } else {
2454 otherScreenFolders.put(id, folder);
2455 }
2456 }
2457 }
2458
2459 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2460 * right) */
2461 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002462 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002463 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 // XXX: review this
2465 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002466 @Override
2467 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002468 int cellCountX = (int) profile.numColumns;
2469 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002470 int screenOffset = cellCountX * cellCountY;
2471 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002472 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002473 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002475 rhs.cellY * cellCountX + rhs.cellX);
2476 return (int) (lr - rr);
2477 }
2478 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002480
Adam Cohendcd297f2013-06-18 13:13:40 -07002481 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2482 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002483 final Runnable r = new Runnable() {
2484 @Override
2485 public void run() {
2486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2487 if (callbacks != null) {
2488 callbacks.bindScreens(orderedScreens);
2489 }
2490 }
2491 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002492 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002493 }
2494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2496 final ArrayList<ItemInfo> workspaceItems,
2497 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002498 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002500
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 final boolean postOnMainThread = (deferredBindRunnables != null);
2502
2503 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002504 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002506 final int start = i;
2507 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 final Runnable r = new Runnable() {
2509 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002511 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002513 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2514 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 }
2516 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 };
2518 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002519 synchronized (deferredBindRunnables) {
2520 deferredBindRunnables.add(r);
2521 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002523 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 }
Joe Onorato36115782010-06-17 13:28:48 -04002525 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526
2527 // Bind the folders
2528 if (!folders.isEmpty()) {
2529 final Runnable r = new Runnable() {
2530 public void run() {
2531 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532 if (callbacks != null) {
2533 callbacks.bindFolders(folders);
2534 }
2535 }
2536 };
2537 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002538 synchronized (deferredBindRunnables) {
2539 deferredBindRunnables.add(r);
2540 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002542 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 }
2544 }
2545
2546 // Bind the widgets, one at a time
2547 N = appWidgets.size();
2548 for (int i = 0; i < N; i++) {
2549 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2550 final Runnable r = new Runnable() {
2551 public void run() {
2552 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2553 if (callbacks != null) {
2554 callbacks.bindAppWidget(widget);
2555 }
2556 }
2557 };
2558 if (postOnMainThread) {
2559 deferredBindRunnables.add(r);
2560 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002561 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 }
2563 }
2564 }
2565
2566 /**
2567 * Binds all loaded data to actual views on the main thread.
2568 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002569 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570 final long t = SystemClock.uptimeMillis();
2571 Runnable r;
2572
2573 // Don't use these two variables in any of the callback runnables.
2574 // Otherwise we hold a reference to them.
2575 final Callbacks oldCallbacks = mCallbacks.get();
2576 if (oldCallbacks == null) {
2577 // This launcher has exited and nobody bothered to tell us. Just bail.
2578 Log.w(TAG, "LoaderTask running with no launcher");
2579 return;
2580 }
2581
Winson Chung9b9fb962013-11-15 15:39:34 -08002582 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2584 ArrayList<LauncherAppWidgetInfo> appWidgets =
2585 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002586 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002587
2588 final LongArrayMap<FolderInfo> folders;
2589 final LongArrayMap<ItemInfo> itemsIdMap;
2590
Winson Chung2abf94d2012-07-18 18:16:38 -07002591 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 workspaceItems.addAll(sBgWorkspaceItems);
2593 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002594 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002595
2596 folders = sBgFolders.clone();
2597 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002598 }
2599
Derek Prothro7aff3992013-12-10 14:00:37 -05002600 final boolean isLoadingSynchronously =
2601 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002602 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002603 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002604 if (currScreen >= orderedScreenIds.size()) {
2605 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002606 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002607 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002608 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002609 final long currentScreenId = currentScreen < 0
2610 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002611
2612 // Load all the items that are on the current page first (and in the process, unbind
2613 // all the existing workspace items before we call startBinding() below.
2614 unbindWorkspaceItemsOnMainThread();
2615
2616 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002617 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2618 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2619 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2620 new ArrayList<LauncherAppWidgetInfo>();
2621 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2622 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002623 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2624 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625
Winson Chung9b9fb962013-11-15 15:39:34 -08002626 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002628 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002630 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002631 otherFolders);
2632 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2633 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2634
2635 // Tell the workspace that we're about to start binding items
2636 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002637 public void run() {
2638 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2639 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002640 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002641 }
2642 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002644 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645
Adam Cohendcd297f2013-06-18 13:13:40 -07002646 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2647
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 // Load items on the current page
2649 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2650 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002651 if (isLoadingSynchronously) {
2652 r = new Runnable() {
2653 public void run() {
2654 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002655 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002656 callbacks.onPageBoundSynchronously(currentScreen);
2657 }
2658 }
2659 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002660 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662
Winson Chung4a2afa32012-07-19 14:53:05 -07002663 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2664 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002665 synchronized (mDeferredBindRunnables) {
2666 mDeferredBindRunnables.clear();
2667 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002669 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670
2671 // Tell the workspace that we're done binding items
2672 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002673 public void run() {
2674 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2675 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002676 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002677 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678
Winson Chung98e030b2012-05-07 16:01:11 -07002679 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002680 if (DEBUG_LOADERS) {
2681 Log.d(TAG, "bound workspace in "
2682 + (SystemClock.uptimeMillis()-t) + "ms");
2683 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002684
2685 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002686 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002687 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002688 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002689 synchronized (mDeferredBindRunnables) {
2690 mDeferredBindRunnables.add(r);
2691 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002692 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002693 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002694 }
Joe Onorato36115782010-06-17 13:28:48 -04002695 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002696
Joe Onorato36115782010-06-17 13:28:48 -04002697 private void loadAndBindAllApps() {
2698 if (DEBUG_LOADERS) {
2699 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2700 }
2701 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002702 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002703 synchronized (LoaderTask.this) {
2704 if (mStopped) {
2705 return;
2706 }
2707 }
Sunny Goyal75b0f552015-05-20 21:57:06 -07002708 mIconCache.updateDbIcons();
Reena Lee93f824a2011-09-23 17:20:28 -07002709 synchronized (LoaderTask.this) {
2710 if (mStopped) {
2711 return;
2712 }
2713 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002714 }
Joe Onorato36115782010-06-17 13:28:48 -04002715 } else {
2716 onlyBindAllApps();
2717 }
2718 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002719
Joe Onorato36115782010-06-17 13:28:48 -04002720 private void onlyBindAllApps() {
2721 final Callbacks oldCallbacks = mCallbacks.get();
2722 if (oldCallbacks == null) {
2723 // This launcher has exited and nobody bothered to tell us. Just bail.
2724 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2725 return;
2726 }
2727
2728 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002729 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002730 final ArrayList<AppInfo> list
2731 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002732 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002733 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002734 public void run() {
2735 final long t = SystemClock.uptimeMillis();
2736 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2737 if (callbacks != null) {
2738 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002739 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002740 }
2741 if (DEBUG_LOADERS) {
2742 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2743 + (SystemClock.uptimeMillis()-t) + "ms");
2744 }
2745 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002746 };
2747 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002748 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002749 r.run();
2750 } else {
2751 mHandler.post(r);
2752 }
Joe Onorato36115782010-06-17 13:28:48 -04002753 }
2754
Winson Chung64359a52013-07-08 17:17:08 -07002755 private void loadAllApps() {
2756 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002757
Joe Onorato36115782010-06-17 13:28:48 -04002758 final Callbacks oldCallbacks = mCallbacks.get();
2759 if (oldCallbacks == null) {
2760 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002761 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002762 return;
2763 }
2764
Kenny Guyed131872014-04-30 03:02:21 +01002765 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2766
Winson Chung64359a52013-07-08 17:17:08 -07002767 // Clear the list of apps
2768 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002769 for (UserHandleCompat user : profiles) {
2770 // Query for the set of apps
2771 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002772 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002773 if (DEBUG_LOADERS) {
2774 Log.d(TAG, "getActivityList took "
2775 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2776 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2777 }
2778 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002779 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002780 if (apps == null || apps.isEmpty()) {
2781 return;
2782 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002783
Kenny Guyed131872014-04-30 03:02:21 +01002784 // Create the ApplicationInfos
2785 for (int i = 0; i < apps.size(); i++) {
2786 LauncherActivityInfoCompat app = apps.get(i);
2787 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002788 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002789 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002790
Sunny Goyal756a28a2015-04-23 17:07:55 -07002791 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2792 if (heuristic != null) {
2793 runAfterBindCompletes(new Runnable() {
2794
2795 @Override
2796 public void run() {
2797 heuristic.processUserApps(apps);
2798 }
2799 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002800 }
Winson Chung64359a52013-07-08 17:17:08 -07002801 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002802 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002803 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2804 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002805
2806 // Post callback on main thread
2807 mHandler.post(new Runnable() {
2808 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002809
Winson Chung64359a52013-07-08 17:17:08 -07002810 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002811 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002812 if (callbacks != null) {
2813 callbacks.bindAllApplications(added);
2814 if (DEBUG_LOADERS) {
2815 Log.d(TAG, "bound " + added.size() + " apps in "
2816 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2817 }
2818 } else {
2819 Log.i(TAG, "not binding apps: no Launcher activity");
2820 }
2821 }
2822 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002823 // Cleanup any data stored for a deleted user.
2824 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002825
Hyunyoung Song219d0482015-05-07 12:51:42 -07002826 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2827 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002828 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002829 Log.d(TAG, "Icons processed in "
2830 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002831 }
2832 }
2833
2834 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002835 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002836 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002837 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2838 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2839 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2840 }
Joe Onorato36115782010-06-17 13:28:48 -04002841 }
2842 }
2843
Sunny Goyal75b0f552015-05-20 21:57:06 -07002844 /**
2845 * Called when the icons for packages have been updated in the icon cache.
2846 */
2847 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2848 final Callbacks callbacks = getCallback();
2849 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2850 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2851
2852 // If any package icon has changed (app was updated while launcher was dead),
2853 // update the corresponding shortcuts.
2854 synchronized (sBgLock) {
2855 for (ItemInfo info : sBgItemsIdMap) {
2856 if (info instanceof ShortcutInfo && user.equals(info.user)
2857 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2858 ShortcutInfo si = (ShortcutInfo) info;
2859 ComponentName cn = si.getTargetComponent();
2860 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2861 si.updateIcon(mIconCache);
2862 updatedShortcuts.add(si);
2863 }
2864 }
2865 }
2866 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2867 }
2868
2869 if (!updatedShortcuts.isEmpty()) {
2870 final UserHandleCompat userFinal = user;
2871 mHandler.post(new Runnable() {
2872
2873 public void run() {
2874 Callbacks cb = getCallback();
2875 if (cb != null && callbacks == cb) {
2876 cb.bindShortcutsChanged(updatedShortcuts,
2877 new ArrayList<ShortcutInfo>(), userFinal);
2878 }
2879 }
2880 });
2881 }
2882
2883 if (!updatedApps.isEmpty()) {
2884 mHandler.post(new Runnable() {
2885
2886 public void run() {
2887 Callbacks cb = getCallback();
2888 if (cb != null && callbacks == cb) {
2889 cb.bindAppsUpdated(updatedApps);
2890 }
2891 }
2892 });
2893 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002894
2895 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2896 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002897 }
2898
Joe Onorato36115782010-06-17 13:28:48 -04002899 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002900 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002901 }
2902
Adam Cohen091440a2015-03-18 14:16:05 -07002903 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002904
2905 @Override
2906 public void onReceive(Context context, Intent intent) {
2907 synchronized (sBgLock) {
2908 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2909 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002910 final PackageManager manager = context.getPackageManager();
2911 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2912 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002913 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2914 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002915 packagesRemoved.clear();
2916 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002917 for (String pkg : entry.getValue()) {
2918 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002919 boolean packageOnSdcard = launcherApps.isAppEnabled(
2920 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2921 if (packageOnSdcard) {
2922 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2923 packagesUnavailable.add(pkg);
2924 } else {
2925 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2926 packagesRemoved.add(pkg);
2927 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928 }
2929 }
2930 if (!packagesRemoved.isEmpty()) {
2931 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2932 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2933 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 if (!packagesUnavailable.isEmpty()) {
2935 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2936 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2937 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002938 }
Sunny Goyal34942622014-08-29 17:20:55 -07002939 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002940 }
2941 }
2942 }
2943
Joe Onorato36115782010-06-17 13:28:48 -04002944 private class PackageUpdatedTask implements Runnable {
2945 int mOp;
2946 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002947 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002948
2949 public static final int OP_NONE = 0;
2950 public static final int OP_ADD = 1;
2951 public static final int OP_UPDATE = 2;
2952 public static final int OP_REMOVE = 3; // uninstlled
2953 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2954
2955
Kenny Guyed131872014-04-30 03:02:21 +01002956 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002957 mOp = op;
2958 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002959 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002960 }
2961
2962 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002963 if (!mHasLoaderCompletedOnce) {
2964 // Loader has not yet run.
2965 return;
2966 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002967 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002968
2969 final String[] packages = mPackages;
2970 final int N = packages.length;
2971 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002972 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002973 for (int i=0; i<N; i++) {
2974 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002975 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002976 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002977 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002978
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002979 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2980 if (heuristic != null) {
2981 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002982 }
Joe Onorato36115782010-06-17 13:28:48 -04002983 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002984 }
Joe Onorato36115782010-06-17 13:28:48 -04002985 case OP_UPDATE:
2986 for (int i=0; i<N; i++) {
2987 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002988 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002989 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002990 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002991 }
2992 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002993 case OP_REMOVE: {
2994 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2995 if (heuristic != null) {
2996 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002997 }
Joe Onorato36115782010-06-17 13:28:48 -04002998 for (int i=0; i<N; i++) {
2999 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003000 mIconCache.removeIconsForPkg(packages[i], mUser);
3001 }
3002 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003003 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003004 case OP_UNAVAILABLE:
3005 for (int i=0; i<N; i++) {
3006 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3007 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003008 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003009 }
3010 break;
3011 }
3012
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003013 ArrayList<AppInfo> added = null;
3014 ArrayList<AppInfo> modified = null;
3015 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003016
Adam Cohen487f7dd2012-06-28 18:12:10 -07003017 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003018 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003019 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003020 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003021 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003022 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003023 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003024 }
Winson Chung5d55f332012-07-16 20:45:03 -07003025 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003026 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003027 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003028 }
3029
Sunny Goyale0f58d72014-11-10 18:05:31 -08003030 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003031 if (callbacks == null) {
3032 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3033 return;
3034 }
3035
Sunny Goyal4390ace2014-10-13 11:33:11 -07003036 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3037 new HashMap<ComponentName, AppInfo>();
3038
Joe Onorato36115782010-06-17 13:28:48 -04003039 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003040 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003041 for (AppInfo ai : added) {
3042 addedOrUpdatedApps.put(ai.componentName, ai);
3043 }
Joe Onorato36115782010-06-17 13:28:48 -04003044 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003045
Joe Onorato36115782010-06-17 13:28:48 -04003046 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003047 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003048 for (AppInfo ai : modified) {
3049 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003050 }
3051
Joe Onorato36115782010-06-17 13:28:48 -04003052 mHandler.post(new Runnable() {
3053 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003054 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003055 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003056 callbacks.bindAppsUpdated(modifiedFinal);
3057 }
3058 }
3059 });
3060 }
Winson Chung83892cc2013-05-01 16:53:33 -07003061
Sunny Goyal4390ace2014-10-13 11:33:11 -07003062 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003063 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003064 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3065 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3066 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3067
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003068 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003069 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003070 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003071 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3072 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003073 boolean infoUpdated = false;
3074 boolean shortcutUpdated = false;
3075
3076 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003077 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003078 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003079 Bitmap icon = Utilities.createIconBitmap(
3080 si.iconResource.packageName,
3081 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 if (icon != null) {
3083 si.setIcon(icon);
3084 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003085 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 }
3087 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088
3089 ComponentName cn = si.getTargetComponent();
3090 if (cn != null && packageSet.contains(cn.getPackageName())) {
3091 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3092
3093 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3095 // Auto install icon
3096 PackageManager pm = context.getPackageManager();
3097 ResolveInfo matched = pm.resolveActivity(
3098 new Intent(Intent.ACTION_MAIN)
3099 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3100 PackageManager.MATCH_DEFAULT_ONLY);
3101 if (matched == null) {
3102 // Try to find the best match activity.
3103 Intent intent = pm.getLaunchIntentForPackage(
3104 cn.getPackageName());
3105 if (intent != null) {
3106 cn = intent.getComponent();
3107 appInfo = addedOrUpdatedApps.get(cn);
3108 }
3109
3110 if ((intent == null) || (appInfo == null)) {
3111 removedShortcuts.add(si);
3112 continue;
3113 }
3114 si.promisedIntent = intent;
3115 }
3116 }
3117
3118 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003119 if (appInfo != null) {
3120 si.flags = appInfo.flags;
3121 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122
Sunny Goyal756adbc2015-04-16 15:20:51 -07003123 si.intent = si.promisedIntent;
3124 si.promisedIntent = null;
3125 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003126 infoUpdated = true;
3127 si.updateIcon(mIconCache);
3128 }
3129
3130 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3131 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3132 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003133 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003134 si.contentDescription = appInfo.contentDescription;
3135 infoUpdated = true;
3136 }
3137
3138 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3139 // Since package was just updated, the target must be available now.
3140 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3141 shortcutUpdated = true;
3142 }
3143 }
3144
3145 if (infoUpdated || shortcutUpdated) {
3146 updatedShortcuts.add(si);
3147 }
3148 if (infoUpdated) {
3149 updateItemInDatabase(context, si);
3150 }
3151 } else if (info instanceof LauncherAppWidgetInfo) {
3152 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3153 if (mUser.equals(widgetInfo.user)
3154 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3155 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3156 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3157 widgets.add(widgetInfo);
3158 updateItemInDatabase(context, widgetInfo);
3159 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003160 }
3161 }
3162 }
3163
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3165 mHandler.post(new Runnable() {
3166
3167 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003168 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 if (callbacks == cb && cb != null) {
3170 callbacks.bindShortcutsChanged(
3171 updatedShortcuts, removedShortcuts, mUser);
3172 }
3173 }
3174 });
3175 if (!removedShortcuts.isEmpty()) {
3176 deleteItemsFromDatabase(context, removedShortcuts);
3177 }
3178 }
3179 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003180 mHandler.post(new Runnable() {
3181 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003182 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003183 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003184 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003185 }
3186 }
3187 });
3188 }
3189 }
3190
Winson Chungdf95eb12013-10-16 14:57:07 -07003191 final ArrayList<String> removedPackageNames =
3192 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003193 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003194 // Mark all packages in the broadcast to be removed
3195 removedPackageNames.addAll(Arrays.asList(packages));
3196 } else if (mOp == OP_UPDATE) {
3197 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003198 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003199 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003200 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003201 }
3202 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003203 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003204
Winson Chungdf95eb12013-10-16 14:57:07 -07003205 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003206 final int removeReason;
3207 if (mOp == OP_UNAVAILABLE) {
3208 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3209 } else {
3210 // Remove all the components associated with this package
3211 for (String pn : removedPackageNames) {
3212 deletePackageFromDatabase(context, pn, mUser);
3213 }
3214 // Remove all the specific components
3215 for (AppInfo a : removedApps) {
3216 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3217 deleteItemsFromDatabase(context, infos);
3218 }
3219 removeReason = 0;
3220 }
3221
Winson Chungdf95eb12013-10-16 14:57:07 -07003222 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003224 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003225 mHandler.post(new Runnable() {
3226 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003227 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003228 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003229 callbacks.bindComponentsRemoved(
3230 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003231 }
3232 }
3233 });
Joe Onoratobe386092009-11-17 17:32:16 -08003234 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003235
3236 // onProvidersChanged method (API >= 17) already refreshed the widget list
3237 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3238
Adam Cohen4caf2982013-08-20 18:54:31 -07003239 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003240 mHandler.post(new Runnable() {
3241 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003242 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003243 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003244 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003245 }
3246 }
3247 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 }
3249 }
3250
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003251 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3252 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003253 ArrayList<LauncherAppWidgetProviderInfo> results =
3254 new ArrayList<LauncherAppWidgetProviderInfo>();
3255 try {
3256 synchronized (sBgLock) {
3257 if (sBgWidgetProviders == null || refresh) {
3258 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3259 = new HashMap<>();
3260 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3261 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003262
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003263 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3264 for (AppWidgetProviderInfo pInfo : widgets) {
3265 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3266 UserHandleCompat user = wm.getUser(info);
3267 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3268 }
Robin Lee26ace122015-03-16 19:41:43 +00003269
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003270 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3271 for (CustomAppWidget widget : customWidgets) {
3272 info = new LauncherAppWidgetProviderInfo(context, widget);
3273 UserHandleCompat user = wm.getUser(info);
3274 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3275 }
3276 // Replace the global list at the very end, so that if there is an exception,
3277 // previously loaded provider list is used.
3278 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003279 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003280 results.addAll(sBgWidgetProviders.values());
3281 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003282 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003283 } catch (Exception e) {
3284 if (e.getCause() instanceof TransactionTooLargeException) {
3285 // the returned value may be incomplete and will not be refreshed until the next
3286 // time Launcher starts.
3287 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3288 // onResume is called to refresh the widget provider list.
3289 synchronized (sBgLock) {
3290 if (sBgWidgetProviders != null) {
3291 results.addAll(sBgWidgetProviders.values());
3292 }
3293 return results;
3294 }
3295 } else {
3296 throw e;
3297 }
Adam Cohen59400422014-03-05 18:07:04 -08003298 }
3299 }
3300
Robin Lee26ace122015-03-16 19:41:43 +00003301 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3302 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003303 synchronized (sBgLock) {
3304 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003305 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003306 }
Robin Lee26ace122015-03-16 19:41:43 +00003307 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003308 }
3309 }
3310
Hyunyoung Song227239e2015-05-04 18:17:35 -07003311 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3312 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003313
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003314 runOnWorkerThread(new Runnable(){
3315 @Override
3316 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003317 final WidgetsModel model = createWidgetsModel(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003318 mHandler.post(new Runnable() {
3319 @Override
3320 public void run() {
3321 Callbacks cb = getCallback();
3322 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003323 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003324 }
3325 }
3326 });
Hyunyoung Song9110d482015-05-22 14:49:23 -07003327 mBgWidgetsModel = model;
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003328 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003329 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3330 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003331 }
3332 });
3333 }
3334
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003335 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003336 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003337 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003338 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003339 */
Sunny Goyal316490e2015-06-02 09:38:28 -07003340 @Thunk WidgetsModel createWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003341 PackageManager packageManager = context.getPackageManager();
3342 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003343 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003344 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3345 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003346 WidgetsModel model = new WidgetsModel(context);
3347 model.addWidgetsAndShortcuts(widgetsAndShortcuts);
3348 return model;
Michael Jurkac402cd92013-05-20 15:49:32 +02003349 }
3350
Adam Cohen091440a2015-03-18 14:16:05 -07003351 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003352 UserHandleCompat user) {
3353 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3354 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003355 }
Adam Cohen556f6132014-01-15 15:18:08 -08003356
Kenny Guyed131872014-04-30 03:02:21 +01003357 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3358 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003359 if (cn == null) {
3360 return false;
3361 }
Kenny Guyed131872014-04-30 03:02:21 +01003362 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3363 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003364 return false;
3365 }
Kenny Guyed131872014-04-30 03:02:21 +01003366 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003367 }
3368
Adam Cohena28b78e2014-05-20 17:03:04 -07003369 public static boolean isValidPackage(Context context, String packageName,
3370 UserHandleCompat user) {
3371 if (packageName == null) {
3372 return false;
3373 }
3374 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3375 return launcherApps.isPackageEnabledForProfile(packageName, user);
3376 }
3377
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003379 * Make an ShortcutInfo object for a restored application or shortcut item that points
3380 * to a package that is not yet installed on the system.
3381 */
Sunny Goyal34942622014-08-29 17:20:55 -07003382 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07003383 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003384 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003385 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal5c97f512015-05-19 16:03:28 -07003386 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
Sunny Goyal34942622014-08-29 17:20:55 -07003387
3388 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3389 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3390 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003391 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003392 }
Sunny Goyal34942622014-08-29 17:20:55 -07003393 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3394 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003395 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003396 }
Sunny Goyal34942622014-08-29 17:20:55 -07003397 } else {
3398 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3399 }
3400
Winson Chung82b016c2015-05-08 17:00:10 -07003401 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003402 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003403 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003404 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003405 return info;
3406 }
3407
3408 /**
3409 * Make an Intent object for a restored application or shortcut item that points
3410 * to the market page for the item.
3411 */
Adam Cohen091440a2015-03-18 14:16:05 -07003412 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003413 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003414 return getMarketIntent(componentName.getPackageName());
3415 }
3416
3417 static Intent getMarketIntent(String packageName) {
3418 return new Intent(Intent.ACTION_VIEW)
3419 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003420 .scheme("market")
3421 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003422 .appendQueryParameter("id", packageName)
3423 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 }
3425
3426 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003427 * Make an ShortcutInfo object for a shortcut that is an application.
3428 *
3429 * If c is not null, then it will be used to fill in missing data like the title and icon.
3430 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003431 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003432 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003433 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003434 if (user == null) {
3435 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003436 return null;
3437 }
3438
Kenny Guyed131872014-04-30 03:02:21 +01003439 ComponentName componentName = intent.getComponent();
3440 if (componentName == null) {
3441 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3442 return null;
3443 }
3444
3445 Intent newIntent = new Intent(intent.getAction(), null);
3446 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3447 newIntent.setComponent(componentName);
3448 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003449 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003450 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3451 return null;
3452 }
3453
3454 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003455 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003456 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3457 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3458 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003459 }
3460
Joe Onorato56d82912010-03-07 14:32:10 -05003461 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003462 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003463 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003464 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003465
Joe Onorato56d82912010-03-07 14:32:10 -05003466 // fall back to the class name of the activity
3467 if (info.title == null) {
3468 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003469 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003470
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003472 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003473 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003474 if (lai != null) {
3475 info.flags = AppInfo.initFlags(lai);
3476 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003479
Sunny Goyale2df0622015-04-24 11:27:00 -07003480 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003481 ItemInfoFilter f) {
3482 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3483 for (ItemInfo i : infos) {
3484 if (i instanceof ShortcutInfo) {
3485 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003486 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003487 if (cn != null && f.filterItem(null, info, cn)) {
3488 filtered.add(info);
3489 }
3490 } else if (i instanceof FolderInfo) {
3491 FolderInfo info = (FolderInfo) i;
3492 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003493 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003494 if (cn != null && f.filterItem(info, s, cn)) {
3495 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003496 }
3497 }
Winson Chung64359a52013-07-08 17:17:08 -07003498 } else if (i instanceof LauncherAppWidgetInfo) {
3499 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3500 ComponentName cn = info.providerName;
3501 if (cn != null && f.filterItem(null, info, cn)) {
3502 filtered.add(info);
3503 }
Winson Chung8a435102012-08-30 17:16:53 -07003504 }
3505 }
Winson Chung64359a52013-07-08 17:17:08 -07003506 return new ArrayList<ItemInfo>(filtered);
3507 }
3508
Adam Cohen091440a2015-03-18 14:16:05 -07003509 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003510 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003511 ItemInfoFilter filter = new ItemInfoFilter() {
3512 @Override
3513 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003514 if (info.user == null) {
3515 return cn.equals(cname);
3516 } else {
3517 return cn.equals(cname) && info.user.equals(user);
3518 }
Winson Chung64359a52013-07-08 17:17:08 -07003519 }
3520 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003521 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003522 }
3523
Sunny Goyal1a745e82014-10-02 15:58:31 -07003524 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003525 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003526 */
Adam Cohen091440a2015-03-18 14:16:05 -07003527 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003528 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3529 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003530
Joe Onorato56d82912010-03-07 14:32:10 -05003531 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003532 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003533 // Non-app shortcuts are only supported for current user.
3534 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003536
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003537 // TODO: If there's an explicit component and we can't install that, delete it.
3538
Winson Chung82b016c2015-05-08 17:00:10 -07003539 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003540
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 int iconType = c.getInt(iconTypeIndex);
3542 switch (iconType) {
3543 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3544 String packageName = c.getString(iconPackageIndex);
3545 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003546 info.customIcon = false;
3547 // the resource
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003548 icon = Utilities.createIconBitmap(packageName, resourceName, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003549 // the db
3550 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003551 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003552 }
3553 // the fallback icon
3554 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003555 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003556 info.usingFallbackIcon = true;
3557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 break;
3559 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003560 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003561 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003562 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003563 info.customIcon = false;
3564 info.usingFallbackIcon = true;
3565 } else {
3566 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 break;
3569 default:
Kenny Guyed131872014-04-30 03:02:21 +01003570 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003571 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 info.customIcon = false;
3573 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003574 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003575 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 return info;
3577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578
Sunny Goyal2350bc92014-10-14 16:42:54 -07003579 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003580 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3581 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3582 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3583
Adam Cohend9198822011-11-22 16:42:47 -08003584 if (intent == null) {
3585 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3586 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3587 return null;
3588 }
3589
Joe Onorato0589f0f2010-02-08 13:44:00 -08003590 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003591 boolean customIcon = false;
3592 ShortcutIconResource iconResource = null;
3593
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003594 if (bitmap instanceof Bitmap) {
3595 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003596 customIcon = true;
3597 } else {
3598 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003599 if (extra instanceof ShortcutIconResource) {
3600 iconResource = (ShortcutIconResource) extra;
3601 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003602 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003603 }
3604 }
3605
Michael Jurkac9d95c52011-08-29 14:03:34 -07003606 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003607
Kenny Guyed131872014-04-30 03:02:21 +01003608 // Only support intents for current user for now. Intents sent from other
3609 // users wouldn't get here without intent forwarding anyway.
3610 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003611 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003612 icon = mIconCache.getDefaultIcon(info.user);
3613 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003614 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003615 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003616
Winson Chung82b016c2015-05-08 17:00:10 -07003617 info.title = Utilities.trim(name);
3618 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003619 info.intent = intent;
3620 info.customIcon = customIcon;
3621 info.iconResource = iconResource;
3622
3623 return info;
3624 }
3625
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003627 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 * or make a new one.
3629 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003630 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 // See if a placeholder was created for us already
3632 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003633 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003635 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 folders.put(id, folderInfo);
3637 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003638 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639 }
3640
Joe Onoratobe386092009-11-17 17:32:16 -08003641
Sunny Goyal651077b2014-06-30 14:15:31 -07003642 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3643 return (provider != null) && (provider.provider != null)
3644 && (provider.provider.getPackageName() != null);
3645 }
3646
Joe Onoratobe386092009-11-17 17:32:16 -08003647 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003648 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003649 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3650 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3651 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3652 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003653 if (mLoaderTask != null) {
3654 mLoaderTask.dumpState();
3655 } else {
3656 Log.d(TAG, "mLoaderTask=null");
3657 }
Joe Onoratobe386092009-11-17 17:32:16 -08003658 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003659
3660 public Callbacks getCallback() {
3661 return mCallbacks != null ? mCallbacks.get() : null;
3662 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003663
3664 /**
3665 * @return {@link FolderInfo} if its already loaded.
3666 */
3667 public FolderInfo findFolderById(Long folderId) {
3668 synchronized (sBgLock) {
3669 return sBgFolders.get(folderId);
3670 }
3671 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003672
3673 /**
3674 * @return the looper for the worker thread which can be used to start background tasks.
3675 */
3676 public static Looper getWorkerLooper() {
3677 return sWorkerThread.getLooper();
3678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003679}