blob: 53966a58f75777899571d500be5fa1abf973b937 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070039import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080040import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040041import android.os.Handler;
42import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070043import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040048import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080049import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080051import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070052import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010053
Sunny Goyalffe83f12014-08-14 17:39:34 -070054import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010055import com.android.launcher3.compat.LauncherActivityInfoCompat;
56import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070057import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070058import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010059import com.android.launcher3.compat.UserHandleCompat;
60import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
143 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700145 // The lock that must be acquired before referencing any static bg data structures. Unlike
146 // other locks, this one can generally be held long-term because we never expect any of these
147 // static data structures to be referenced outside of the worker thread except on the first
148 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700149 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700153 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
156 // created by LauncherModel that are directly on the home screen (however, no widgets or
157 // shortcuts within folders).
158 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
161 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 new ArrayList<LauncherAppWidgetInfo>();
163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700165 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700166
Adam Cohendcd297f2013-06-18 13:13:40 -0700167 // sBgWorkspaceScreens is the ordered set of workspace screens.
168 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
169
Adam Cohen59400422014-03-05 18:07:04 -0800170 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000171 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Reena Lee99a73f32011-10-24 17:27:37 -0700181 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700182
Adam Cohen091440a2015-03-18 14:16:05 -0700183 @Thunk final LauncherAppsCompat mLauncherApps;
184 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700187 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public int getCurrentWorkspaceScreen();
189 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
191 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700192 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700193 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700194 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800195 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200197 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700198 public void bindAppsAdded(ArrayList<Long> newScreens,
199 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700200 ArrayList<ItemInfo> addAnimated,
201 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200202 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700203 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
204 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
205 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700206 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700207 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700208 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700209 public void bindAllPackages(WidgetsModel model);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100210 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700211 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700212 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700213 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chung64359a52013-07-08 17:17:08 -0700216 public interface ItemInfoFilter {
217 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
218 }
219
Bjorn Bringert1307f632013-10-03 22:31:03 +0100220 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800221 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222
Winson Chungee055712013-07-30 14:46:24 -0700223 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700224 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400225 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
226 // resource string.
227 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
228 ProviderInfo providerInfo =
229 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
230 ProviderInfo redirectProvider =
231 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400234 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700235
236 if (mOldContentProviderExists) {
237 Log.d(TAG, "Old launcher provider exists.");
238 } else {
239 Log.d(TAG, "Old launcher provider does not exist.");
240 }
241
Daniel Sandlere4f98912013-06-25 15:13:26 -0400242 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100243 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 mIconCache = iconCache;
245
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400246 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700247 Configuration config = res.getConfiguration();
248 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100249 mLauncherApps = LauncherAppsCompat.getInstance(context);
250 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 }
252
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
254 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 if (sWorkerThread.getThreadId() == Process.myTid()) {
257 // If we are on the worker thread, post onto the main handler
258 mHandler.post(r);
259 } else {
260 r.run();
261 }
262 }
263
264 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
265 * posted on the worker thread handler. */
266 private static void runOnWorkerThread(Runnable r) {
267 if (sWorkerThread.getThreadId() == Process.myTid()) {
268 r.run();
269 } else {
270 // If we are not on the worker thread, then post to the worker handler
271 sWorker.post(r);
272 }
273 }
274
Sunny Goyal756a28a2015-04-23 17:07:55 -0700275 /**
276 * Runs the specified runnable after the loader is complete
277 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700278 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700279 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
280 synchronized (mBindCompleteRunnables) {
281 mBindCompleteRunnables.add(r);
282 }
283 } else {
284 runOnWorkerThread(r);
285 }
286 }
287
Winson Chunge43a1e72014-01-15 10:33:02 -0800288 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
289 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800290 }
291
Sunny Goyal756adbc2015-04-16 15:20:51 -0700292 public void setPackageState(final PackageInstallInfo installInfo) {
293 Runnable updateRunnable = new Runnable() {
294
295 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500296 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700297 synchronized (sBgLock) {
298 final HashSet<ItemInfo> updates = new HashSet<>();
299
300 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
301 // Ignore install success events as they are handled by Package add events.
302 return;
303 }
304
Sunny Goyale2df0622015-04-24 11:27:00 -0700305 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700306 if (info instanceof ShortcutInfo) {
307 ShortcutInfo si = (ShortcutInfo) info;
308 ComponentName cn = si.getTargetComponent();
309 if (si.isPromise() && (cn != null)
310 && installInfo.packageName.equals(cn.getPackageName())) {
311 si.setInstallProgress(installInfo.progress);
312
313 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
314 // Mark this info as broken.
315 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
316 }
317 updates.add(si);
318 }
319 }
320 }
321
322 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
323 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
324 widget.installProgress = installInfo.progress;
325 updates.add(widget);
326 }
327 }
328
329 if (!updates.isEmpty()) {
330 // Push changes to the callback.
331 Runnable r = new Runnable() {
332 public void run() {
333 Callbacks callbacks = getCallback();
334 if (callbacks != null) {
335 callbacks.bindRestoreItemsChange(updates);
336 }
337 }
338 };
339 mHandler.post(r);
340 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500341 }
342 }
343 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500345 }
346
Sunny Goyal756adbc2015-04-16 15:20:51 -0700347 /**
348 * Updates the icons and label of all pending icons for the provided package name.
349 */
350 public void updateSessionDisplayInfo(final String packageName) {
351 Runnable updateRunnable = new Runnable() {
352
353 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700354 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700355 synchronized (sBgLock) {
356 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
357 final UserHandleCompat user = UserHandleCompat.myUserHandle();
358
Sunny Goyale2df0622015-04-24 11:27:00 -0700359 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700360 if (info instanceof ShortcutInfo) {
361 ShortcutInfo si = (ShortcutInfo) info;
362 ComponentName cn = si.getTargetComponent();
363 if (si.isPromise() && (cn != null)
364 && packageName.equals(cn.getPackageName())) {
365 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
366 // For auto install apps update the icon as well as label.
367 mIconCache.getTitleAndIcon(si,
368 si.promisedIntent, user,
369 si.shouldUseLowResIcon());
370 } else {
371 // Only update the icon for restored apps.
372 si.updateIcon(mIconCache);
373 }
374 updates.add(si);
375 }
376 }
377 }
378
379 if (!updates.isEmpty()) {
380 // Push changes to the callback.
381 Runnable r = new Runnable() {
382 public void run() {
383 Callbacks callbacks = getCallback();
384 if (callbacks != null) {
385 callbacks.bindShortcutsChanged(updates,
386 new ArrayList<ShortcutInfo>(), user);
387 }
388 }
389 };
390 mHandler.post(r);
391 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700392 }
393 }
394 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700395 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700396 }
397
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800399 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800400
401 if (allAppsApps == null) {
402 throw new RuntimeException("allAppsApps must not be null");
403 }
404 if (allAppsApps.isEmpty()) {
405 return;
406 }
407
408 // Process the newly added applications and add them to the database first
409 Runnable r = new Runnable() {
410 public void run() {
411 runOnMainThread(new Runnable() {
412 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800413 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800414 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500415 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800416 }
417 }
418 });
419 }
420 };
421 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700422 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800423
Sunny Goyala9116722015-04-29 13:55:58 -0700424 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800425 int[] xy, int spanX, int spanY) {
426 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700427 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
428 final int xCount = (int) profile.numColumns;
429 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 boolean[][] occupied = new boolean[xCount][yCount];
431 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700432 for (ItemInfo r : occupiedPos) {
433 int right = r.cellX + r.spanX;
434 int bottom = r.cellY + r.spanY;
435 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
436 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 occupied[x][y] = true;
438 }
439 }
440 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800441 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700442 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800443 }
444
445 /**
446 * Find a position on the screen for the given size or adds a new screen.
447 * @return screenId and the coordinates for the item.
448 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700449 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 ArrayList<Long> workspaceScreens,
452 ArrayList<Long> addedWorkspaceScreensFinal,
453 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700454 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800455
Sunny Goyala9116722015-04-29 13:55:58 -0700456 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700457 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700458 synchronized (sBgLock) {
459 for (ItemInfo info : sBgItemsIdMap) {
460 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
461 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
462 if (items == null) {
463 items = new ArrayList<>();
464 screenItems.put(info.screenId, items);
465 }
466 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 }
470
471 // Find appropriate space for the item.
472 long screenId = 0;
473 int[] cordinates = new int[2];
474 boolean found = false;
475
476 int screenCount = workspaceScreens.size();
477 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700478 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 if (preferredScreenIndex < screenCount) {
480 screenId = workspaceScreens.get(preferredScreenIndex);
481 found = findNextAvailableIconSpaceInScreen(
482 screenItems.get(screenId), cordinates, spanX, spanY);
483 }
484
485 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700486 // Search on any of the screens starting from the first screen.
487 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800488 screenId = workspaceScreens.get(screen);
489 if (findNextAvailableIconSpaceInScreen(
490 screenItems.get(screenId), cordinates, spanX, spanY)) {
491 // We found a space for it
492 found = true;
493 break;
494 }
495 }
496 }
497
498 if (!found) {
499 // Still no position found. Add a new screen to the end.
500 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
501
502 // Save the screen id for binding in the workspace
503 workspaceScreens.add(screenId);
504 addedWorkspaceScreensFinal.add(screenId);
505
506 // If we still can't find an empty space, then God help us all!!!
507 if (!findNextAvailableIconSpaceInScreen(
508 screenItems.get(screenId), cordinates, spanX, spanY)) {
509 throw new RuntimeException("Can't find space to add the item");
510 }
511 }
512 return Pair.create(screenId, cordinates);
513 }
514
515 /**
516 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700518 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700519 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800521 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700522 return;
Winson Chung997a9232013-07-24 15:33:46 -0700523 }
Winson Chung64359a52013-07-08 17:17:08 -0700524 // Process the newly added applications and add them to the database first
525 Runnable r = new Runnable() {
526 public void run() {
527 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
528 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
529
Winson Chung76828c82013-08-19 15:43:29 -0700530 // Get the list of workspace screens. We need to append to this list and
531 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
532 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800533 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700534 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700536 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700538 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700540 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 }
Winson Chung76828c82013-08-19 15:43:29 -0700542
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800543 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700544 Pair<Long, int[]> coords = findSpaceForItem(context,
545 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800546 1, 1);
547 long screenId = coords.first;
548 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700549
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 ItemInfo itemInfo;
551 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
552 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800553 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700554 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700555 } else {
556 throw new RuntimeException("Unexpected info type");
557 }
Winson Chung94d67682013-09-25 16:29:40 -0700558
Winson Chung64359a52013-07-08 17:17:08 -0700559 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700560 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700561 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700562 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700563 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700564 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700565 }
566 }
567
Winson Chung76828c82013-08-19 15:43:29 -0700568 // Update the workspace screens
569 updateWorkspaceScreenOrder(context, workspaceScreens);
570
Adam Cohen76a47a12014-02-05 11:47:43 -0800571 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700572 runOnMainThread(new Runnable() {
573 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800574 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700575 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700576 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
577 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700578 if (!addedShortcutsFinal.isEmpty()) {
579 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
580 long lastScreenId = info.screenId;
581 for (ItemInfo i : addedShortcutsFinal) {
582 if (i.screenId == lastScreenId) {
583 addAnimated.add(i);
584 } else {
585 addNotAnimated.add(i);
586 }
Winson Chung997a9232013-07-24 15:33:46 -0700587 }
588 }
Winson Chungd64d1762013-08-20 14:37:16 -0700589 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800590 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700591 }
Winson Chung64359a52013-07-08 17:17:08 -0700592 }
Winson Chung997a9232013-07-24 15:33:46 -0700593 });
594 }
Winson Chung64359a52013-07-08 17:17:08 -0700595 }
596 };
597 runOnWorkerThread(r);
598 }
599
Sunny Goyald33860f2015-04-23 16:02:20 -0700600 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700601 if (sWorkerThread.getThreadId() == Process.myTid()) {
602 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
603 "main thread");
604 }
605
606 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400607 synchronized (mDeferredBindRunnables) {
608 mDeferredBindRunnables.clear();
609 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700610
611 // Remove any queued UI runnables
612 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700613 // Unbind all the workspace items
614 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700615 }
616
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700618 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700619 // Ensure that we don't use the same workspace items data structure on the main thread
620 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700621 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700622 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700623 tmpItems.addAll(sBgWorkspaceItems);
624 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 }
626 Runnable r = new Runnable() {
627 @Override
628 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700629 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 item.unbind();
631 }
632 }
633 };
634 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700635 }
636
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 /**
638 * Adds an item to the DB if it was not created previously, or move it to a new
639 * <container, screen, cellX, cellY>
640 */
641 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 if (item.container == ItemInfo.NO_ID) {
644 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700645 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 } else {
647 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
650 }
651
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700652 static void checkItemInfoLocked(
653 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
654 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
655 if (modelItem != null && item != modelItem) {
656 // check all the data is consistent
657 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
658 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
659 ShortcutInfo shortcut = (ShortcutInfo) item;
660 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
661 modelShortcut.intent.filterEquals(shortcut.intent) &&
662 modelShortcut.id == shortcut.id &&
663 modelShortcut.itemType == shortcut.itemType &&
664 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700665 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700666 modelShortcut.cellX == shortcut.cellX &&
667 modelShortcut.cellY == shortcut.cellY &&
668 modelShortcut.spanX == shortcut.spanX &&
669 modelShortcut.spanY == shortcut.spanY &&
670 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
671 (modelShortcut.dropPos != null &&
672 shortcut.dropPos != null &&
673 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
674 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
675 // For all intents and purposes, this is the same object
676 return;
677 }
678 }
679
680 // the modelItem needs to match up perfectly with item if our model is
681 // to be consistent with the database-- for now, just require
682 // modelItem == item or the equality check above
683 String msg = "item: " + ((item != null) ? item.toString() : "null") +
684 "modelItem: " +
685 ((modelItem != null) ? modelItem.toString() : "null") +
686 "Error: ItemInfo passed to checkItemInfo doesn't match original";
687 RuntimeException e = new RuntimeException(msg);
688 if (stackTrace != null) {
689 e.setStackTrace(stackTrace);
690 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800691 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700692 }
693 }
694
Michael Jurka816474f2012-06-25 14:49:02 -0700695 static void checkItemInfo(final ItemInfo item) {
696 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
697 final long itemId = item.id;
698 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700699 public void run() {
700 synchronized (sBgLock) {
701 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700702 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700703 }
704 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700705 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700706 }
707
Michael Jurkac9d95c52011-08-29 14:03:34 -0700708 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
709 final ItemInfo item, final String callingFunction) {
710 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700711 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712 final ContentResolver cr = context.getContentResolver();
713
Adam Cohen487f7dd2012-06-28 18:12:10 -0700714 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700715 Runnable r = new Runnable() {
716 public void run() {
717 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 updateItemArrays(item, itemId, stackTrace);
719 }
720 };
721 runOnWorkerThread(r);
722 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700724 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
725 final ArrayList<ItemInfo> items, final String callingFunction) {
726 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700727
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
729 Runnable r = new Runnable() {
730 public void run() {
731 ArrayList<ContentProviderOperation> ops =
732 new ArrayList<ContentProviderOperation>();
733 int count = items.size();
734 for (int i = 0; i < count; i++) {
735 ItemInfo item = items.get(i);
736 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700737 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700739
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
741 updateItemArrays(item, itemId, stackTrace);
742
743 }
744 try {
745 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
746 } catch (Exception e) {
747 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700748 }
749 }
750 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700751 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700752 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700753
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
755 // Lock on mBgLock *after* the db operation
756 synchronized (sBgLock) {
757 checkItemInfoLocked(itemId, item, stackTrace);
758
759 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
760 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
761 // Item is in a folder, make sure this folder exists
762 if (!sBgFolders.containsKey(item.container)) {
763 // An items container is being set to a that of an item which is not in
764 // the list of Folders.
765 String msg = "item: " + item + " container being set to: " +
766 item.container + ", not in the list of folders";
767 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 }
769 }
770
771 // Items are added/removed from the corresponding FolderInfo elsewhere, such
772 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
773 // that are on the desktop, as appropriate
774 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800775 if (modelItem != null &&
776 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
777 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700778 switch (modelItem.itemType) {
779 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
780 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
781 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
782 if (!sBgWorkspaceItems.contains(modelItem)) {
783 sBgWorkspaceItems.add(modelItem);
784 }
785 break;
786 default:
787 break;
788 }
789 } else {
790 sBgWorkspaceItems.remove(modelItem);
791 }
792 }
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700797 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700798 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 item.cellX = cellX;
802 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700803
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 // We store hotseat items in canonical form which is this orientation invariant position
805 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700810 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812
813 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700815 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
816 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800817 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700818 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819
Michael Jurkac9d95c52011-08-29 14:03:34 -0700820 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700821 }
822
823 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
825 * cellX, cellY have already been updated on the ItemInfos.
826 */
827 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
828 final long container, final int screen) {
829
830 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
831 int count = items.size();
832
833 for (int i = 0; i < count; i++) {
834 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835 item.container = container;
836
837 // We store hotseat items in canonical form which is this orientation invariant position
838 // in the hotseat
839 if (context instanceof Launcher && screen < 0 &&
840 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 item.cellY);
843 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700845 }
846
847 final ContentValues values = new ContentValues();
848 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
849 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
850 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800851 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700853
854 contentValues.add(values);
855 }
856 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
857 }
858
859 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800861 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700864 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800865 item.cellX = cellX;
866 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 item.spanX = spanX;
868 item.spanY = spanY;
869
870 // We store hotseat items in canonical form which is this orientation invariant position
871 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700877 }
Adam Cohend4844c32011-02-18 19:25:06 -0800878
Adam Cohend4844c32011-02-18 19:25:06 -0800879 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800880 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700881 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
882 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800883 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700884 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
885 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800887
Michael Jurka816474f2012-06-25 14:49:02 -0700888 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700889 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700890
891 /**
892 * Update an item to the database in a specified container.
893 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700894 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700895 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100896 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700897 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800898 }
899
Sunny Goyal756a28a2015-04-23 17:07:55 -0700900 private void assertWorkspaceLoaded() {
901 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
902 throw new RuntimeException("Trying to add shortcut while loader is running");
903 }
904 }
905
Adam Cohend4844c32011-02-18 19:25:06 -0800906 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700907 * Returns true if the shortcuts already exists on the workspace. This must be called after
908 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700910 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
911 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700913 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 if (intent.getComponent() != null) {
915 // If component is not null, an intent with null package will produce
916 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700919 intentWithPkg = intent.toUri(0);
920 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700921 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700922 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
923 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700924 }
925 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700926 intentWithPkg = intent.toUri(0);
927 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700928 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700929 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700930
931 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700932 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700933 if (item instanceof ShortcutInfo) {
934 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700935 if (info.getIntent() != null && info.user.equals(user)) {
936 String s = info.getIntent().toUri(0);
937 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
938 return true;
939 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700940 }
941 }
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700944 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700945 }
946
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 /**
948 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
949 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700950 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 final ContentResolver cr = context.getContentResolver();
952 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
953 "_id=? and (itemType=? or itemType=?)",
954 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700955 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700956
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 try {
958 if (c.moveToFirst()) {
959 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
960 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
961 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
962 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
963 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
964 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700965 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 FolderInfo folderInfo = null;
968 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700969 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
970 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700972 }
973
Sunny Goyala508e4f2015-05-21 09:33:57 -0700974 // Do not trim the folder label, as is was set by the user.
975 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 folderInfo.id = id;
977 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700979 folderInfo.cellX = c.getInt(cellXIndex);
980 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700981 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
983 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 } finally {
986 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700987 }
988
989 return null;
990 }
991
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 /**
993 * Add an item to the database in a specified container. Sets the container, screen, cellX and
994 * cellY fields of the item. Also assigns an ID to the item.
995 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700996 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700997 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 item.cellX = cellX;
1000 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 // We store hotseat items in canonical form which is this orientation invariant position
1002 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001007 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009
1010 final ContentValues values = new ContentValues();
1011 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001012 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013
Michael Jurka414300a2013-08-27 15:42:35 +02001014 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001016
Jason Monk8e19cf22014-03-20 15:06:57 -04001017 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001018 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001019 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001020 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001023 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001024 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025 sBgItemsIdMap.put(item.id, item);
1026 switch (item.itemType) {
1027 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1028 sBgFolders.put(item.id, (FolderInfo) item);
1029 // Fall through
1030 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1031 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1032 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1033 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1034 sBgWorkspaceItems.add(item);
1035 } else {
1036 if (!sBgFolders.containsKey(item.container)) {
1037 // Adding an item to a folder that doesn't exist.
1038 String msg = "adding item: " + item + " to a folder that " +
1039 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001040 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001041 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001042 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001043 break;
1044 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1045 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1046 break;
1047 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048 }
1049 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001050 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001052 }
1053
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001055 * Creates a new unique child id, for a given cell span across all layouts.
1056 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001057 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001059 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001061 }
1062
Sunny Goyal34942622014-08-29 17:20:55 -07001063 private static ArrayList<ItemInfo> getItemsByPackageName(
1064 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001065 ItemInfoFilter filter = new ItemInfoFilter() {
1066 @Override
1067 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1068 return cn.getPackageName().equals(pn) && info.user.equals(user);
1069 }
1070 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001071 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001072 }
1073
1074 /**
1075 * Removes all the items from the database corresponding to the specified package.
1076 */
1077 static void deletePackageFromDatabase(Context context, final String pn,
1078 final UserHandleCompat user) {
1079 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001080 }
1081
1082 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001083 * Removes the specified item from the database
1084 * @param context
1085 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001087 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001088 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1089 items.add(item);
1090 deleteItemsFromDatabase(context, items);
1091 }
1092
1093 /**
1094 * Removes the specified items from the database
1095 * @param context
1096 * @param item
1097 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001098 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001100 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001101 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001102 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001103 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001104 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001105
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001106 // Lock on mBgLock *after* the db operation
1107 synchronized (sBgLock) {
1108 switch (item.itemType) {
1109 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1110 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001111 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001112 if (info.container == item.id) {
1113 // We are deleting a folder which still contains items that
1114 // think they are contained by that folder.
1115 String msg = "deleting a folder (" + item + ") which still " +
1116 "contains items (" + info + ")";
1117 Log.e(TAG, msg);
1118 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001120 sBgWorkspaceItems.remove(item);
1121 break;
1122 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1123 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1124 sBgWorkspaceItems.remove(item);
1125 break;
1126 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1127 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1128 break;
1129 }
1130 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001132 }
1133 }
Michael Jurka83df1882011-08-31 20:59:26 -07001134 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 }
1137
1138 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 * Update the order of the workspace screens in the database. The array list contains
1140 * a list of screen ids in the order that they should appear.
1141 */
Winson Chungc9168342013-06-26 14:54:55 -07001142 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001143 // Log to disk
1144 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1145 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1146
Winson Chung64359a52013-07-08 17:17:08 -07001147 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 final ContentResolver cr = context.getContentResolver();
1149 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1150
1151 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001152 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 while (iter.hasNext()) {
1154 long id = iter.next();
1155 if (id < 0) {
1156 iter.remove();
1157 }
1158 }
1159
1160 Runnable r = new Runnable() {
1161 @Override
1162 public void run() {
Yura085c8532014-02-11 15:15:29 +00001163 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001165 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001166 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 for (int i = 0; i < count; i++) {
1168 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001169 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1171 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001172 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001173 }
Yura085c8532014-02-11 15:15:29 +00001174
1175 try {
1176 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1177 } catch (Exception ex) {
1178 throw new RuntimeException(ex);
1179 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001180
Winson Chungba9c37f2013-08-30 14:11:37 -07001181 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001182 sBgWorkspaceScreens.clear();
1183 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001184 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 }
1186 };
1187 runOnWorkerThread(r);
1188 }
1189
1190 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 * Remove the contents of the specified folder from the database
1192 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001193 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 final ContentResolver cr = context.getContentResolver();
1195
Michael Jurkac9d95c52011-08-29 14:03:34 -07001196 Runnable r = new Runnable() {
1197 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001198 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001200 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 sBgItemsIdMap.remove(info.id);
1202 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 sBgWorkspaceItems.remove(info);
1204 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001205
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001206 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001207 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001209 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 for (ItemInfo childInfo : info.contents) {
1211 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001213 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001214 }
1215 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 }
1218
1219 /**
1220 * Set this as the current Launcher activity object for the loader.
1221 */
1222 public void initialize(Callbacks callbacks) {
1223 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001224 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1225 // workspace to prevent leaking Launcher activities on orientation change.
1226 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001227 mCallbacks = new WeakReference<Callbacks>(callbacks);
1228 }
1229 }
1230
Kenny Guyed131872014-04-30 03:02:21 +01001231 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001232 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001233 int op = PackageUpdatedTask.OP_UPDATE;
1234 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1235 user));
1236 }
1237
1238 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001239 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001240 int op = PackageUpdatedTask.OP_REMOVE;
1241 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1242 user));
1243 }
1244
1245 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001246 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001247 int op = PackageUpdatedTask.OP_ADD;
1248 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1249 user));
1250 }
1251
1252 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001253 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001254 boolean replacing) {
1255 if (!replacing) {
1256 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1257 user));
1258 if (mAppsCanBeOnRemoveableStorage) {
1259 // Only rebind if we support removable storage. It catches the
1260 // case where
1261 // apps on the external sd card need to be reloaded
1262 startLoaderFromBackground();
1263 }
1264 } else {
1265 // If we are replacing then just update the packages in the list
1266 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1267 packageNames, user));
1268 }
1269 }
1270
1271 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001272 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001273 boolean replacing) {
1274 if (!replacing) {
1275 enqueuePackageUpdated(new PackageUpdatedTask(
1276 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1277 user));
1278 }
Kenny Guyed131872014-04-30 03:02:21 +01001279 }
1280
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001281 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1283 * ACTION_PACKAGE_CHANGED.
1284 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001285 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001286 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001287 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001288
Joe Onorato36115782010-06-17 13:28:48 -04001289 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001290 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001291 // If we have changed locale we need to clear out the labels in all apps/workspace.
1292 forceReload();
1293 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1294 // Check if configuration change was an mcc/mnc change which would affect app resources
1295 // and we would need to clear out the labels in all apps/workspace. Same handling as
1296 // above for ACTION_LOCALE_CHANGED
1297 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001298 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001299 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001300 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001301 forceReload();
1302 }
1303 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001304 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001305 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1306 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001307 Callbacks callbacks = getCallback();
1308 if (callbacks != null) {
1309 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001310 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001311 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1312 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1313 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001314 }
1315 }
1316
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001317 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001318 resetLoadedState(true, true);
1319
Reena Lee93f824a2011-09-23 17:20:28 -07001320 // Do this here because if the launcher activity is running it will be restarted.
1321 // If it's not running startLoaderFromBackground will merely tell it that it needs
1322 // to reload.
1323 startLoaderFromBackground();
1324 }
1325
Winson Chungf0c6ae02012-03-21 16:10:31 -07001326 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1327 synchronized (mLock) {
1328 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1329 // mWorkspaceLoaded to true later
1330 stopLoaderLocked();
1331 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1332 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1333 }
1334 }
1335
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001336 /**
1337 * When the launcher is in the background, it's possible for it to miss paired
1338 * configuration changes. So whenever we trigger the loader from the background
1339 * tell the launcher that it needs to re-run the loader when it comes back instead
1340 * of doing it now.
1341 */
1342 public void startLoaderFromBackground() {
1343 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001344 Callbacks callbacks = getCallback();
1345 if (callbacks != null) {
1346 // Only actually run the loader if they're not paused.
1347 if (!callbacks.setLoadOnResume()) {
1348 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001349 }
1350 }
1351 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001352 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001353 }
Joe Onorato36115782010-06-17 13:28:48 -04001354 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001355
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001356 /**
1357 * If there is already a loader task running, tell it to stop.
1358 */
1359 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001360 LoaderTask oldTask = mLoaderTask;
1361 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001362 oldTask.stopLocked();
1363 }
Reena Lee93f824a2011-09-23 17:20:28 -07001364 }
1365
Adam Cohen1a85c582014-09-30 09:48:49 -07001366 public boolean isCurrentCallbacks(Callbacks callbacks) {
1367 return (mCallbacks != null && mCallbacks.get() == callbacks);
1368 }
1369
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001370 public void startLoader(int synchronousBindPage) {
1371 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001372 }
1373
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001374 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001375 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1376 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001377 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 // Clear any deferred bind-runnables from the synchronized load process
1379 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001380 synchronized (mDeferredBindRunnables) {
1381 mDeferredBindRunnables.clear();
1382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383
Joe Onorato36115782010-06-17 13:28:48 -04001384 // Don't bother to start the thread if we know it's not going to do anything
1385 if (mCallbacks != null && mCallbacks.get() != null) {
1386 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001387 stopLoaderLocked();
1388 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001389 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001390 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1392 } else {
1393 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1394 sWorker.post(mLoaderTask);
1395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001397 }
1398 }
1399
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 void bindRemainingSynchronousPages() {
1401 // Post the remaining side pages to be loaded
1402 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001403 Runnable[] deferredBindRunnables = null;
1404 synchronized (mDeferredBindRunnables) {
1405 deferredBindRunnables = mDeferredBindRunnables.toArray(
1406 new Runnable[mDeferredBindRunnables.size()]);
1407 mDeferredBindRunnables.clear();
1408 }
1409 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001410 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001412 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001413
1414 // Run all the bind complete runnables after workspace is bound.
1415 if (!mBindCompleteRunnables.isEmpty()) {
1416 synchronized (mBindCompleteRunnables) {
1417 for (final Runnable r : mBindCompleteRunnables) {
1418 runOnWorkerThread(r);
1419 }
1420 mBindCompleteRunnables.clear();
1421 }
1422 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001423 }
1424
Joe Onorato36115782010-06-17 13:28:48 -04001425 public void stopLoader() {
1426 synchronized (mLock) {
1427 if (mLoaderTask != null) {
1428 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 }
1430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001433 /**
1434 * Loads the workspace screen ids in an ordered list.
1435 */
Adam Cohen091440a2015-03-18 14:16:05 -07001436 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001437 final ContentResolver contentResolver = context.getContentResolver();
1438 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001439
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001440 // Get screens ordered by rank.
1441 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1442 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1443 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001444 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001445 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001446 while (sc.moveToNext()) {
1447 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001448 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001449 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001450 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1451 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001452 }
1453 }
1454 } finally {
1455 sc.close();
1456 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001457 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001458 }
1459
Michael Jurkac57b7a82011-08-09 22:02:20 -07001460 public boolean isAllAppsLoaded() {
1461 return mAllAppsLoaded;
1462 }
1463
Winson Chung36a62fe2012-05-06 18:04:42 -07001464 boolean isLoadingWorkspace() {
1465 synchronized (mLock) {
1466 if (mLoaderTask != null) {
1467 return mLoaderTask.isLoadingWorkspace();
1468 }
1469 }
1470 return false;
1471 }
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 /**
1474 * Runnable for the thread that loads the contents of the launcher:
1475 * - workspace icons
1476 * - widgets
1477 * - all apps icons
1478 */
1479 private class LoaderTask implements Runnable {
1480 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001481 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001482 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001483 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001484 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001485
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001486 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001487 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001488 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
1490
Winson Chung36a62fe2012-05-06 18:04:42 -07001491 boolean isLoadingWorkspace() {
1492 return mIsLoadingAndBindingWorkspace;
1493 }
1494
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001495 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001496 mIsLoadingAndBindingWorkspace = true;
1497
Joe Onorato36115782010-06-17 13:28:48 -04001498 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001499 if (DEBUG_LOADERS) {
1500 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001502
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001503 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001504 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001505 synchronized (LoaderTask.this) {
1506 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001507 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001508 }
1509 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 }
1511 }
1512
Joe Onorato36115782010-06-17 13:28:48 -04001513 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001514 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001515 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001516
Joe Onorato36115782010-06-17 13:28:48 -04001517 private void waitForIdle() {
1518 // Wait until the either we're stopped or the other threads are done.
1519 // This way we don't start loading all apps until the workspace has settled
1520 // down.
1521 synchronized (LoaderTask.this) {
1522 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001523
Joe Onorato36115782010-06-17 13:28:48 -04001524 mHandler.postIdle(new Runnable() {
1525 public void run() {
1526 synchronized (LoaderTask.this) {
1527 mLoadAndBindStepFinished = true;
1528 if (DEBUG_LOADERS) {
1529 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001532 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001533 }
Joe Onorato36115782010-06-17 13:28:48 -04001534 });
1535
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001536 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001537 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001538 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1539 // wait no longer than 1sec at a time
1540 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001541 } catch (InterruptedException ex) {
1542 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001543 }
1544 }
Joe Onorato36115782010-06-17 13:28:48 -04001545 if (DEBUG_LOADERS) {
1546 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001547 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001548 + "ms for previous step to finish binding");
1549 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001550 }
Joe Onorato36115782010-06-17 13:28:48 -04001551 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001552
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001554 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 // Ensure that we have a valid page index to load synchronously
1556 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1557 "valid page index");
1558 }
1559 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1560 // Ensure that we don't try and bind a specified page when the pages have not been
1561 // loaded already (we should load everything asynchronously in that case)
1562 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1563 }
1564 synchronized (mLock) {
1565 if (mIsLoaderTaskRunning) {
1566 // Ensure that we are never running the background loading at this point since
1567 // we also touch the background collections
1568 throw new RuntimeException("Error! Background loading is already running");
1569 }
1570 }
1571
1572 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1573 // data structures, we can't allow any other thread to touch that data, but because
1574 // this call is synchronous, we can get away with not locking).
1575
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001576 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001577 // operations from the previous activity. We need to ensure that all queued operations
1578 // are executed before any synchronous binding work is done.
1579 mHandler.flush();
1580
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 // Divide the set of loaded items into those that we are binding synchronously, and
1582 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001583 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001584 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1585 // arise from that.
1586 onlyBindAllApps();
1587 }
1588
Joe Onorato36115782010-06-17 13:28:48 -04001589 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001590 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001591 if (mStopped) {
1592 return;
1593 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001594 mIsLoaderTaskRunning = true;
1595 }
Joe Onorato36115782010-06-17 13:28:48 -04001596 // Optimize for end-user experience: if the Launcher is up and // running with the
1597 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1598 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001599 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001600 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001601 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001602
Joe Onorato36115782010-06-17 13:28:48 -04001603 if (mStopped) {
1604 break keep_running;
1605 }
1606
Joe Onorato36115782010-06-17 13:28:48 -04001607 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001608
1609 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001610 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1611 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001612 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613
Joe Onorato36115782010-06-17 13:28:48 -04001614 // Clear out this reference, otherwise we end up holding it until all of the
1615 // callback runnables are done.
1616 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617
Joe Onorato36115782010-06-17 13:28:48 -04001618 synchronized (mLock) {
1619 // If we are still the last one to be scheduled, remove ourselves.
1620 if (mLoaderTask == this) {
1621 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001624 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
1627
1628 public void stopLocked() {
1629 synchronized (LoaderTask.this) {
1630 mStopped = true;
1631 this.notify();
1632 }
1633 }
1634
1635 /**
1636 * Gets the callbacks object. If we've been stopped, or if the launcher object
1637 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1638 * object that was around when the deferred message was scheduled, and if there's
1639 * a new Callbacks object around then also return null. This will save us from
1640 * calling onto it with data that will be ignored.
1641 */
1642 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1643 synchronized (mLock) {
1644 if (mStopped) {
1645 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001646 }
Joe Onorato36115782010-06-17 13:28:48 -04001647
1648 if (mCallbacks == null) {
1649 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001650 }
Joe Onorato36115782010-06-17 13:28:48 -04001651
1652 final Callbacks callbacks = mCallbacks.get();
1653 if (callbacks != oldCallbacks) {
1654 return null;
1655 }
1656 if (callbacks == null) {
1657 Log.w(TAG, "no mCallbacks");
1658 return null;
1659 }
1660
1661 return callbacks;
1662 }
1663 }
1664
1665 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001666 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001667 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001668 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1669 final int countX = (int) profile.numColumns;
1670 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001671
Adam Cohendcd297f2013-06-18 13:13:40 -07001672 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001673 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001674 // Return early if we detect that an item is under the hotseat button
1675 if (mCallbacks == null ||
1676 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001677 Log.e(TAG, "Error loading shortcut into hotseat " + item
1678 + " into position (" + item.screenId + ":" + item.cellX + ","
1679 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001680 return false;
1681 }
1682
Dan Sandler295ae182013-12-10 16:05:47 -05001683 final ItemInfo[][] hotseatItems =
1684 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1685
Adam Cohen2e6da152015-05-06 11:42:25 -07001686 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001687 Log.e(TAG, "Error loading shortcut " + item
1688 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001689 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001690 + ")");
1691 return false;
1692 }
1693
Dan Sandler295ae182013-12-10 16:05:47 -05001694 if (hotseatItems != null) {
1695 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 Log.e(TAG, "Error loading shortcut into hotseat " + item
1697 + " into position (" + item.screenId + ":" + item.cellX + ","
1698 + item.cellY + ") occupied by "
1699 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1700 [(int) item.screenId][0]);
1701 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001702 } else {
1703 hotseatItems[(int) item.screenId][0] = item;
1704 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001706 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001707 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001708 items[(int) item.screenId][0] = item;
1709 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001710 return true;
1711 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001712 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1713 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001714 return true;
1715 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001716
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001718 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 occupied.put(item.screenId, items);
1720 }
1721
Dan Sandler295ae182013-12-10 16:05:47 -05001722 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001723 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1724 item.cellX < 0 || item.cellY < 0 ||
1725 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1726 Log.e(TAG, "Error loading shortcut " + item
1727 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1728 + item.cellX + "," + item.cellY
1729 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1730 return false;
1731 }
1732
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001733 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001734 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1735 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001737 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001738 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001739 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001740 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001741 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001742 return false;
1743 }
1744 }
1745 }
1746 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1747 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001748 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001749 }
1750 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001751
Joe Onorato36115782010-06-17 13:28:48 -04001752 return true;
1753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754
Winson Chungba9c37f2013-08-30 14:11:37 -07001755 /** Clears all the sBg data structures */
1756 private void clearSBgDataStructures() {
1757 synchronized (sBgLock) {
1758 sBgWorkspaceItems.clear();
1759 sBgAppWidgets.clear();
1760 sBgFolders.clear();
1761 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001762 sBgWorkspaceScreens.clear();
1763 }
1764 }
1765
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001766 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001767 // Log to disk
1768 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1769
Joe Onorato36115782010-06-17 13:28:48 -04001770 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771
Joe Onorato36115782010-06-17 13:28:48 -04001772 final Context context = mContext;
1773 final ContentResolver contentResolver = context.getContentResolver();
1774 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001775 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001776 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1777 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001778 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001779
Winson Chung892c74d2013-08-22 16:15:50 -07001780 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001781 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1782 int countX = (int) profile.numColumns;
1783 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001784
Dan Sandlerd5024042014-01-09 15:01:33 -05001785 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1786 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1787 LauncherAppState.getLauncherProvider().deleteDatabase();
1788 }
1789
1790 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1791 // append the user's Launcher2 shortcuts
1792 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1793 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1794 } else {
1795 // Make sure the default workspace is loaded
1796 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001797 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001798 }
Adam Cohene25af792013-06-06 23:08:25 -07001799
Winson Chung2abf94d2012-07-18 18:16:38 -07001800 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001801 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001802 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001803 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001804
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001806 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001807 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001808 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001809 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001810
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 // +1 for the hotseat (it can be larger than the workspace)
1812 // Load workspace in reverse order to ensure that latest items are loaded first (and
1813 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001814 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 try {
1817 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1818 final int intentIndex = c.getColumnIndexOrThrow
1819 (LauncherSettings.Favorites.INTENT);
1820 final int titleIndex = c.getColumnIndexOrThrow
1821 (LauncherSettings.Favorites.TITLE);
1822 final int iconTypeIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.ICON_TYPE);
1824 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1825 final int iconPackageIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.ICON_PACKAGE);
1827 final int iconResourceIndex = c.getColumnIndexOrThrow(
1828 LauncherSettings.Favorites.ICON_RESOURCE);
1829 final int containerIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.CONTAINER);
1831 final int itemTypeIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.ITEM_TYPE);
1833 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1834 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001835 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1836 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 final int screenIndex = c.getColumnIndexOrThrow(
1838 LauncherSettings.Favorites.SCREEN);
1839 final int cellXIndex = c.getColumnIndexOrThrow
1840 (LauncherSettings.Favorites.CELLX);
1841 final int cellYIndex = c.getColumnIndexOrThrow
1842 (LauncherSettings.Favorites.CELLY);
1843 final int spanXIndex = c.getColumnIndexOrThrow
1844 (LauncherSettings.Favorites.SPANX);
1845 final int spanYIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001847 final int rankIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001849 final int restoredIndex = c.getColumnIndexOrThrow(
1850 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001851 final int profileIdIndex = c.getColumnIndexOrThrow(
1852 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001853 final int optionsIndex = c.getColumnIndexOrThrow(
1854 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855
Sunny Goyal7f834d22015-04-21 10:10:23 -07001856 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1857 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1858 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1859 }
1860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 ShortcutInfo info;
1862 String intentDescription;
1863 LauncherAppWidgetInfo appWidgetInfo;
1864 int container;
1865 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001866 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001868 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 while (!mStopped && c.moveToNext()) {
1871 try {
1872 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001873 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001874 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 switch (itemType) {
1877 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1878 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001879 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001881 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001882 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001883 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001884 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001885 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001886 if (user == null) {
1887 // User has been deleted remove the item.
1888 itemsToRemove.add(id);
1889 continue;
1890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 try {
1892 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001893 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001894 if (cn != null && cn.getPackageName() != null) {
1895 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1896 cn.getPackageName(), user);
1897 boolean validComponent = validPkg &&
1898 launcherApps.isActivityEnabledForProfile(cn, user);
1899
1900 if (validComponent) {
1901 if (restored) {
1902 // no special handling necessary for this item
1903 restoredRows.add(id);
1904 restored = false;
1905 }
1906 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001907 intent = null;
1908 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1909 // We allow auto install apps to have their intent
1910 // updated after an install.
1911 intent = manager.getLaunchIntentForPackage(
1912 cn.getPackageName());
1913 if (intent != null) {
1914 ContentValues values = new ContentValues();
1915 values.put(LauncherSettings.Favorites.INTENT,
1916 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001917 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001918 }
1919 }
1920
1921 if (intent == null) {
1922 // The app is installed but the component is no
1923 // longer available.
1924 Launcher.addDumpLog(TAG,
1925 "Invalid component removed: " + cn, true);
1926 itemsToRemove.add(id);
1927 continue;
1928 } else {
1929 // no special handling necessary for this item
1930 restoredRows.add(id);
1931 restored = false;
1932 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001933 } else if (restored) {
1934 // Package is not yet available but might be
1935 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001936 Launcher.addDumpLog(TAG,
1937 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001938
1939 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1940 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001941 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001942 // App restore has started. Update the flag
1943 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1944 ContentValues values = new ContentValues();
1945 values.put(LauncherSettings.Favorites.RESTORED,
1946 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001947 updateItem(id, values);
1948 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1949 // This is a common app. Try to replace this.
1950 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1951 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1952 if (parser.findDefaultApp()) {
1953 // Default app found. Replace it.
1954 intent = parser.parsedIntent;
1955 cn = intent.getComponent();
1956 ContentValues values = parser.parsedValues;
1957 values.put(LauncherSettings.Favorites.RESTORED, 0);
1958 updateItem(id, values);
1959 restored = false;
1960 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001961
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001962 } else if (REMOVE_UNRESTORED_ICONS) {
1963 Launcher.addDumpLog(TAG,
1964 "Unrestored package removed: " + cn, true);
1965 itemsToRemove.add(id);
1966 continue;
1967 }
Sunny Goyal94485362014-09-18 16:13:58 -07001968 } else if (REMOVE_UNRESTORED_ICONS) {
1969 Launcher.addDumpLog(TAG,
1970 "Unrestored package removed: " + cn, true);
1971 itemsToRemove.add(id);
1972 continue;
1973 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001974 } else if (launcherApps.isAppEnabled(
1975 manager, cn.getPackageName(),
1976 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1977 // Package is present but not available.
1978 allowMissingTarget = true;
1979 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1980 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001981 // SdCard is not ready yet. Package might get available,
1982 // once it is ready.
1983 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1984 + " (check again later)", true);
1985 HashSet<String> pkgs = sPendingPackages.get(user);
1986 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001987 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001988 sPendingPackages.put(user, pkgs);
1989 }
1990 pkgs.add(cn.getPackageName());
1991 allowMissingTarget = true;
1992 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001993
1994 } else {
1995 // Do not wait for external media load anymore.
1996 // Log the invalid package, and remove it
1997 Launcher.addDumpLog(TAG,
1998 "Invalid package removed: " + cn, true);
1999 itemsToRemove.add(id);
2000 continue;
Winson Chungee055712013-07-30 14:46:24 -07002001 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002002 } else if (cn == null) {
2003 // For shortcuts with no component, keep them as they are
2004 restoredRows.add(id);
2005 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002006 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002008 Launcher.addDumpLog(TAG,
2009 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 continue;
2011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012
Sunny Goyal34b65272015-03-11 16:56:52 -07002013 container = c.getInt(containerIndex);
2014 boolean useLowResIcon = container >= 0 &&
2015 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2016
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002017 if (itemReplaced) {
2018 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002019 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002020 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002021 } else {
2022 // Don't replace items for other profiles.
2023 itemsToRemove.add(id);
2024 continue;
2025 }
2026 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002027 if (user.equals(UserHandleCompat.myUserHandle())) {
2028 Launcher.addDumpLog(TAG,
2029 "constructing info for partially restored package",
2030 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002031 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07002032 promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002033 intent = getRestoredItemIntent(c, context, intent);
2034 } else {
2035 // Don't restore items for other profiles.
2036 itemsToRemove.add(id);
2037 continue;
2038 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002039 } else if (itemType ==
2040 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002041 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002042 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 } else {
2044 info = getShortcutInfo(c, context, iconTypeIndex,
2045 iconPackageIndex, iconResourceIndex, iconIndex,
2046 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002047
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 // App shortcuts that used to be automatically added to Launcher
2049 // didn't always have the correct intent flags set, so do that
2050 // here
2051 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002052 intent.getCategories() != null &&
2053 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002054 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 intent.addFlags(
2056 Intent.FLAG_ACTIVITY_NEW_TASK |
2057 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2058 }
Michael Jurka96879562012-03-22 05:54:33 -07002059 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002062 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 info.cellX = c.getInt(cellXIndex);
2067 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002068 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002069 info.spanX = 1;
2070 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002071 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002072 info.isDisabled = disabledState;
2073 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2074 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2075 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002076
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002078 if (!checkItemPlacement(occupied, info)) {
2079 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 break;
2081 }
2082
Sunny Goyal756adbc2015-04-16 15:20:51 -07002083 if (restored) {
2084 ComponentName cn = info.getTargetComponent();
2085 if (cn != null) {
2086 Integer progress = installingPkgs.get(cn.getPackageName());
2087 if (progress != null) {
2088 info.setInstallProgress(progress);
2089 } else {
2090 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2091 }
2092 }
2093 }
2094
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 switch (container) {
2096 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2097 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2098 sBgWorkspaceItems.add(info);
2099 break;
2100 default:
2101 // Item is in a user folder
2102 FolderInfo folderInfo =
2103 findOrMakeFolder(sBgFolders, container);
2104 folderInfo.add(info);
2105 break;
2106 }
2107 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002108 } else {
2109 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 }
2111 break;
2112
2113 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2114 id = c.getLong(idIndex);
2115 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2116
Sunny Goyala508e4f2015-05-21 09:33:57 -07002117 // Do not trim the folder label, as is was set by the user.
2118 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002122 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123 folderInfo.cellX = c.getInt(cellXIndex);
2124 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002125 folderInfo.spanX = 1;
2126 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002127 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128
Daniel Sandler8802e962010-05-26 16:28:16 -04002129 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002130 if (!checkItemPlacement(occupied, folderInfo)) {
2131 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002132 break;
2133 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002134
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2137 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2138 sBgWorkspaceItems.add(folderInfo);
2139 break;
Joe Onorato36115782010-06-17 13:28:48 -04002140 }
Joe Onorato17a89222011-02-08 17:26:11 -08002141
Chris Wrenf4d08112014-01-16 18:13:56 -05002142 if (restored) {
2143 // no special handling required for restored folders
2144 restoredRows.add(id);
2145 }
2146
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2148 sBgFolders.put(folderInfo.id, folderInfo);
2149 break;
2150
2151 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002152 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002154 boolean customWidget = itemType ==
2155 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2156
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002158 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002159 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002160 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002161 user = allUsers.get(serialNumber);
2162 if (user == null) {
2163 itemsToRemove.add(id);
2164 continue;
2165 }
2166
Sunny Goyalff572272014-07-23 13:58:07 -07002167 final ComponentName component =
2168 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002169
Sunny Goyal651077b2014-06-30 14:15:31 -07002170 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002171 final boolean isIdValid = (restoreStatus &
2172 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002173 final boolean wasProviderReady = (restoreStatus &
2174 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002175
Adam Cohen59400422014-03-05 18:07:04 -08002176 final LauncherAppWidgetProviderInfo provider =
2177 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002178 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002179 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002180
2181 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002182 if (!isSafeMode && !customWidget &&
2183 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002184 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002185 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002188 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 itemsToRemove.add(id);
2190 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002191 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002192 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2193 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002194
Sunny Goyalff572272014-07-23 13:58:07 -07002195 int status = restoreStatus;
2196 if (!wasProviderReady) {
2197 // If provider was not previously ready, update the
2198 // status and UI flag.
2199
2200 // Id would be valid only if the widget restore broadcast was received.
2201 if (isIdValid) {
2202 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2203 } else {
2204 status &= ~LauncherAppWidgetInfo
2205 .FLAG_PROVIDER_NOT_READY;
2206 }
2207 }
2208 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002209 } else {
2210 Log.v(TAG, "Widget restore pending id=" + id
2211 + " appWidgetId=" + appWidgetId
2212 + " status =" + restoreStatus);
2213 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002214 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002215 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002216 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002217
2218 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2219 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002220 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002221 // App restore has started. Update the flag
2222 appWidgetInfo.restoreStatus |=
2223 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002224 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002225 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002226 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002227 itemsToRemove.add(id);
2228 continue;
2229 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002230
2231 appWidgetInfo.installProgress =
2232 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002233 }
Sunny Goyalff572272014-07-23 13:58:07 -07002234
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 appWidgetInfo.cellX = c.getInt(cellXIndex);
2238 appWidgetInfo.cellY = c.getInt(cellYIndex);
2239 appWidgetInfo.spanX = c.getInt(spanXIndex);
2240 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 container = c.getInt(containerIndex);
2243 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2244 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2245 Log.e(TAG, "Widget found where container != " +
2246 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2247 continue;
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249
Adam Cohene25af792013-06-06 23:08:25 -07002250 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002252 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2253 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 break;
2255 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002256
Adam Cohen59400422014-03-05 18:07:04 -08002257 if (!customWidget) {
2258 String providerName =
2259 appWidgetInfo.providerName.flattenToString();
2260 if (!providerName.equals(savedProvider) ||
2261 (appWidgetInfo.restoreStatus != restoreStatus)) {
2262 ContentValues values = new ContentValues();
2263 values.put(
2264 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2265 providerName);
2266 values.put(LauncherSettings.Favorites.RESTORED,
2267 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002268 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002269 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002270 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2272 sBgAppWidgets.add(appWidgetInfo);
2273 }
Joe Onorato36115782010-06-17 13:28:48 -04002274 break;
2275 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002277 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002278 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 }
2280 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002281 if (c != null) {
2282 c.close();
2283 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 }
2285
Winson Chungba9c37f2013-08-30 14:11:37 -07002286 // Break early if we've stopped loading
2287 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002288 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002289 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002290 }
2291
Sunny Goyal1936ec02015-05-12 11:03:04 -07002292 // Remove any empty folder
2293 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2294 for (ItemInfo item: sBgItemsIdMap) {
2295 long container = item.container;
2296 if (emptyFolders.containsKey(container)) {
2297 emptyFolders.remove(container);
2298 }
2299 }
2300 for (FolderInfo folder : emptyFolders) {
2301 long folderId = folder.id;
2302 sBgFolders.remove(folderId);
2303 sBgItemsIdMap.remove(folderId);
2304 sBgWorkspaceItems.remove(folder);
2305 itemsToRemove.add(folderId);
2306 }
2307
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 if (itemsToRemove.size() > 0) {
2309 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002310 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 // Remove dead items
2312 for (long id : itemsToRemove) {
2313 if (DEBUG_LOADERS) {
2314 Log.d(TAG, "Removed id = " + id);
2315 }
2316 // Don't notify content observers
2317 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002318 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 } catch (RemoteException e) {
2320 Log.w(TAG, "Could not remove id = " + id);
2321 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002322 }
2323 }
2324
Chris Wrenf4d08112014-01-16 18:13:56 -05002325 if (restoredRows.size() > 0) {
2326 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002327 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002328 // Update restored items that no longer require special handling
2329 try {
2330 StringBuilder selectionBuilder = new StringBuilder();
2331 selectionBuilder.append(LauncherSettings.Favorites._ID);
2332 selectionBuilder.append(" IN (");
2333 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2334 selectionBuilder.append(")");
2335 ContentValues values = new ContentValues();
2336 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002337 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002338 values, selectionBuilder.toString(), null);
2339 } catch (RemoteException e) {
2340 Log.w(TAG, "Could not update restored rows");
2341 }
2342 }
2343
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002344 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2345 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002346 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002347 null, sWorker);
2348 }
2349
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002350 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2351 // Log to disk
2352 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2353 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002354
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002355 // Remove any empty screens
2356 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002357 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002358 long screenId = item.screenId;
2359 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2360 unusedScreens.contains(screenId)) {
2361 unusedScreens.remove(screenId);
2362 }
2363 }
2364
2365 // If there are any empty screens remove them, and update.
2366 if (unusedScreens.size() != 0) {
2367 // Log to disk
2368 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2369 TextUtils.join(", ", unusedScreens), true);
2370
2371 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002372 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002373 }
2374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 if (DEBUG_LOADERS) {
2376 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2377 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002378 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002379 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002380 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002381
Sunny Goyale2df0622015-04-24 11:27:00 -07002382 for (int i = 0; i < nScreens; i++) {
2383 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002384 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 line += " | ";
2386 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002387 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002388 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002389 if (x < screen.length && y < screen[x].length) {
2390 line += (screen[x][y] != null) ? "#" : ".";
2391 } else {
2392 line += "!";
2393 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 }
Joe Onorato36115782010-06-17 13:28:48 -04002395 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002397 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 }
Joe Onorato36115782010-06-17 13:28:48 -04002399 }
Adam Cohene25af792013-06-06 23:08:25 -07002400 }
2401
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002402 /**
2403 * Partially updates the item without any notification. Must be called on the worker thread.
2404 */
2405 private void updateItem(long itemId, ContentValues update) {
2406 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002407 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002408 update,
2409 BaseColumns._ID + "= ?",
2410 new String[]{Long.toString(itemId)});
2411 }
2412
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 /** Filters the set of items who are directly or indirectly (via another container) on the
2414 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002415 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 ArrayList<ItemInfo> allWorkspaceItems,
2417 ArrayList<ItemInfo> currentScreenItems,
2418 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002419 // Purge any null ItemInfos
2420 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2421 while (iter.hasNext()) {
2422 ItemInfo i = iter.next();
2423 if (i == null) {
2424 iter.remove();
2425 }
2426 }
2427
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 // Order the set of items by their containers first, this allows use to walk through the
2429 // list sequentially, build up a list of containers that are in the specified screen,
2430 // as well as all items in those containers.
2431 Set<Long> itemsOnScreen = new HashSet<Long>();
2432 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2433 @Override
2434 public int compare(ItemInfo lhs, ItemInfo rhs) {
2435 return (int) (lhs.container - rhs.container);
2436 }
2437 });
2438 for (ItemInfo info : allWorkspaceItems) {
2439 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002440 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 currentScreenItems.add(info);
2442 itemsOnScreen.add(info.id);
2443 } else {
2444 otherScreenItems.add(info);
2445 }
2446 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2447 currentScreenItems.add(info);
2448 itemsOnScreen.add(info.id);
2449 } else {
2450 if (itemsOnScreen.contains(info.container)) {
2451 currentScreenItems.add(info);
2452 itemsOnScreen.add(info.id);
2453 } else {
2454 otherScreenItems.add(info);
2455 }
2456 }
2457 }
2458 }
2459
2460 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002461 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 ArrayList<LauncherAppWidgetInfo> appWidgets,
2463 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2464 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465
2466 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002467 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002469 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 currentScreenWidgets.add(widget);
2471 } else {
2472 otherScreenWidgets.add(widget);
2473 }
2474 }
2475 }
2476
2477 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002478 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002479 LongArrayMap<ItemInfo> itemsIdMap,
2480 LongArrayMap<FolderInfo> folders,
2481 LongArrayMap<FolderInfo> currentScreenFolders,
2482 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483
Sunny Goyale2df0622015-04-24 11:27:00 -07002484 int total = folders.size();
2485 for (int i = 0; i < total; i++) {
2486 long id = folders.keyAt(i);
2487 FolderInfo folder = folders.valueAt(i);
2488
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002490 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002492 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 currentScreenFolders.put(id, folder);
2494 } else {
2495 otherScreenFolders.put(id, folder);
2496 }
2497 }
2498 }
2499
2500 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2501 * right) */
2502 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002503 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002504 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 // XXX: review this
2506 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002507 @Override
2508 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002509 int cellCountX = (int) profile.numColumns;
2510 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002511 int screenOffset = cellCountX * cellCountY;
2512 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002513 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002514 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002515 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002516 rhs.cellY * cellCountX + rhs.cellX);
2517 return (int) (lr - rr);
2518 }
2519 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002521
Adam Cohendcd297f2013-06-18 13:13:40 -07002522 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2523 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002524 final Runnable r = new Runnable() {
2525 @Override
2526 public void run() {
2527 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2528 if (callbacks != null) {
2529 callbacks.bindScreens(orderedScreens);
2530 }
2531 }
2532 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002533 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002534 }
2535
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2537 final ArrayList<ItemInfo> workspaceItems,
2538 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002539 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002541
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 final boolean postOnMainThread = (deferredBindRunnables != null);
2543
2544 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002545 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002547 final int start = i;
2548 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 final Runnable r = new Runnable() {
2550 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002551 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002552 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002553 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002554 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2555 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 }
2557 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 };
2559 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002560 synchronized (deferredBindRunnables) {
2561 deferredBindRunnables.add(r);
2562 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002564 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565 }
Joe Onorato36115782010-06-17 13:28:48 -04002566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567
2568 // Bind the folders
2569 if (!folders.isEmpty()) {
2570 final Runnable r = new Runnable() {
2571 public void run() {
2572 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2573 if (callbacks != null) {
2574 callbacks.bindFolders(folders);
2575 }
2576 }
2577 };
2578 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002579 synchronized (deferredBindRunnables) {
2580 deferredBindRunnables.add(r);
2581 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002582 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002583 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002584 }
2585 }
2586
2587 // Bind the widgets, one at a time
2588 N = appWidgets.size();
2589 for (int i = 0; i < N; i++) {
2590 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2591 final Runnable r = new Runnable() {
2592 public void run() {
2593 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2594 if (callbacks != null) {
2595 callbacks.bindAppWidget(widget);
2596 }
2597 }
2598 };
2599 if (postOnMainThread) {
2600 deferredBindRunnables.add(r);
2601 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002602 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603 }
2604 }
2605 }
2606
2607 /**
2608 * Binds all loaded data to actual views on the main thread.
2609 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002610 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 final long t = SystemClock.uptimeMillis();
2612 Runnable r;
2613
2614 // Don't use these two variables in any of the callback runnables.
2615 // Otherwise we hold a reference to them.
2616 final Callbacks oldCallbacks = mCallbacks.get();
2617 if (oldCallbacks == null) {
2618 // This launcher has exited and nobody bothered to tell us. Just bail.
2619 Log.w(TAG, "LoaderTask running with no launcher");
2620 return;
2621 }
2622
Winson Chung9b9fb962013-11-15 15:39:34 -08002623 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002624 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2625 ArrayList<LauncherAppWidgetInfo> appWidgets =
2626 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002627 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002628
2629 final LongArrayMap<FolderInfo> folders;
2630 final LongArrayMap<ItemInfo> itemsIdMap;
2631
Winson Chung2abf94d2012-07-18 18:16:38 -07002632 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 workspaceItems.addAll(sBgWorkspaceItems);
2634 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002635 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002636
2637 folders = sBgFolders.clone();
2638 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002639 }
2640
Derek Prothro7aff3992013-12-10 14:00:37 -05002641 final boolean isLoadingSynchronously =
2642 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002643 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002644 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002645 if (currScreen >= orderedScreenIds.size()) {
2646 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002647 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002648 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002649 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002650 final long currentScreenId = currentScreen < 0
2651 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002652
2653 // Load all the items that are on the current page first (and in the process, unbind
2654 // all the existing workspace items before we call startBinding() below.
2655 unbindWorkspaceItemsOnMainThread();
2656
2657 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002658 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2659 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2660 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2661 new ArrayList<LauncherAppWidgetInfo>();
2662 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2663 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002664 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2665 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666
Winson Chung9b9fb962013-11-15 15:39:34 -08002667 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002669 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002671 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002672 otherFolders);
2673 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2674 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2675
2676 // Tell the workspace that we're about to start binding items
2677 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002678 public void run() {
2679 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2680 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002681 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002682 }
2683 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002684 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002685 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002686
Adam Cohendcd297f2013-06-18 13:13:40 -07002687 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2688
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002689 // Load items on the current page
2690 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2691 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002692 if (isLoadingSynchronously) {
2693 r = new Runnable() {
2694 public void run() {
2695 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002696 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002697 callbacks.onPageBoundSynchronously(currentScreen);
2698 }
2699 }
2700 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002701 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002702 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002703
Winson Chung4a2afa32012-07-19 14:53:05 -07002704 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2705 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002706 synchronized (mDeferredBindRunnables) {
2707 mDeferredBindRunnables.clear();
2708 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002709 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002710 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002711
2712 // Tell the workspace that we're done binding items
2713 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002714 public void run() {
2715 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2716 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002717 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002718 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002719
Winson Chung98e030b2012-05-07 16:01:11 -07002720 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002721 if (DEBUG_LOADERS) {
2722 Log.d(TAG, "bound workspace in "
2723 + (SystemClock.uptimeMillis()-t) + "ms");
2724 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002725
2726 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002728 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002729 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002730 synchronized (mDeferredBindRunnables) {
2731 mDeferredBindRunnables.add(r);
2732 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002733 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002734 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002735 }
Joe Onorato36115782010-06-17 13:28:48 -04002736 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002737
Joe Onorato36115782010-06-17 13:28:48 -04002738 private void loadAndBindAllApps() {
2739 if (DEBUG_LOADERS) {
2740 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2741 }
2742 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002743 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002744 synchronized (LoaderTask.this) {
2745 if (mStopped) {
2746 return;
2747 }
2748 }
Sunny Goyal75b0f552015-05-20 21:57:06 -07002749 mIconCache.updateDbIcons();
Reena Lee93f824a2011-09-23 17:20:28 -07002750 synchronized (LoaderTask.this) {
2751 if (mStopped) {
2752 return;
2753 }
2754 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002755 }
Joe Onorato36115782010-06-17 13:28:48 -04002756 } else {
2757 onlyBindAllApps();
2758 }
2759 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002760
Joe Onorato36115782010-06-17 13:28:48 -04002761 private void onlyBindAllApps() {
2762 final Callbacks oldCallbacks = mCallbacks.get();
2763 if (oldCallbacks == null) {
2764 // This launcher has exited and nobody bothered to tell us. Just bail.
2765 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2766 return;
2767 }
2768
2769 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002770 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002771 final ArrayList<AppInfo> list
2772 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002773 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002774 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002775 public void run() {
2776 final long t = SystemClock.uptimeMillis();
2777 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2778 if (callbacks != null) {
2779 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002780 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002781 }
2782 if (DEBUG_LOADERS) {
2783 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2784 + (SystemClock.uptimeMillis()-t) + "ms");
2785 }
2786 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002787 };
2788 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002789 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002790 r.run();
2791 } else {
2792 mHandler.post(r);
2793 }
Joe Onorato36115782010-06-17 13:28:48 -04002794 }
2795
Winson Chung64359a52013-07-08 17:17:08 -07002796 private void loadAllApps() {
2797 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002798
Joe Onorato36115782010-06-17 13:28:48 -04002799 final Callbacks oldCallbacks = mCallbacks.get();
2800 if (oldCallbacks == null) {
2801 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002802 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002803 return;
2804 }
2805
Kenny Guyed131872014-04-30 03:02:21 +01002806 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2807
Winson Chung64359a52013-07-08 17:17:08 -07002808 // Clear the list of apps
2809 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002810 for (UserHandleCompat user : profiles) {
2811 // Query for the set of apps
2812 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002813 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002814 if (DEBUG_LOADERS) {
2815 Log.d(TAG, "getActivityList took "
2816 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2817 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2818 }
2819 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002820 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002821 if (apps == null || apps.isEmpty()) {
2822 return;
2823 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002824
Kenny Guyed131872014-04-30 03:02:21 +01002825 // Create the ApplicationInfos
2826 for (int i = 0; i < apps.size(); i++) {
2827 LauncherActivityInfoCompat app = apps.get(i);
2828 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002829 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002830 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002831
Sunny Goyal756a28a2015-04-23 17:07:55 -07002832 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2833 if (heuristic != null) {
2834 runAfterBindCompletes(new Runnable() {
2835
2836 @Override
2837 public void run() {
2838 heuristic.processUserApps(apps);
2839 }
2840 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002841 }
Winson Chung64359a52013-07-08 17:17:08 -07002842 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002843 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002844 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2845 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002846
2847 // Post callback on main thread
2848 mHandler.post(new Runnable() {
2849 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002850
Winson Chung64359a52013-07-08 17:17:08 -07002851 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002852 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002853 if (callbacks != null) {
2854 callbacks.bindAllApplications(added);
2855 if (DEBUG_LOADERS) {
2856 Log.d(TAG, "bound " + added.size() + " apps in "
2857 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2858 }
2859 } else {
2860 Log.i(TAG, "not binding apps: no Launcher activity");
2861 }
2862 }
2863 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002864 // Cleanup any data stored for a deleted user.
2865 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002866
Hyunyoung Song219d0482015-05-07 12:51:42 -07002867 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2868 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002869 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002870 Log.d(TAG, "Icons processed in "
2871 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002872 }
2873 }
2874
2875 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002876 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002877 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002878 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2879 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2880 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2881 }
Joe Onorato36115782010-06-17 13:28:48 -04002882 }
2883 }
2884
Sunny Goyal75b0f552015-05-20 21:57:06 -07002885 /**
2886 * Called when the icons for packages have been updated in the icon cache.
2887 */
2888 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2889 final Callbacks callbacks = getCallback();
2890 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2891 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2892
2893 // If any package icon has changed (app was updated while launcher was dead),
2894 // update the corresponding shortcuts.
2895 synchronized (sBgLock) {
2896 for (ItemInfo info : sBgItemsIdMap) {
2897 if (info instanceof ShortcutInfo && user.equals(info.user)
2898 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2899 ShortcutInfo si = (ShortcutInfo) info;
2900 ComponentName cn = si.getTargetComponent();
2901 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2902 si.updateIcon(mIconCache);
2903 updatedShortcuts.add(si);
2904 }
2905 }
2906 }
2907 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2908 }
2909
2910 if (!updatedShortcuts.isEmpty()) {
2911 final UserHandleCompat userFinal = user;
2912 mHandler.post(new Runnable() {
2913
2914 public void run() {
2915 Callbacks cb = getCallback();
2916 if (cb != null && callbacks == cb) {
2917 cb.bindShortcutsChanged(updatedShortcuts,
2918 new ArrayList<ShortcutInfo>(), userFinal);
2919 }
2920 }
2921 });
2922 }
2923
2924 if (!updatedApps.isEmpty()) {
2925 mHandler.post(new Runnable() {
2926
2927 public void run() {
2928 Callbacks cb = getCallback();
2929 if (cb != null && callbacks == cb) {
2930 cb.bindAppsUpdated(updatedApps);
2931 }
2932 }
2933 });
2934 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002935
2936 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2937 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002938 }
2939
Joe Onorato36115782010-06-17 13:28:48 -04002940 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002941 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002942 }
2943
Adam Cohen091440a2015-03-18 14:16:05 -07002944 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002945
2946 @Override
2947 public void onReceive(Context context, Intent intent) {
2948 synchronized (sBgLock) {
2949 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2950 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002951 final PackageManager manager = context.getPackageManager();
2952 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2953 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002954 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2955 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002956 packagesRemoved.clear();
2957 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002958 for (String pkg : entry.getValue()) {
2959 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002960 boolean packageOnSdcard = launcherApps.isAppEnabled(
2961 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2962 if (packageOnSdcard) {
2963 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2964 packagesUnavailable.add(pkg);
2965 } else {
2966 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2967 packagesRemoved.add(pkg);
2968 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002969 }
2970 }
2971 if (!packagesRemoved.isEmpty()) {
2972 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2973 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2974 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002975 if (!packagesUnavailable.isEmpty()) {
2976 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2977 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2978 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002979 }
Sunny Goyal34942622014-08-29 17:20:55 -07002980 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002981 }
2982 }
2983 }
2984
Joe Onorato36115782010-06-17 13:28:48 -04002985 private class PackageUpdatedTask implements Runnable {
2986 int mOp;
2987 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002988 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002989
2990 public static final int OP_NONE = 0;
2991 public static final int OP_ADD = 1;
2992 public static final int OP_UPDATE = 2;
2993 public static final int OP_REMOVE = 3; // uninstlled
2994 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2995
2996
Kenny Guyed131872014-04-30 03:02:21 +01002997 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002998 mOp = op;
2999 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003000 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003001 }
3002
3003 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003004 if (!mHasLoaderCompletedOnce) {
3005 // Loader has not yet run.
3006 return;
3007 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003008 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003009
3010 final String[] packages = mPackages;
3011 final int N = packages.length;
3012 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003013 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003014 for (int i=0; i<N; i++) {
3015 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003016 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003017 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003018 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003019
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003020 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3021 if (heuristic != null) {
3022 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003023 }
Joe Onorato36115782010-06-17 13:28:48 -04003024 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003025 }
Joe Onorato36115782010-06-17 13:28:48 -04003026 case OP_UPDATE:
3027 for (int i=0; i<N; i++) {
3028 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003029 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003030 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003031 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003032 }
3033 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003034 case OP_REMOVE: {
3035 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3036 if (heuristic != null) {
3037 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003038 }
Joe Onorato36115782010-06-17 13:28:48 -04003039 for (int i=0; i<N; i++) {
3040 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003041 mIconCache.removeIconsForPkg(packages[i], mUser);
3042 }
3043 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003044 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003045 case OP_UNAVAILABLE:
3046 for (int i=0; i<N; i++) {
3047 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3048 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003049 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003050 }
3051 break;
3052 }
3053
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003054 ArrayList<AppInfo> added = null;
3055 ArrayList<AppInfo> modified = null;
3056 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003057
Adam Cohen487f7dd2012-06-28 18:12:10 -07003058 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003059 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003060 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003062 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003063 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003064 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003065 }
Winson Chung5d55f332012-07-16 20:45:03 -07003066 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003067 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003068 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003069 }
3070
Sunny Goyale0f58d72014-11-10 18:05:31 -08003071 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003072 if (callbacks == null) {
3073 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3074 return;
3075 }
3076
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3078 new HashMap<ComponentName, AppInfo>();
3079
Joe Onorato36115782010-06-17 13:28:48 -04003080 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003081 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082 for (AppInfo ai : added) {
3083 addedOrUpdatedApps.put(ai.componentName, ai);
3084 }
Joe Onorato36115782010-06-17 13:28:48 -04003085 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003086
Joe Onorato36115782010-06-17 13:28:48 -04003087 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003088 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003089 for (AppInfo ai : modified) {
3090 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003091 }
3092
Joe Onorato36115782010-06-17 13:28:48 -04003093 mHandler.post(new Runnable() {
3094 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003095 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003096 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003097 callbacks.bindAppsUpdated(modifiedFinal);
3098 }
3099 }
3100 });
3101 }
Winson Chung83892cc2013-05-01 16:53:33 -07003102
Sunny Goyal4390ace2014-10-13 11:33:11 -07003103 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003104 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3106 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3107 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3108
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003110 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003111 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3113 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003114 boolean infoUpdated = false;
3115 boolean shortcutUpdated = false;
3116
3117 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003118 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003120 Bitmap icon = Utilities.createIconBitmap(
3121 si.iconResource.packageName,
3122 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if (icon != null) {
3124 si.setIcon(icon);
3125 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003126 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003127 }
3128 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003129
3130 ComponentName cn = si.getTargetComponent();
3131 if (cn != null && packageSet.contains(cn.getPackageName())) {
3132 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3133
3134 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3136 // Auto install icon
3137 PackageManager pm = context.getPackageManager();
3138 ResolveInfo matched = pm.resolveActivity(
3139 new Intent(Intent.ACTION_MAIN)
3140 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3141 PackageManager.MATCH_DEFAULT_ONLY);
3142 if (matched == null) {
3143 // Try to find the best match activity.
3144 Intent intent = pm.getLaunchIntentForPackage(
3145 cn.getPackageName());
3146 if (intent != null) {
3147 cn = intent.getComponent();
3148 appInfo = addedOrUpdatedApps.get(cn);
3149 }
3150
3151 if ((intent == null) || (appInfo == null)) {
3152 removedShortcuts.add(si);
3153 continue;
3154 }
3155 si.promisedIntent = intent;
3156 }
3157 }
3158
3159 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003160 if (appInfo != null) {
3161 si.flags = appInfo.flags;
3162 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163
Sunny Goyal756adbc2015-04-16 15:20:51 -07003164 si.intent = si.promisedIntent;
3165 si.promisedIntent = null;
3166 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167 infoUpdated = true;
3168 si.updateIcon(mIconCache);
3169 }
3170
3171 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3172 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3173 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003174 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003175 si.contentDescription = appInfo.contentDescription;
3176 infoUpdated = true;
3177 }
3178
3179 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3180 // Since package was just updated, the target must be available now.
3181 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3182 shortcutUpdated = true;
3183 }
3184 }
3185
3186 if (infoUpdated || shortcutUpdated) {
3187 updatedShortcuts.add(si);
3188 }
3189 if (infoUpdated) {
3190 updateItemInDatabase(context, si);
3191 }
3192 } else if (info instanceof LauncherAppWidgetInfo) {
3193 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3194 if (mUser.equals(widgetInfo.user)
3195 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3196 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3197 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3198 widgets.add(widgetInfo);
3199 updateItemInDatabase(context, widgetInfo);
3200 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003201 }
3202 }
3203 }
3204
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3206 mHandler.post(new Runnable() {
3207
3208 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003209 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003210 if (callbacks == cb && cb != null) {
3211 callbacks.bindShortcutsChanged(
3212 updatedShortcuts, removedShortcuts, mUser);
3213 }
3214 }
3215 });
3216 if (!removedShortcuts.isEmpty()) {
3217 deleteItemsFromDatabase(context, removedShortcuts);
3218 }
3219 }
3220 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003221 mHandler.post(new Runnable() {
3222 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003224 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003225 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003226 }
3227 }
3228 });
3229 }
3230 }
3231
Winson Chungdf95eb12013-10-16 14:57:07 -07003232 final ArrayList<String> removedPackageNames =
3233 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003234 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 // Mark all packages in the broadcast to be removed
3236 removedPackageNames.addAll(Arrays.asList(packages));
3237 } else if (mOp == OP_UPDATE) {
3238 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003240 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003241 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003242 }
3243 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003244 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003245
Winson Chungdf95eb12013-10-16 14:57:07 -07003246 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003247 final int removeReason;
3248 if (mOp == OP_UNAVAILABLE) {
3249 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3250 } else {
3251 // Remove all the components associated with this package
3252 for (String pn : removedPackageNames) {
3253 deletePackageFromDatabase(context, pn, mUser);
3254 }
3255 // Remove all the specific components
3256 for (AppInfo a : removedApps) {
3257 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3258 deleteItemsFromDatabase(context, infos);
3259 }
3260 removeReason = 0;
3261 }
3262
Winson Chungdf95eb12013-10-16 14:57:07 -07003263 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003264 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003266 mHandler.post(new Runnable() {
3267 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003268 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003269 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003270 callbacks.bindComponentsRemoved(
3271 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003272 }
3273 }
3274 });
Joe Onoratobe386092009-11-17 17:32:16 -08003275 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003276
3277 // onProvidersChanged method (API >= 17) already refreshed the widget list
3278 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3279
Adam Cohen4caf2982013-08-20 18:54:31 -07003280 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003281 mHandler.post(new Runnable() {
3282 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003283 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003284 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003285 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003286 }
3287 }
3288 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289 }
3290 }
3291
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003292 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3293 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003294 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003295 if (sBgWidgetProviders == null || refresh) {
3296 sBgWidgetProviders = new HashMap<>();
3297 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3298 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003299
Robin Lee26ace122015-03-16 19:41:43 +00003300 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3301 for (AppWidgetProviderInfo pInfo : widgets) {
3302 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3303 UserHandleCompat user = wm.getUser(info);
3304 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3305 }
3306
3307 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3308 for (CustomAppWidget widget : customWidgets) {
3309 info = new LauncherAppWidgetProviderInfo(context, widget);
3310 UserHandleCompat user = wm.getUser(info);
3311 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3312 }
Adam Cohen59400422014-03-05 18:07:04 -08003313 }
Adam Cohen59400422014-03-05 18:07:04 -08003314 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3315 }
3316 }
3317
Robin Lee26ace122015-03-16 19:41:43 +00003318 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3319 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003320 synchronized (sBgLock) {
3321 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003322 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003323 }
Robin Lee26ace122015-03-16 19:41:43 +00003324 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003325 }
3326 }
3327
Hyunyoung Song227239e2015-05-04 18:17:35 -07003328 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3329 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003330
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003331 runOnWorkerThread(new Runnable(){
3332 @Override
3333 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003334 final WidgetsModel model = createWidgetsModel(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003335 mHandler.post(new Runnable() {
3336 @Override
3337 public void run() {
3338 Callbacks cb = getCallback();
3339 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003340 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 }
3342 }
3343 });
Hyunyoung Song9110d482015-05-22 14:49:23 -07003344 mBgWidgetsModel = model;
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003345 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003346 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3347 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003348 }
3349 });
3350 }
3351
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003352 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003353 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003354 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003355 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003356 */
Sunny Goyal316490e2015-06-02 09:38:28 -07003357 @Thunk WidgetsModel createWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003358 PackageManager packageManager = context.getPackageManager();
3359 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003360 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003361 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3362 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003363 WidgetsModel model = new WidgetsModel(context);
3364 model.addWidgetsAndShortcuts(widgetsAndShortcuts);
3365 return model;
Michael Jurkac402cd92013-05-20 15:49:32 +02003366 }
3367
Adam Cohen091440a2015-03-18 14:16:05 -07003368 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003369 UserHandleCompat user) {
3370 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3371 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003372 }
Adam Cohen556f6132014-01-15 15:18:08 -08003373
Kenny Guyed131872014-04-30 03:02:21 +01003374 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3375 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003376 if (cn == null) {
3377 return false;
3378 }
Kenny Guyed131872014-04-30 03:02:21 +01003379 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3380 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003381 return false;
3382 }
Kenny Guyed131872014-04-30 03:02:21 +01003383 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003384 }
3385
Adam Cohena28b78e2014-05-20 17:03:04 -07003386 public static boolean isValidPackage(Context context, String packageName,
3387 UserHandleCompat user) {
3388 if (packageName == null) {
3389 return false;
3390 }
3391 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3392 return launcherApps.isPackageEnabledForProfile(packageName, user);
3393 }
3394
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003396 * Make an ShortcutInfo object for a restored application or shortcut item that points
3397 * to a package that is not yet installed on the system.
3398 */
Sunny Goyal34942622014-08-29 17:20:55 -07003399 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07003400 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003401 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003402 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal5c97f512015-05-19 16:03:28 -07003403 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
Sunny Goyal34942622014-08-29 17:20:55 -07003404
3405 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3406 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3407 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003408 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003409 }
Sunny Goyal34942622014-08-29 17:20:55 -07003410 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3411 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003412 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003413 }
Sunny Goyal34942622014-08-29 17:20:55 -07003414 } else {
3415 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3416 }
3417
Winson Chung82b016c2015-05-08 17:00:10 -07003418 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003419 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003420 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003421 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003422 return info;
3423 }
3424
3425 /**
3426 * Make an Intent object for a restored application or shortcut item that points
3427 * to the market page for the item.
3428 */
Adam Cohen091440a2015-03-18 14:16:05 -07003429 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003430 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003431 return getMarketIntent(componentName.getPackageName());
3432 }
3433
3434 static Intent getMarketIntent(String packageName) {
3435 return new Intent(Intent.ACTION_VIEW)
3436 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003437 .scheme("market")
3438 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003439 .appendQueryParameter("id", packageName)
3440 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003441 }
3442
3443 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003444 * Make an ShortcutInfo object for a shortcut that is an application.
3445 *
3446 * If c is not null, then it will be used to fill in missing data like the title and icon.
3447 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003448 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003449 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003450 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003451 if (user == null) {
3452 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003453 return null;
3454 }
3455
Kenny Guyed131872014-04-30 03:02:21 +01003456 ComponentName componentName = intent.getComponent();
3457 if (componentName == null) {
3458 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3459 return null;
3460 }
3461
3462 Intent newIntent = new Intent(intent.getAction(), null);
3463 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3464 newIntent.setComponent(componentName);
3465 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003466 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003467 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3468 return null;
3469 }
3470
3471 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003472 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003473 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3474 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3475 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003476 }
3477
Joe Onorato56d82912010-03-07 14:32:10 -05003478 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003479 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003480 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003481 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003482
Joe Onorato56d82912010-03-07 14:32:10 -05003483 // fall back to the class name of the activity
3484 if (info.title == null) {
3485 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003486 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003489 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003490 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003491 if (lai != null) {
3492 info.flags = AppInfo.initFlags(lai);
3493 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003495 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003496
Sunny Goyale2df0622015-04-24 11:27:00 -07003497 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003498 ItemInfoFilter f) {
3499 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3500 for (ItemInfo i : infos) {
3501 if (i instanceof ShortcutInfo) {
3502 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003503 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003504 if (cn != null && f.filterItem(null, info, cn)) {
3505 filtered.add(info);
3506 }
3507 } else if (i instanceof FolderInfo) {
3508 FolderInfo info = (FolderInfo) i;
3509 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003510 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003511 if (cn != null && f.filterItem(info, s, cn)) {
3512 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003513 }
3514 }
Winson Chung64359a52013-07-08 17:17:08 -07003515 } else if (i instanceof LauncherAppWidgetInfo) {
3516 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3517 ComponentName cn = info.providerName;
3518 if (cn != null && f.filterItem(null, info, cn)) {
3519 filtered.add(info);
3520 }
Winson Chung8a435102012-08-30 17:16:53 -07003521 }
3522 }
Winson Chung64359a52013-07-08 17:17:08 -07003523 return new ArrayList<ItemInfo>(filtered);
3524 }
3525
Adam Cohen091440a2015-03-18 14:16:05 -07003526 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003527 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003528 ItemInfoFilter filter = new ItemInfoFilter() {
3529 @Override
3530 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003531 if (info.user == null) {
3532 return cn.equals(cname);
3533 } else {
3534 return cn.equals(cname) && info.user.equals(user);
3535 }
Winson Chung64359a52013-07-08 17:17:08 -07003536 }
3537 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003538 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003539 }
3540
Sunny Goyal1a745e82014-10-02 15:58:31 -07003541 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003542 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 */
Adam Cohen091440a2015-03-18 14:16:05 -07003544 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003545 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3546 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547
Joe Onorato56d82912010-03-07 14:32:10 -05003548 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003549 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003550 // Non-app shortcuts are only supported for current user.
3551 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003553
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003554 // TODO: If there's an explicit component and we can't install that, delete it.
3555
Winson Chung82b016c2015-05-08 17:00:10 -07003556 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003557
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 int iconType = c.getInt(iconTypeIndex);
3559 switch (iconType) {
3560 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3561 String packageName = c.getString(iconPackageIndex);
3562 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003563 info.customIcon = false;
3564 // the resource
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003565 icon = Utilities.createIconBitmap(packageName, resourceName, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003566 // the db
3567 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003568 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003569 }
3570 // the fallback icon
3571 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003572 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003573 info.usingFallbackIcon = true;
3574 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 break;
3576 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003577 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003578 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003579 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003580 info.customIcon = false;
3581 info.usingFallbackIcon = true;
3582 } else {
3583 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 break;
3586 default:
Kenny Guyed131872014-04-30 03:02:21 +01003587 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003588 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 info.customIcon = false;
3590 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003591 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003592 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 return info;
3594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003595
Sunny Goyal2350bc92014-10-14 16:42:54 -07003596 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003597 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3598 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3599 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3600
Adam Cohend9198822011-11-22 16:42:47 -08003601 if (intent == null) {
3602 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3603 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3604 return null;
3605 }
3606
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003608 boolean customIcon = false;
3609 ShortcutIconResource iconResource = null;
3610
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003611 if (bitmap instanceof Bitmap) {
3612 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003613 customIcon = true;
3614 } else {
3615 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003616 if (extra instanceof ShortcutIconResource) {
3617 iconResource = (ShortcutIconResource) extra;
3618 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003619 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003620 }
3621 }
3622
Michael Jurkac9d95c52011-08-29 14:03:34 -07003623 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003624
Kenny Guyed131872014-04-30 03:02:21 +01003625 // Only support intents for current user for now. Intents sent from other
3626 // users wouldn't get here without intent forwarding anyway.
3627 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003628 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003629 icon = mIconCache.getDefaultIcon(info.user);
3630 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003631 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003632 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003633
Winson Chung82b016c2015-05-08 17:00:10 -07003634 info.title = Utilities.trim(name);
3635 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003636 info.intent = intent;
3637 info.customIcon = customIcon;
3638 info.iconResource = iconResource;
3639
3640 return info;
3641 }
3642
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003644 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 * or make a new one.
3646 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003647 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 // See if a placeholder was created for us already
3649 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003650 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003652 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 folders.put(id, folderInfo);
3654 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003655 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 }
3657
Joe Onoratobe386092009-11-17 17:32:16 -08003658
Sunny Goyal651077b2014-06-30 14:15:31 -07003659 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3660 return (provider != null) && (provider.provider != null)
3661 && (provider.provider.getPackageName() != null);
3662 }
3663
Joe Onoratobe386092009-11-17 17:32:16 -08003664 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003665 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003666 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3667 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3668 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3669 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003670 if (mLoaderTask != null) {
3671 mLoaderTask.dumpState();
3672 } else {
3673 Log.d(TAG, "mLoaderTask=null");
3674 }
Joe Onoratobe386092009-11-17 17:32:16 -08003675 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003676
3677 public Callbacks getCallback() {
3678 return mCallbacks != null ? mCallbacks.get() : null;
3679 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003680
3681 /**
3682 * @return {@link FolderInfo} if its already loaded.
3683 */
3684 public FolderInfo findFolderById(Long folderId) {
3685 synchronized (sBgLock) {
3686 return sBgFolders.get(folderId);
3687 }
3688 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003689
3690 /**
3691 * @return the looper for the worker thread which can be used to start background tasks.
3692 */
3693 public static Looper getWorkerLooper() {
3694 return sWorkerThread.getLooper();
3695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696}