blob: a5703a262c0f226b8cd30979b879e31a06e248ff [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;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080038import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070040import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070044import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040049import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080050import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070053import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010054
Sunny Goyalffe83f12014-08-14 17:39:34 -070055import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.LauncherActivityInfoCompat;
57import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070058import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070059import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010060import com.android.launcher3.compat.UserHandleCompat;
61import com.android.launcher3.compat.UserManagerCompat;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070063import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080065
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.lang.ref.WeakReference;
67import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070068import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.text.Collator;
70import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Jason Monkbbe1e242014-05-16 17:37:34 -0400111 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // When we are loading pages synchronously, we can't just post the binding of items on the side
126 // pages as this delays the rotation process. Instead, we wait for a callback from the first
127 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
128 // a normal load, we also clear this set of Runnables.
129 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
130
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700134 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800135
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700136 // The lock that must be acquired before referencing any static bg data structures. Unlike
137 // other locks, this one can generally be held long-term because we never expect any of these
138 // static data structures to be referenced outside of the worker thread except on the first
139 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700140 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700141
Adam Cohen487f7dd2012-06-28 18:12:10 -0700142 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700144 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
147 // created by LauncherModel that are directly on the home screen (however, no widgets or
148 // shortcuts within folders).
149 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
152 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 new ArrayList<LauncherAppWidgetInfo>();
154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
156 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700157
Adam Cohendcd297f2013-06-18 13:13:40 -0700158 // sBgWorkspaceScreens is the ordered set of workspace screens.
159 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
160
Adam Cohen59400422014-03-05 18:07:04 -0800161 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000162 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800163
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700164 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700165 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
166 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700167
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700168 // </ only access in worker thread >
169
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Reena Lee99a73f32011-10-24 17:27:37 -0700172 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700173
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk final LauncherAppsCompat mLauncherApps;
175 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700178 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public int getCurrentWorkspaceScreen();
180 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700181 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
182 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700183 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500185 public void bindFolders(HashMap<Long,FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800186 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700189 public void bindAppsAdded(ArrayList<Long> newScreens,
190 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700191 ArrayList<ItemInfo> addAnimated,
192 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700194 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
195 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
196 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700197 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700198 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700199 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Michael Jurkac402cd92013-05-20 15:49:32 +0200200 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100201 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700202 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700203 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700204 public void dumpLogsToLocalData();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800205 public void bindAddPendingItem(PendingAddItemInfo info, long container, long screenId,
206 int[] cell, int spanX, int spanY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chung64359a52013-07-08 17:17:08 -0700209 public interface ItemInfoFilter {
210 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
211 }
212
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800213 public interface ScreenPosProvider {
214 int getScreenIndex(ArrayList<Long> screenIDs);
215 }
216
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800218 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219
Winson Chungee055712013-07-30 14:46:24 -0700220 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700221 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400222 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
223 // resource string.
224 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
225 ProviderInfo providerInfo =
226 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
227 ProviderInfo redirectProvider =
228 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400231 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 if (mOldContentProviderExists) {
234 Log.d(TAG, "Old launcher provider exists.");
235 } else {
236 Log.d(TAG, "Old launcher provider does not exist.");
237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700244 Configuration config = res.getConfiguration();
245 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
263 private static void runOnWorkerThread(Runnable r) {
264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Winson Chunge43a1e72014-01-15 10:33:02 -0800272 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
273 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800274 }
275
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 public void setPackageState(final PackageInstallInfo installInfo) {
277 Runnable updateRunnable = new Runnable() {
278
279 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500280 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 synchronized (sBgLock) {
282 final HashSet<ItemInfo> updates = new HashSet<>();
283
284 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
285 // Ignore install success events as they are handled by Package add events.
286 return;
287 }
288
289 for (ItemInfo info : sBgItemsIdMap.values()) {
290 if (info instanceof ShortcutInfo) {
291 ShortcutInfo si = (ShortcutInfo) info;
292 ComponentName cn = si.getTargetComponent();
293 if (si.isPromise() && (cn != null)
294 && installInfo.packageName.equals(cn.getPackageName())) {
295 si.setInstallProgress(installInfo.progress);
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
298 // Mark this info as broken.
299 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
300 }
301 updates.add(si);
302 }
303 }
304 }
305
306 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
307 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
308 widget.installProgress = installInfo.progress;
309 updates.add(widget);
310 }
311 }
312
313 if (!updates.isEmpty()) {
314 // Push changes to the callback.
315 Runnable r = new Runnable() {
316 public void run() {
317 Callbacks callbacks = getCallback();
318 if (callbacks != null) {
319 callbacks.bindRestoreItemsChange(updates);
320 }
321 }
322 };
323 mHandler.post(r);
324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326 }
327 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500329 }
330
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 /**
332 * Updates the icons and label of all pending icons for the provided package name.
333 */
334 public void updateSessionDisplayInfo(final String packageName) {
335 Runnable updateRunnable = new Runnable() {
336
337 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 synchronized (sBgLock) {
340 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
341 final UserHandleCompat user = UserHandleCompat.myUserHandle();
342
343 for (ItemInfo info : sBgItemsIdMap.values()) {
344 if (info instanceof ShortcutInfo) {
345 ShortcutInfo si = (ShortcutInfo) info;
346 ComponentName cn = si.getTargetComponent();
347 if (si.isPromise() && (cn != null)
348 && packageName.equals(cn.getPackageName())) {
349 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
350 // For auto install apps update the icon as well as label.
351 mIconCache.getTitleAndIcon(si,
352 si.promisedIntent, user,
353 si.shouldUseLowResIcon());
354 } else {
355 // Only update the icon for restored apps.
356 si.updateIcon(mIconCache);
357 }
358 updates.add(si);
359 }
360 }
361 }
362
363 if (!updates.isEmpty()) {
364 // Push changes to the callback.
365 Runnable r = new Runnable() {
366 public void run() {
367 Callbacks callbacks = getCallback();
368 if (callbacks != null) {
369 callbacks.bindShortcutsChanged(updates,
370 new ArrayList<ShortcutInfo>(), user);
371 }
372 }
373 };
374 mHandler.post(r);
375 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377 }
378 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700379 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700380 }
381
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384
385 if (allAppsApps == null) {
386 throw new RuntimeException("allAppsApps must not be null");
387 }
388 if (allAppsApps.isEmpty()) {
389 return;
390 }
391
392 // Process the newly added applications and add them to the database first
393 Runnable r = new Runnable() {
394 public void run() {
395 runOnMainThread(new Runnable() {
396 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 }
401 }
402 });
403 }
404 };
405 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700406 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800407
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700408 public void addAndBindAddedWorkspaceItems(final Context context,
Adam Cohen76a47a12014-02-05 11:47:43 -0800409 final ArrayList<ItemInfo> workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700410 addAndBindAddedWorkspaceItems(context, workspaceApps,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 new ScreenPosProvider() {
Adam Cohen76a47a12014-02-05 11:47:43 -0800412
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800413 @Override
414 public int getScreenIndex(ArrayList<Long> screenIDs) {
415 return screenIDs.isEmpty() ? 0 : 1;
416 }
417 }, 1, false);
418 }
419
420 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<Rect> occupiedPos,
421 int[] xy, int spanX, int spanY) {
422 LauncherAppState app = LauncherAppState.getInstance();
423 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
424 final int xCount = (int) grid.numColumns;
425 final int yCount = (int) grid.numRows;
426 boolean[][] occupied = new boolean[xCount][yCount];
427 if (occupiedPos != null) {
428 for (Rect r : occupiedPos) {
429 for (int x = r.left; 0 <= x && x < r.right && x < xCount; x++) {
430 for (int y = r.top; 0 <= y && y < r.bottom && y < yCount; y++) {
431 occupied[x][y] = true;
432 }
433 }
434 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800435 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436 return CellLayout.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
437 }
438
439 /**
440 * Find a position on the screen for the given size or adds a new screen.
441 * @return screenId and the coordinates for the item.
442 */
Adam Cohen091440a2015-03-18 14:16:05 -0700443 @Thunk static Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 Context context,
445 ScreenPosProvider preferredScreen,
446 int fallbackStartScreen,
447 ArrayList<Long> workspaceScreens,
448 ArrayList<Long> addedWorkspaceScreensFinal,
449 int spanX, int spanY) {
450 // Load position of items which are on the desktop. We can't use sBgItemsIdMap because
451 // loadWorkspace() may not have been called.
452 final ContentResolver cr = context.getContentResolver();
453 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
454 new String[] {
455 LauncherSettings.Favorites.SCREEN,
456 LauncherSettings.Favorites.CELLX,
457 LauncherSettings.Favorites.CELLY,
458 LauncherSettings.Favorites.SPANX,
459 LauncherSettings.Favorites.SPANY,
460 LauncherSettings.Favorites.CONTAINER
461 },
462 "container=?",
463 new String[] { Integer.toString(LauncherSettings.Favorites.CONTAINER_DESKTOP) },
464 null);
465
466 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
467 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
468 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
469 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
470 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
471 LongSparseArray<ArrayList<Rect>> screenItems = new LongSparseArray<ArrayList<Rect>>();
472 try {
473 while (c.moveToNext()) {
474 Rect rect = new Rect();
475 rect.left = c.getInt(cellXIndex);
476 rect.top = c.getInt(cellYIndex);
477 rect.right = rect.left + Math.max(1, c.getInt(spanXIndex));
478 rect.bottom = rect.top + Math.max(1, c.getInt(spanYIndex));
479
480 long screenId = c.getInt(screenIndex);
481 ArrayList<Rect> items = screenItems.get(screenId);
482 if (items == null) {
483 items = new ArrayList<Rect>();
484 screenItems.put(screenId, items);
485 }
486 items.add(rect);
487 }
488 } catch (Exception e) {
489 screenItems.clear();
490 } finally {
491 c.close();
492 }
493
494 // Find appropriate space for the item.
495 long screenId = 0;
496 int[] cordinates = new int[2];
497 boolean found = false;
498
499 int screenCount = workspaceScreens.size();
500 // First check the preferred screen.
501 int preferredScreenIndex = preferredScreen.getScreenIndex(workspaceScreens);
502 if (preferredScreenIndex < screenCount) {
503 screenId = workspaceScreens.get(preferredScreenIndex);
504 found = findNextAvailableIconSpaceInScreen(
505 screenItems.get(screenId), cordinates, spanX, spanY);
506 }
507
508 if (!found) {
509 // Search on any of the screens.
510 for (int screen = fallbackStartScreen; screen < screenCount; screen++) {
511 screenId = workspaceScreens.get(screen);
512 if (findNextAvailableIconSpaceInScreen(
513 screenItems.get(screenId), cordinates, spanX, spanY)) {
514 // We found a space for it
515 found = true;
516 break;
517 }
518 }
519 }
520
521 if (!found) {
522 // Still no position found. Add a new screen to the end.
523 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
524
525 // Save the screen id for binding in the workspace
526 workspaceScreens.add(screenId);
527 addedWorkspaceScreensFinal.add(screenId);
528
529 // If we still can't find an empty space, then God help us all!!!
530 if (!findNextAvailableIconSpaceInScreen(
531 screenItems.get(screenId), cordinates, spanX, spanY)) {
532 throw new RuntimeException("Can't find space to add the item");
533 }
534 }
535 return Pair.create(screenId, cordinates);
536 }
537
538 /**
539 * Adds the provided items to the workspace.
540 * @param preferredScreen the screen where we should try to add the app first
541 * @param fallbackStartScreen the screen to start search for empty space if
542 * preferredScreen is not available.
543 */
544 public void addAndBindPendingItem(
545 final Context context,
546 final PendingAddItemInfo addInfo,
547 final ScreenPosProvider preferredScreen,
548 final int fallbackStartScreen) {
549 final Callbacks callbacks = getCallback();
550 // Process the newly added applications and add them to the database first
551 Runnable r = new Runnable() {
552 public void run() {
553 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800554 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800555
556 // Find appropriate space for the item.
557 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
558 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
559 addInfo.spanX,
560 addInfo.spanY);
561 final long screenId = coords.first;
562 final int[] cordinates = coords.second;
563
564 // Update the workspace screens
565 updateWorkspaceScreenOrder(context, workspaceScreens);
566 runOnMainThread(new Runnable() {
567 public void run() {
568 Callbacks cb = getCallback();
569 if (callbacks == cb && cb != null) {
570 cb.bindAddScreens(addedWorkspaceScreensFinal);
571 cb.bindAddPendingItem(addInfo,
572 LauncherSettings.Favorites.CONTAINER_DESKTOP,
573 screenId, cordinates, addInfo.spanX, addInfo.spanY);
574 }
575 }
576 });
577 }
578 };
579 runOnWorkerThread(r);
580 }
581
582 /**
583 * Adds the provided items to the workspace.
584 * @param preferredScreen the screen where we should try to add the app first
585 * @param fallbackStartScreen the screen to start search for empty space if
586 * preferredScreen is not available.
587 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700588 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800589 final ArrayList<ItemInfo> workspaceApps,
590 final ScreenPosProvider preferredScreen,
591 final int fallbackStartScreen,
592 final boolean allowDuplicate) {
593 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800594 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700595 return;
Winson Chung997a9232013-07-24 15:33:46 -0700596 }
Winson Chung64359a52013-07-08 17:17:08 -0700597 // Process the newly added applications and add them to the database first
598 Runnable r = new Runnable() {
599 public void run() {
600 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
601 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
602
Winson Chung76828c82013-08-19 15:43:29 -0700603 // Get the list of workspace screens. We need to append to this list and
604 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
605 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800606 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700607 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800608 for (ItemInfo item : workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700609 if (!allowDuplicate && item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800610 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700611 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800612 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700613 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800614 }
Winson Chung76828c82013-08-19 15:43:29 -0700615
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800616 // Find appropriate space for the item.
617 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
618 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
619 1, 1);
620 long screenId = coords.first;
621 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700622
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700623 ItemInfo itemInfo;
624 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
625 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800626 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700627 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700628 } else {
629 throw new RuntimeException("Unexpected info type");
630 }
Winson Chung94d67682013-09-25 16:29:40 -0700631
Winson Chung64359a52013-07-08 17:17:08 -0700632 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700633 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700634 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700635 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700636 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700637 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700638 }
639 }
640
Winson Chung76828c82013-08-19 15:43:29 -0700641 // Update the workspace screens
642 updateWorkspaceScreenOrder(context, workspaceScreens);
643
Adam Cohen76a47a12014-02-05 11:47:43 -0800644 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700645 runOnMainThread(new Runnable() {
646 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800647 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700648 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700649 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
650 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700651 if (!addedShortcutsFinal.isEmpty()) {
652 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
653 long lastScreenId = info.screenId;
654 for (ItemInfo i : addedShortcutsFinal) {
655 if (i.screenId == lastScreenId) {
656 addAnimated.add(i);
657 } else {
658 addNotAnimated.add(i);
659 }
Winson Chung997a9232013-07-24 15:33:46 -0700660 }
661 }
Winson Chungd64d1762013-08-20 14:37:16 -0700662 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800663 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700664 }
Winson Chung64359a52013-07-08 17:17:08 -0700665 }
Winson Chung997a9232013-07-24 15:33:46 -0700666 });
667 }
Winson Chung64359a52013-07-08 17:17:08 -0700668 }
669 };
670 runOnWorkerThread(r);
671 }
672
Sunny Goyald33860f2015-04-23 16:02:20 -0700673 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700674 if (sWorkerThread.getThreadId() == Process.myTid()) {
675 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
676 "main thread");
677 }
678
679 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400680 synchronized (mDeferredBindRunnables) {
681 mDeferredBindRunnables.clear();
682 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700683
684 // Remove any queued UI runnables
685 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700686 // Unbind all the workspace items
687 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700688 }
689
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700690 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700691 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700692 // Ensure that we don't use the same workspace items data structure on the main thread
693 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700694 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700695 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700696 tmpItems.addAll(sBgWorkspaceItems);
697 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700698 }
699 Runnable r = new Runnable() {
700 @Override
701 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700702 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700703 item.unbind();
704 }
705 }
706 };
707 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700708 }
709
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 /**
711 * Adds an item to the DB if it was not created previously, or move it to a new
712 * <container, screen, cellX, cellY>
713 */
714 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 if (item.container == ItemInfo.NO_ID) {
717 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700718 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 } else {
720 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 }
723 }
724
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700725 static void checkItemInfoLocked(
726 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
727 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
728 if (modelItem != null && item != modelItem) {
729 // check all the data is consistent
730 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
731 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
732 ShortcutInfo shortcut = (ShortcutInfo) item;
733 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
734 modelShortcut.intent.filterEquals(shortcut.intent) &&
735 modelShortcut.id == shortcut.id &&
736 modelShortcut.itemType == shortcut.itemType &&
737 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700739 modelShortcut.cellX == shortcut.cellX &&
740 modelShortcut.cellY == shortcut.cellY &&
741 modelShortcut.spanX == shortcut.spanX &&
742 modelShortcut.spanY == shortcut.spanY &&
743 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
744 (modelShortcut.dropPos != null &&
745 shortcut.dropPos != null &&
746 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
747 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
748 // For all intents and purposes, this is the same object
749 return;
750 }
751 }
752
753 // the modelItem needs to match up perfectly with item if our model is
754 // to be consistent with the database-- for now, just require
755 // modelItem == item or the equality check above
756 String msg = "item: " + ((item != null) ? item.toString() : "null") +
757 "modelItem: " +
758 ((modelItem != null) ? modelItem.toString() : "null") +
759 "Error: ItemInfo passed to checkItemInfo doesn't match original";
760 RuntimeException e = new RuntimeException(msg);
761 if (stackTrace != null) {
762 e.setStackTrace(stackTrace);
763 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800764 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700765 }
766 }
767
Michael Jurka816474f2012-06-25 14:49:02 -0700768 static void checkItemInfo(final ItemInfo item) {
769 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
770 final long itemId = item.id;
771 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700772 public void run() {
773 synchronized (sBgLock) {
774 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700775 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700776 }
777 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700778 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700779 }
780
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
782 final ItemInfo item, final String callingFunction) {
783 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700784 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700785 final ContentResolver cr = context.getContentResolver();
786
Adam Cohen487f7dd2012-06-28 18:12:10 -0700787 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700788 Runnable r = new Runnable() {
789 public void run() {
790 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700791 updateItemArrays(item, itemId, stackTrace);
792 }
793 };
794 runOnWorkerThread(r);
795 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700796
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700797 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
798 final ArrayList<ItemInfo> items, final String callingFunction) {
799 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700800
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700801 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
802 Runnable r = new Runnable() {
803 public void run() {
804 ArrayList<ContentProviderOperation> ops =
805 new ArrayList<ContentProviderOperation>();
806 int count = items.size();
807 for (int i = 0; i < count; i++) {
808 ItemInfo item = items.get(i);
809 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700810 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700811 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700812
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
814 updateItemArrays(item, itemId, stackTrace);
815
816 }
817 try {
818 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
819 } catch (Exception e) {
820 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700821 }
822 }
823 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700824 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700825 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700826
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700827 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
828 // Lock on mBgLock *after* the db operation
829 synchronized (sBgLock) {
830 checkItemInfoLocked(itemId, item, stackTrace);
831
832 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
833 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
834 // Item is in a folder, make sure this folder exists
835 if (!sBgFolders.containsKey(item.container)) {
836 // An items container is being set to a that of an item which is not in
837 // the list of Folders.
838 String msg = "item: " + item + " container being set to: " +
839 item.container + ", not in the list of folders";
840 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700841 }
842 }
843
844 // Items are added/removed from the corresponding FolderInfo elsewhere, such
845 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
846 // that are on the desktop, as appropriate
847 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800848 if (modelItem != null &&
849 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
850 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700851 switch (modelItem.itemType) {
852 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
853 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
854 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
855 if (!sBgWorkspaceItems.contains(modelItem)) {
856 sBgWorkspaceItems.add(modelItem);
857 }
858 break;
859 default:
860 break;
861 }
862 } else {
863 sBgWorkspaceItems.remove(modelItem);
864 }
865 }
866 }
867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700870 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700871 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 item.cellX = cellX;
875 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700876
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 // We store hotseat items in canonical form which is this orientation invariant position
878 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700880 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700881 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700882 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700883 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
886 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
889 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800890 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700891 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
Michael Jurkac9d95c52011-08-29 14:03:34 -0700893 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700894 }
895
896 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700897 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
898 * cellX, cellY have already been updated on the ItemInfos.
899 */
900 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
901 final long container, final int screen) {
902
903 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
904 int count = items.size();
905
906 for (int i = 0; i < count; i++) {
907 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700908 item.container = container;
909
910 // We store hotseat items in canonical form which is this orientation invariant position
911 // in the hotseat
912 if (context instanceof Launcher && screen < 0 &&
913 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700914 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700915 item.cellY);
916 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700918 }
919
920 final ContentValues values = new ContentValues();
921 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
922 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
923 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800924 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700926
927 contentValues.add(values);
928 }
929 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
930 }
931
932 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700933 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800934 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700935 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700937 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800938 item.cellX = cellX;
939 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700940 item.spanX = spanX;
941 item.spanY = spanY;
942
943 // We store hotseat items in canonical form which is this orientation invariant position
944 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700946 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700948 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700950 }
Adam Cohend4844c32011-02-18 19:25:06 -0800951
Adam Cohend4844c32011-02-18 19:25:06 -0800952 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800953 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700954 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
955 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800956 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700957 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
958 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800960
Michael Jurka816474f2012-06-25 14:49:02 -0700961 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700962 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700963
964 /**
965 * Update an item to the database in a specified container.
966 */
967 static void updateItemInDatabase(Context context, final ItemInfo item) {
968 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100969 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700970 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800971 }
972
973 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700974 * Returns true if the shortcuts already exists on the workspace. This must be called after
975 * the workspace has been loaded. We identify a shortcut by its intent.
976 * TODO: Throw exception is above condition is not met.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 */
Sunny Goyal756adbc2015-04-16 15:20:51 -0700978 @Thunk static boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700979 final Intent intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700980 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700981 if (intent.getComponent() != null) {
982 // If component is not null, an intent with null package will produce
983 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700984 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700985 if (intent.getPackage() != null) {
986 intentWithPkg = intent;
987 intentWithoutPkg = new Intent(intent).setPackage(null);
988 } else {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700989 intentWithPkg = new Intent(intent).setPackage(packageName);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700990 intentWithoutPkg = intent;
991 }
992 } else {
993 intentWithPkg = intent;
994 intentWithoutPkg = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700995 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700996 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700997
998 synchronized (sBgLock) {
999 for (ItemInfo item : sBgItemsIdMap.values()) {
1000 if (item instanceof ShortcutInfo) {
1001 ShortcutInfo info = (ShortcutInfo) item;
1002 if (intentWithPkg.equals(info.getIntent())
1003 || intentWithoutPkg.equals(info.getIntent())) {
1004 return true;
1005 }
1006 }
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001009 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001010 }
1011
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 /**
1013 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
1014 */
1015 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
1016 final ContentResolver cr = context.getContentResolver();
1017 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
1018 "_id=? and (itemType=? or itemType=?)",
1019 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -07001020 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001021
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 try {
1023 if (c.moveToFirst()) {
1024 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1025 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1026 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1027 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1028 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1029 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001030 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 FolderInfo folderInfo = null;
1033 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001034 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1035 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001037 }
1038
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 folderInfo.title = c.getString(titleIndex);
1040 folderInfo.id = id;
1041 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -07001042 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001043 folderInfo.cellX = c.getInt(cellXIndex);
1044 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001045 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046
1047 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 } finally {
1050 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001051 }
1052
1053 return null;
1054 }
1055
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 /**
1057 * Add an item to the database in a specified container. Sets the container, screen, cellX and
1058 * cellY fields of the item. Also assigns an ID to the item.
1059 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001060 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001061 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 item.cellX = cellX;
1064 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 // We store hotseat items in canonical form which is this orientation invariant position
1066 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001070 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001071 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073
1074 final ContentValues values = new ContentValues();
1075 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001076 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077
Michael Jurka414300a2013-08-27 15:42:35 +02001078 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001079 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001080
Jason Monk8e19cf22014-03-20 15:06:57 -04001081 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001082 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001083 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001084 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001086 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001087 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001088 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001089 sBgItemsIdMap.put(item.id, item);
1090 switch (item.itemType) {
1091 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1092 sBgFolders.put(item.id, (FolderInfo) item);
1093 // Fall through
1094 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1095 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1096 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1097 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1098 sBgWorkspaceItems.add(item);
1099 } else {
1100 if (!sBgFolders.containsKey(item.container)) {
1101 // Adding an item to a folder that doesn't exist.
1102 String msg = "adding item: " + item + " to a folder that " +
1103 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001104 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001105 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001106 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 break;
1108 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1109 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1110 break;
1111 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001112 }
1113 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001114 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001116 }
1117
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001119 * Creates a new unique child id, for a given cell span across all layouts.
1120 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001121 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001123 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001125 }
1126
Sunny Goyal34942622014-08-29 17:20:55 -07001127 private static ArrayList<ItemInfo> getItemsByPackageName(
1128 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001129 ItemInfoFilter filter = new ItemInfoFilter() {
1130 @Override
1131 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1132 return cn.getPackageName().equals(pn) && info.user.equals(user);
1133 }
1134 };
Sunny Goyal34942622014-08-29 17:20:55 -07001135 return filterItemInfos(sBgItemsIdMap.values(), filter);
1136 }
1137
1138 /**
1139 * Removes all the items from the database corresponding to the specified package.
1140 */
1141 static void deletePackageFromDatabase(Context context, final String pn,
1142 final UserHandleCompat user) {
1143 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001144 }
1145
1146 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001147 * Removes the specified item from the database
1148 * @param context
1149 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001151 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001152 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1153 items.add(item);
1154 deleteItemsFromDatabase(context, items);
1155 }
1156
1157 /**
1158 * Removes the specified items from the database
1159 * @param context
1160 * @param item
1161 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001162 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001163 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001164 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001165 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001166 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001167 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001168 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001169
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001170 // Lock on mBgLock *after* the db operation
1171 synchronized (sBgLock) {
1172 switch (item.itemType) {
1173 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1174 sBgFolders.remove(item.id);
1175 for (ItemInfo info: sBgItemsIdMap.values()) {
1176 if (info.container == item.id) {
1177 // We are deleting a folder which still contains items that
1178 // think they are contained by that folder.
1179 String msg = "deleting a folder (" + item + ") which still " +
1180 "contains items (" + info + ")";
1181 Log.e(TAG, msg);
1182 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001183 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001184 sBgWorkspaceItems.remove(item);
1185 break;
1186 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1187 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1188 sBgWorkspaceItems.remove(item);
1189 break;
1190 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1191 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1192 break;
1193 }
1194 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001196 }
1197 }
Michael Jurka83df1882011-08-31 20:59:26 -07001198 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
1201
1202 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001203 * Update the order of the workspace screens in the database. The array list contains
1204 * a list of screen ids in the order that they should appear.
1205 */
Winson Chungc9168342013-06-26 14:54:55 -07001206 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001207 // Log to disk
1208 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1209 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1210
Winson Chung64359a52013-07-08 17:17:08 -07001211 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 final ContentResolver cr = context.getContentResolver();
1213 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1214
1215 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001216 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001217 while (iter.hasNext()) {
1218 long id = iter.next();
1219 if (id < 0) {
1220 iter.remove();
1221 }
1222 }
1223
1224 Runnable r = new Runnable() {
1225 @Override
1226 public void run() {
Yura085c8532014-02-11 15:15:29 +00001227 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001228 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001229 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001230 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001231 for (int i = 0; i < count; i++) {
1232 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001233 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001234 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1235 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001236 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001237 }
Yura085c8532014-02-11 15:15:29 +00001238
1239 try {
1240 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1241 } catch (Exception ex) {
1242 throw new RuntimeException(ex);
1243 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001244
Winson Chungba9c37f2013-08-30 14:11:37 -07001245 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001246 sBgWorkspaceScreens.clear();
1247 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001248 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 }
1250 };
1251 runOnWorkerThread(r);
1252 }
1253
1254 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 * Remove the contents of the specified folder from the database
1256 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001257 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 final ContentResolver cr = context.getContentResolver();
1259
Michael Jurkac9d95c52011-08-29 14:03:34 -07001260 Runnable r = new Runnable() {
1261 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001262 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001263 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001264 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001265 sBgItemsIdMap.remove(info.id);
1266 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001267 sBgWorkspaceItems.remove(info);
1268 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001269
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001270 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001271 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001272 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001273 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001274 for (ItemInfo childInfo : info.contents) {
1275 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001276 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001277 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001278 }
1279 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001280 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281 }
1282
1283 /**
1284 * Set this as the current Launcher activity object for the loader.
1285 */
1286 public void initialize(Callbacks callbacks) {
1287 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001288 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1289 // workspace to prevent leaking Launcher activities on orientation change.
1290 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 mCallbacks = new WeakReference<Callbacks>(callbacks);
1292 }
1293 }
1294
Kenny Guyed131872014-04-30 03:02:21 +01001295 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001296 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001297 int op = PackageUpdatedTask.OP_UPDATE;
1298 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1299 user));
1300 }
1301
1302 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001303 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001304 int op = PackageUpdatedTask.OP_REMOVE;
1305 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1306 user));
1307 }
1308
1309 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001310 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001311 int op = PackageUpdatedTask.OP_ADD;
1312 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1313 user));
1314 }
1315
1316 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001317 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001318 boolean replacing) {
1319 if (!replacing) {
1320 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1321 user));
1322 if (mAppsCanBeOnRemoveableStorage) {
1323 // Only rebind if we support removable storage. It catches the
1324 // case where
1325 // apps on the external sd card need to be reloaded
1326 startLoaderFromBackground();
1327 }
1328 } else {
1329 // If we are replacing then just update the packages in the list
1330 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1331 packageNames, user));
1332 }
1333 }
1334
1335 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001336 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001337 boolean replacing) {
1338 if (!replacing) {
1339 enqueuePackageUpdated(new PackageUpdatedTask(
1340 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1341 user));
1342 }
Kenny Guyed131872014-04-30 03:02:21 +01001343 }
1344
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001345 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001346 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1347 * ACTION_PACKAGE_CHANGED.
1348 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001349 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001350 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001351 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001352
Joe Onorato36115782010-06-17 13:28:48 -04001353 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001354 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001355 // If we have changed locale we need to clear out the labels in all apps/workspace.
1356 forceReload();
1357 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1358 // Check if configuration change was an mcc/mnc change which would affect app resources
1359 // and we would need to clear out the labels in all apps/workspace. Same handling as
1360 // above for ACTION_LOCALE_CHANGED
1361 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001362 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001363 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001364 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001365 forceReload();
1366 }
1367 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001368 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001369 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1370 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001371 Callbacks callbacks = getCallback();
1372 if (callbacks != null) {
1373 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001374 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001375 }
1376 }
1377
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001378 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001379 resetLoadedState(true, true);
1380
Reena Lee93f824a2011-09-23 17:20:28 -07001381 // Do this here because if the launcher activity is running it will be restarted.
1382 // If it's not running startLoaderFromBackground will merely tell it that it needs
1383 // to reload.
1384 startLoaderFromBackground();
1385 }
1386
Winson Chungf0c6ae02012-03-21 16:10:31 -07001387 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1388 synchronized (mLock) {
1389 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1390 // mWorkspaceLoaded to true later
1391 stopLoaderLocked();
1392 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1393 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1394 }
1395 }
1396
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001397 /**
1398 * When the launcher is in the background, it's possible for it to miss paired
1399 * configuration changes. So whenever we trigger the loader from the background
1400 * tell the launcher that it needs to re-run the loader when it comes back instead
1401 * of doing it now.
1402 */
1403 public void startLoaderFromBackground() {
1404 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001405 Callbacks callbacks = getCallback();
1406 if (callbacks != null) {
1407 // Only actually run the loader if they're not paused.
1408 if (!callbacks.setLoadOnResume()) {
1409 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001410 }
1411 }
1412 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001413 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001414 }
Joe Onorato36115782010-06-17 13:28:48 -04001415 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001416
Reena Lee93f824a2011-09-23 17:20:28 -07001417 // If there is already a loader task running, tell it to stop.
1418 // returns true if isLaunching() was true on the old task
1419 private boolean stopLoaderLocked() {
1420 boolean isLaunching = false;
1421 LoaderTask oldTask = mLoaderTask;
1422 if (oldTask != null) {
1423 if (oldTask.isLaunching()) {
1424 isLaunching = true;
1425 }
1426 oldTask.stopLocked();
1427 }
1428 return isLaunching;
1429 }
1430
Adam Cohen1a85c582014-09-30 09:48:49 -07001431 public boolean isCurrentCallbacks(Callbacks callbacks) {
1432 return (mCallbacks != null && mCallbacks.get() == callbacks);
1433 }
1434
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001436 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1437 }
1438
1439 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001440 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1441 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001442 synchronized (mLock) {
1443 if (DEBUG_LOADERS) {
1444 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 // Clear any deferred bind-runnables from the synchronized load process
1448 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001449 synchronized (mDeferredBindRunnables) {
1450 mDeferredBindRunnables.clear();
1451 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452
Joe Onorato36115782010-06-17 13:28:48 -04001453 // Don't bother to start the thread if we know it's not going to do anything
1454 if (mCallbacks != null && mCallbacks.get() != null) {
1455 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001456 // also, don't downgrade isLaunching if we're already running
1457 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001458 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001459 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1460 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1462 } else {
1463 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1464 sWorker.post(mLoaderTask);
1465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
1468 }
1469
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001470 void bindRemainingSynchronousPages() {
1471 // Post the remaining side pages to be loaded
1472 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001473 Runnable[] deferredBindRunnables = null;
1474 synchronized (mDeferredBindRunnables) {
1475 deferredBindRunnables = mDeferredBindRunnables.toArray(
1476 new Runnable[mDeferredBindRunnables.size()]);
1477 mDeferredBindRunnables.clear();
1478 }
1479 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001480 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482 }
1483 }
1484
Joe Onorato36115782010-06-17 13:28:48 -04001485 public void stopLoader() {
1486 synchronized (mLock) {
1487 if (mLoaderTask != null) {
1488 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
1490 }
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001493 /**
1494 * Loads the workspace screen ids in an ordered list.
1495 */
Adam Cohen091440a2015-03-18 14:16:05 -07001496 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001497 final ContentResolver contentResolver = context.getContentResolver();
1498 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001499
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001500 // Get screens ordered by rank.
1501 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1502 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1503 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001504 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001505 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001506 while (sc.moveToNext()) {
1507 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001508 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001509 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001510 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1511 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001512 }
1513 }
1514 } finally {
1515 sc.close();
1516 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001517 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001518 }
1519
Michael Jurkac57b7a82011-08-09 22:02:20 -07001520 public boolean isAllAppsLoaded() {
1521 return mAllAppsLoaded;
1522 }
1523
Winson Chung36a62fe2012-05-06 18:04:42 -07001524 boolean isLoadingWorkspace() {
1525 synchronized (mLock) {
1526 if (mLoaderTask != null) {
1527 return mLoaderTask.isLoadingWorkspace();
1528 }
1529 }
1530 return false;
1531 }
1532
Joe Onorato36115782010-06-17 13:28:48 -04001533 /**
1534 * Runnable for the thread that loads the contents of the launcher:
1535 * - workspace icons
1536 * - widgets
1537 * - all apps icons
1538 */
1539 private class LoaderTask implements Runnable {
1540 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001541 private boolean mIsLaunching;
Adam Cohen091440a2015-03-18 14:16:05 -07001542 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001543 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001544 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001545 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001546
Dan Sandlerd5024042014-01-09 15:01:33 -05001547 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001548 mContext = context;
1549 mIsLaunching = isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001550 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 }
1552
Joe Onorato36115782010-06-17 13:28:48 -04001553 boolean isLaunching() {
1554 return mIsLaunching;
1555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556
Winson Chung36a62fe2012-05-06 18:04:42 -07001557 boolean isLoadingWorkspace() {
1558 return mIsLoadingAndBindingWorkspace;
1559 }
1560
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001561 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001562 mIsLoadingAndBindingWorkspace = true;
1563
Joe Onorato36115782010-06-17 13:28:48 -04001564 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001565 if (DEBUG_LOADERS) {
1566 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001568
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001569 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001570 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001571 synchronized (LoaderTask.this) {
1572 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001573 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001574 }
1575 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 }
1577 }
1578
Joe Onorato36115782010-06-17 13:28:48 -04001579 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001580 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001581 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 private void waitForIdle() {
1584 // Wait until the either we're stopped or the other threads are done.
1585 // This way we don't start loading all apps until the workspace has settled
1586 // down.
1587 synchronized (LoaderTask.this) {
1588 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001589
Joe Onorato36115782010-06-17 13:28:48 -04001590 mHandler.postIdle(new Runnable() {
1591 public void run() {
1592 synchronized (LoaderTask.this) {
1593 mLoadAndBindStepFinished = true;
1594 if (DEBUG_LOADERS) {
1595 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001596 }
Joe Onorato36115782010-06-17 13:28:48 -04001597 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001598 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001599 }
Joe Onorato36115782010-06-17 13:28:48 -04001600 });
1601
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001602 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001603 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001604 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1605 // wait no longer than 1sec at a time
1606 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001607 } catch (InterruptedException ex) {
1608 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001609 }
1610 }
Joe Onorato36115782010-06-17 13:28:48 -04001611 if (DEBUG_LOADERS) {
1612 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001613 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001614 + "ms for previous step to finish binding");
1615 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001616 }
Joe Onorato36115782010-06-17 13:28:48 -04001617 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001618
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001619 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001620 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001621 // Ensure that we have a valid page index to load synchronously
1622 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1623 "valid page index");
1624 }
1625 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1626 // Ensure that we don't try and bind a specified page when the pages have not been
1627 // loaded already (we should load everything asynchronously in that case)
1628 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1629 }
1630 synchronized (mLock) {
1631 if (mIsLoaderTaskRunning) {
1632 // Ensure that we are never running the background loading at this point since
1633 // we also touch the background collections
1634 throw new RuntimeException("Error! Background loading is already running");
1635 }
1636 }
1637
1638 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1639 // data structures, we can't allow any other thread to touch that data, but because
1640 // this call is synchronous, we can get away with not locking).
1641
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001642 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001643 // operations from the previous activity. We need to ensure that all queued operations
1644 // are executed before any synchronous binding work is done.
1645 mHandler.flush();
1646
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 // Divide the set of loaded items into those that we are binding synchronously, and
1648 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001649 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1651 // arise from that.
1652 onlyBindAllApps();
1653 }
1654
Joe Onorato36115782010-06-17 13:28:48 -04001655 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 synchronized (mLock) {
1657 mIsLoaderTaskRunning = true;
1658 }
Joe Onorato36115782010-06-17 13:28:48 -04001659 // Optimize for end-user experience: if the Launcher is up and // running with the
1660 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1661 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001662 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001663 // Elevate priority when Home launches for the first time to avoid
1664 // starving at boot time. Staring at a blank home is not cool.
1665 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001666 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1667 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001668 android.os.Process.setThreadPriority(mIsLaunching
1669 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1670 }
Winson Chung64359a52013-07-08 17:17:08 -07001671 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001672 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001673
Joe Onorato36115782010-06-17 13:28:48 -04001674 if (mStopped) {
1675 break keep_running;
1676 }
1677
1678 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1679 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001680 synchronized (mLock) {
1681 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001682 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001683 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1684 }
1685 }
Joe Onorato36115782010-06-17 13:28:48 -04001686 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001687
1688 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001689 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1690 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001691
Sunny Goyal5b0e6692015-03-19 14:31:19 -07001692 // Remove entries for packages which changed while the launcher was dead.
1693 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews();
1694
Winson Chung7ed37742011-09-08 15:45:51 -07001695 // Restore the default thread priority after we are done loading items
1696 synchronized (mLock) {
1697 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1698 }
Joe Onorato36115782010-06-17 13:28:48 -04001699 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Joe Onorato36115782010-06-17 13:28:48 -04001701 // Clear out this reference, otherwise we end up holding it until all of the
1702 // callback runnables are done.
1703 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704
Joe Onorato36115782010-06-17 13:28:48 -04001705 synchronized (mLock) {
1706 // If we are still the last one to be scheduled, remove ourselves.
1707 if (mLoaderTask == this) {
1708 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001711 }
Joe Onorato36115782010-06-17 13:28:48 -04001712 }
1713
1714 public void stopLocked() {
1715 synchronized (LoaderTask.this) {
1716 mStopped = true;
1717 this.notify();
1718 }
1719 }
1720
1721 /**
1722 * Gets the callbacks object. If we've been stopped, or if the launcher object
1723 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1724 * object that was around when the deferred message was scheduled, and if there's
1725 * a new Callbacks object around then also return null. This will save us from
1726 * calling onto it with data that will be ignored.
1727 */
1728 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1729 synchronized (mLock) {
1730 if (mStopped) {
1731 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001732 }
Joe Onorato36115782010-06-17 13:28:48 -04001733
1734 if (mCallbacks == null) {
1735 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001736 }
Joe Onorato36115782010-06-17 13:28:48 -04001737
1738 final Callbacks callbacks = mCallbacks.get();
1739 if (callbacks != oldCallbacks) {
1740 return null;
1741 }
1742 if (callbacks == null) {
1743 Log.w(TAG, "no mCallbacks");
1744 return null;
1745 }
1746
1747 return callbacks;
1748 }
1749 }
1750
1751 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001752 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001753 LauncherAppState app = LauncherAppState.getInstance();
1754 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001755 final int countX = (int) grid.numColumns;
1756 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001757
Adam Cohendcd297f2013-06-18 13:13:40 -07001758 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001759 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001760 // Return early if we detect that an item is under the hotseat button
1761 if (mCallbacks == null ||
1762 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001763 Log.e(TAG, "Error loading shortcut into hotseat " + item
1764 + " into position (" + item.screenId + ":" + item.cellX + ","
1765 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001766 return false;
1767 }
1768
Dan Sandler295ae182013-12-10 16:05:47 -05001769 final ItemInfo[][] hotseatItems =
1770 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1771
Adam Cohenae4409d2013-11-26 10:34:59 -08001772 if (item.screenId >= grid.numHotseatIcons) {
1773 Log.e(TAG, "Error loading shortcut " + item
1774 + " into hotseat position " + item.screenId
1775 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1776 + ")");
1777 return false;
1778 }
1779
Dan Sandler295ae182013-12-10 16:05:47 -05001780 if (hotseatItems != null) {
1781 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 Log.e(TAG, "Error loading shortcut into hotseat " + item
1783 + " into position (" + item.screenId + ":" + item.cellX + ","
1784 + item.cellY + ") occupied by "
1785 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1786 [(int) item.screenId][0]);
1787 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001788 } else {
1789 hotseatItems[(int) item.screenId][0] = item;
1790 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001792 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001793 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 items[(int) item.screenId][0] = item;
1795 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001796 return true;
1797 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001798 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1799 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001800 return true;
1801 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001802
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001804 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 occupied.put(item.screenId, items);
1806 }
1807
Dan Sandler295ae182013-12-10 16:05:47 -05001808 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001809 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1810 item.cellX < 0 || item.cellY < 0 ||
1811 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1812 Log.e(TAG, "Error loading shortcut " + item
1813 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1814 + item.cellX + "," + item.cellY
1815 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1816 return false;
1817 }
1818
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001819 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001820 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1821 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001822 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001823 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001825 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001826 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001827 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001828 return false;
1829 }
1830 }
1831 }
1832 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1833 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001835 }
1836 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001837
Joe Onorato36115782010-06-17 13:28:48 -04001838 return true;
1839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840
Winson Chungba9c37f2013-08-30 14:11:37 -07001841 /** Clears all the sBg data structures */
1842 private void clearSBgDataStructures() {
1843 synchronized (sBgLock) {
1844 sBgWorkspaceItems.clear();
1845 sBgAppWidgets.clear();
1846 sBgFolders.clear();
1847 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001848 sBgWorkspaceScreens.clear();
1849 }
1850 }
1851
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001852 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001853 // Log to disk
1854 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1855
Joe Onorato36115782010-06-17 13:28:48 -04001856 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857
Joe Onorato36115782010-06-17 13:28:48 -04001858 final Context context = mContext;
1859 final ContentResolver contentResolver = context.getContentResolver();
1860 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001861 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001862 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1863 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001864 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001865
Winson Chung892c74d2013-08-22 16:15:50 -07001866 LauncherAppState app = LauncherAppState.getInstance();
1867 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1868 int countX = (int) grid.numColumns;
1869 int countY = (int) grid.numRows;
1870
Dan Sandlerd5024042014-01-09 15:01:33 -05001871 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1872 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1873 LauncherAppState.getLauncherProvider().deleteDatabase();
1874 }
1875
1876 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1877 // append the user's Launcher2 shortcuts
1878 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1879 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1880 } else {
1881 // Make sure the default workspace is loaded
1882 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001883 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001884 }
Adam Cohene25af792013-06-06 23:08:25 -07001885
Winson Chung2abf94d2012-07-18 18:16:38 -07001886 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001887 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001888 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001889 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001890
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001892 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001893 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001894 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001895 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001896
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 // +1 for the hotseat (it can be larger than the workspace)
1898 // Load workspace in reverse order to ensure that latest items are loaded first (and
1899 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 try {
1903 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1904 final int intentIndex = c.getColumnIndexOrThrow
1905 (LauncherSettings.Favorites.INTENT);
1906 final int titleIndex = c.getColumnIndexOrThrow
1907 (LauncherSettings.Favorites.TITLE);
1908 final int iconTypeIndex = c.getColumnIndexOrThrow(
1909 LauncherSettings.Favorites.ICON_TYPE);
1910 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1911 final int iconPackageIndex = c.getColumnIndexOrThrow(
1912 LauncherSettings.Favorites.ICON_PACKAGE);
1913 final int iconResourceIndex = c.getColumnIndexOrThrow(
1914 LauncherSettings.Favorites.ICON_RESOURCE);
1915 final int containerIndex = c.getColumnIndexOrThrow(
1916 LauncherSettings.Favorites.CONTAINER);
1917 final int itemTypeIndex = c.getColumnIndexOrThrow(
1918 LauncherSettings.Favorites.ITEM_TYPE);
1919 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1920 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001921 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1922 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 final int screenIndex = c.getColumnIndexOrThrow(
1924 LauncherSettings.Favorites.SCREEN);
1925 final int cellXIndex = c.getColumnIndexOrThrow
1926 (LauncherSettings.Favorites.CELLX);
1927 final int cellYIndex = c.getColumnIndexOrThrow
1928 (LauncherSettings.Favorites.CELLY);
1929 final int spanXIndex = c.getColumnIndexOrThrow
1930 (LauncherSettings.Favorites.SPANX);
1931 final int spanYIndex = c.getColumnIndexOrThrow(
1932 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001933 final int rankIndex = c.getColumnIndexOrThrow(
1934 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001935 final int restoredIndex = c.getColumnIndexOrThrow(
1936 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001937 final int profileIdIndex = c.getColumnIndexOrThrow(
1938 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001939 final int optionsIndex = c.getColumnIndexOrThrow(
1940 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941
Sunny Goyal7f834d22015-04-21 10:10:23 -07001942 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1943 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1944 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1945 }
1946
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 ShortcutInfo info;
1948 String intentDescription;
1949 LauncherAppWidgetInfo appWidgetInfo;
1950 int container;
1951 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001952 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001954 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 while (!mStopped && c.moveToNext()) {
1957 try {
1958 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001959 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001960 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001961
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 switch (itemType) {
1963 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1964 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001965 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001967 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001968 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001969 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001970 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001971 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001972 if (user == null) {
1973 // User has been deleted remove the item.
1974 itemsToRemove.add(id);
1975 continue;
1976 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 try {
1978 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001979 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001980 if (cn != null && cn.getPackageName() != null) {
1981 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1982 cn.getPackageName(), user);
1983 boolean validComponent = validPkg &&
1984 launcherApps.isActivityEnabledForProfile(cn, user);
1985
1986 if (validComponent) {
1987 if (restored) {
1988 // no special handling necessary for this item
1989 restoredRows.add(id);
1990 restored = false;
1991 }
1992 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001993 intent = null;
1994 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1995 // We allow auto install apps to have their intent
1996 // updated after an install.
1997 intent = manager.getLaunchIntentForPackage(
1998 cn.getPackageName());
1999 if (intent != null) {
2000 ContentValues values = new ContentValues();
2001 values.put(LauncherSettings.Favorites.INTENT,
2002 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002003 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07002004 }
2005 }
2006
2007 if (intent == null) {
2008 // The app is installed but the component is no
2009 // longer available.
2010 Launcher.addDumpLog(TAG,
2011 "Invalid component removed: " + cn, true);
2012 itemsToRemove.add(id);
2013 continue;
2014 } else {
2015 // no special handling necessary for this item
2016 restoredRows.add(id);
2017 restored = false;
2018 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002019 } else if (restored) {
2020 // Package is not yet available but might be
2021 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05002022 Launcher.addDumpLog(TAG,
2023 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002024
2025 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
2026 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002027 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07002028 // App restore has started. Update the flag
2029 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
2030 ContentValues values = new ContentValues();
2031 values.put(LauncherSettings.Favorites.RESTORED,
2032 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002033 updateItem(id, values);
2034 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
2035 // This is a common app. Try to replace this.
2036 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
2037 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
2038 if (parser.findDefaultApp()) {
2039 // Default app found. Replace it.
2040 intent = parser.parsedIntent;
2041 cn = intent.getComponent();
2042 ContentValues values = parser.parsedValues;
2043 values.put(LauncherSettings.Favorites.RESTORED, 0);
2044 updateItem(id, values);
2045 restored = false;
2046 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07002047
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002048 } else if (REMOVE_UNRESTORED_ICONS) {
2049 Launcher.addDumpLog(TAG,
2050 "Unrestored package removed: " + cn, true);
2051 itemsToRemove.add(id);
2052 continue;
2053 }
Sunny Goyal94485362014-09-18 16:13:58 -07002054 } else if (REMOVE_UNRESTORED_ICONS) {
2055 Launcher.addDumpLog(TAG,
2056 "Unrestored package removed: " + cn, true);
2057 itemsToRemove.add(id);
2058 continue;
2059 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002060 } else if (launcherApps.isAppEnabled(
2061 manager, cn.getPackageName(),
2062 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2063 // Package is present but not available.
2064 allowMissingTarget = true;
2065 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2066 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002067 // SdCard is not ready yet. Package might get available,
2068 // once it is ready.
2069 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2070 + " (check again later)", true);
2071 HashSet<String> pkgs = sPendingPackages.get(user);
2072 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002073 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002074 sPendingPackages.put(user, pkgs);
2075 }
2076 pkgs.add(cn.getPackageName());
2077 allowMissingTarget = true;
2078 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002079
2080 } else {
2081 // Do not wait for external media load anymore.
2082 // Log the invalid package, and remove it
2083 Launcher.addDumpLog(TAG,
2084 "Invalid package removed: " + cn, true);
2085 itemsToRemove.add(id);
2086 continue;
Winson Chungee055712013-07-30 14:46:24 -07002087 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002088 } else if (cn == null) {
2089 // For shortcuts with no component, keep them as they are
2090 restoredRows.add(id);
2091 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002092 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002094 Launcher.addDumpLog(TAG,
2095 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 continue;
2097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002098
Sunny Goyal34b65272015-03-11 16:56:52 -07002099 container = c.getInt(containerIndex);
2100 boolean useLowResIcon = container >= 0 &&
2101 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2102
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002103 if (itemReplaced) {
2104 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002105 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002106 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002107 } else {
2108 // Don't replace items for other profiles.
2109 itemsToRemove.add(id);
2110 continue;
2111 }
2112 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002113 if (user.equals(UserHandleCompat.myUserHandle())) {
2114 Launcher.addDumpLog(TAG,
2115 "constructing info for partially restored package",
2116 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002117 info = getRestoredItemInfo(c, titleIndex, intent,
2118 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002119 intent = getRestoredItemIntent(c, context, intent);
2120 } else {
2121 // Don't restore items for other profiles.
2122 itemsToRemove.add(id);
2123 continue;
2124 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002125 } else if (itemType ==
2126 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002127 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002128 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 } else {
2130 info = getShortcutInfo(c, context, iconTypeIndex,
2131 iconPackageIndex, iconResourceIndex, iconIndex,
2132 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002133
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 // App shortcuts that used to be automatically added to Launcher
2135 // didn't always have the correct intent flags set, so do that
2136 // here
2137 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002138 intent.getCategories() != null &&
2139 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002140 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 intent.addFlags(
2142 Intent.FLAG_ACTIVITY_NEW_TASK |
2143 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2144 }
Michael Jurka96879562012-03-22 05:54:33 -07002145 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002148 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002151 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 info.cellX = c.getInt(cellXIndex);
2153 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002154 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002155 info.spanX = 1;
2156 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002157 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002158 info.isDisabled = disabledState;
2159 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2160 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2161 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002162
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002164 if (!checkItemPlacement(occupied, info)) {
2165 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 break;
2167 }
2168
Sunny Goyal756adbc2015-04-16 15:20:51 -07002169 if (restored) {
2170 ComponentName cn = info.getTargetComponent();
2171 if (cn != null) {
2172 Integer progress = installingPkgs.get(cn.getPackageName());
2173 if (progress != null) {
2174 info.setInstallProgress(progress);
2175 } else {
2176 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2177 }
2178 }
2179 }
2180
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 switch (container) {
2182 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2183 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2184 sBgWorkspaceItems.add(info);
2185 break;
2186 default:
2187 // Item is in a user folder
2188 FolderInfo folderInfo =
2189 findOrMakeFolder(sBgFolders, container);
2190 folderInfo.add(info);
2191 break;
2192 }
2193 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002194 } else {
2195 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 }
2197 break;
2198
2199 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2200 id = c.getLong(idIndex);
2201 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2202
2203 folderInfo.title = c.getString(titleIndex);
2204 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002207 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 folderInfo.cellX = c.getInt(cellXIndex);
2209 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002210 folderInfo.spanX = 1;
2211 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002212 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213
Daniel Sandler8802e962010-05-26 16:28:16 -04002214 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002215 if (!checkItemPlacement(occupied, folderInfo)) {
2216 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002217 break;
2218 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002219
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2222 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2223 sBgWorkspaceItems.add(folderInfo);
2224 break;
Joe Onorato36115782010-06-17 13:28:48 -04002225 }
Joe Onorato17a89222011-02-08 17:26:11 -08002226
Chris Wrenf4d08112014-01-16 18:13:56 -05002227 if (restored) {
2228 // no special handling required for restored folders
2229 restoredRows.add(id);
2230 }
2231
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2233 sBgFolders.put(folderInfo.id, folderInfo);
2234 break;
2235
2236 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002237 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002239 boolean customWidget = itemType ==
2240 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002243 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002244 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002245 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002246 user = allUsers.get(serialNumber);
2247 if (user == null) {
2248 itemsToRemove.add(id);
2249 continue;
2250 }
2251
Sunny Goyalff572272014-07-23 13:58:07 -07002252 final ComponentName component =
2253 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002254
Sunny Goyal651077b2014-06-30 14:15:31 -07002255 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002256 final boolean isIdValid = (restoreStatus &
2257 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002258 final boolean wasProviderReady = (restoreStatus &
2259 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002260
Adam Cohen59400422014-03-05 18:07:04 -08002261 final LauncherAppWidgetProviderInfo provider =
2262 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002263 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002264 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002265
2266 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002267 if (!isSafeMode && !customWidget &&
2268 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002269 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002270 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002271
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002273 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 itemsToRemove.add(id);
2275 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002276 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002277 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2278 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002279
2280 if (!customWidget) {
2281 int[] minSpan =
2282 Launcher.getMinSpanForWidget(context, provider);
2283 appWidgetInfo.minSpanX = minSpan[0];
2284 appWidgetInfo.minSpanY = minSpan[1];
2285 }
Sunny Goyalff572272014-07-23 13:58:07 -07002286
2287 int status = restoreStatus;
2288 if (!wasProviderReady) {
2289 // If provider was not previously ready, update the
2290 // status and UI flag.
2291
2292 // Id would be valid only if the widget restore broadcast was received.
2293 if (isIdValid) {
2294 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2295 } else {
2296 status &= ~LauncherAppWidgetInfo
2297 .FLAG_PROVIDER_NOT_READY;
2298 }
2299 }
2300 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002301 } else {
2302 Log.v(TAG, "Widget restore pending id=" + id
2303 + " appWidgetId=" + appWidgetId
2304 + " status =" + restoreStatus);
2305 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002306 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002307 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002308 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002309
2310 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2311 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002312 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002313 // App restore has started. Update the flag
2314 appWidgetInfo.restoreStatus |=
2315 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002316 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002317 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002318 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002319 itemsToRemove.add(id);
2320 continue;
2321 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002322
2323 appWidgetInfo.installProgress =
2324 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002325 }
Sunny Goyalff572272014-07-23 13:58:07 -07002326
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 appWidgetInfo.cellX = c.getInt(cellXIndex);
2330 appWidgetInfo.cellY = c.getInt(cellYIndex);
2331 appWidgetInfo.spanX = c.getInt(spanXIndex);
2332 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002333
Adam Cohen59400422014-03-05 18:07:04 -08002334 if (!customWidget) {
2335 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2336 appWidgetInfo.minSpanX = minSpan[0];
2337 appWidgetInfo.minSpanY = minSpan[1];
2338 }
2339
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 container = c.getInt(containerIndex);
2341 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2342 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2343 Log.e(TAG, "Widget found where container != " +
2344 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2345 continue;
2346 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347
Adam Cohene25af792013-06-06 23:08:25 -07002348 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002350 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2351 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 break;
2353 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002354
Adam Cohen59400422014-03-05 18:07:04 -08002355 if (!customWidget) {
2356 String providerName =
2357 appWidgetInfo.providerName.flattenToString();
2358 if (!providerName.equals(savedProvider) ||
2359 (appWidgetInfo.restoreStatus != restoreStatus)) {
2360 ContentValues values = new ContentValues();
2361 values.put(
2362 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2363 providerName);
2364 values.put(LauncherSettings.Favorites.RESTORED,
2365 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002366 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002367 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002368 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2370 sBgAppWidgets.add(appWidgetInfo);
2371 }
Joe Onorato36115782010-06-17 13:28:48 -04002372 break;
2373 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002375 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002376 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 }
2378 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002379 if (c != null) {
2380 c.close();
2381 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 }
2383
Winson Chungba9c37f2013-08-30 14:11:37 -07002384 // Break early if we've stopped loading
2385 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002386 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002387 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002388 }
2389
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 if (itemsToRemove.size() > 0) {
2391 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002392 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 // Remove dead items
2394 for (long id : itemsToRemove) {
2395 if (DEBUG_LOADERS) {
2396 Log.d(TAG, "Removed id = " + id);
2397 }
2398 // Don't notify content observers
2399 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002400 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002401 } catch (RemoteException e) {
2402 Log.w(TAG, "Could not remove id = " + id);
2403 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002404 }
2405 }
2406
Chris Wrenf4d08112014-01-16 18:13:56 -05002407 if (restoredRows.size() > 0) {
2408 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002409 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002410 // Update restored items that no longer require special handling
2411 try {
2412 StringBuilder selectionBuilder = new StringBuilder();
2413 selectionBuilder.append(LauncherSettings.Favorites._ID);
2414 selectionBuilder.append(" IN (");
2415 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2416 selectionBuilder.append(")");
2417 ContentValues values = new ContentValues();
2418 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002419 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002420 values, selectionBuilder.toString(), null);
2421 } catch (RemoteException e) {
2422 Log.w(TAG, "Could not update restored rows");
2423 }
2424 }
2425
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002426 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2427 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002428 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002429 null, sWorker);
2430 }
2431
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002432 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2433 // Log to disk
2434 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2435 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002436
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002437 // Remove any empty screens
2438 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2439 for (ItemInfo item: sBgItemsIdMap.values()) {
2440 long screenId = item.screenId;
2441 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2442 unusedScreens.contains(screenId)) {
2443 unusedScreens.remove(screenId);
2444 }
2445 }
2446
2447 // If there are any empty screens remove them, and update.
2448 if (unusedScreens.size() != 0) {
2449 // Log to disk
2450 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2451 TextUtils.join(", ", unusedScreens), true);
2452
2453 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002454 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 }
2456
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 if (DEBUG_LOADERS) {
2458 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2459 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002461 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002463
Daniel Sandler566da102013-06-25 23:43:45 -04002464 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002465 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002466 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002467 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 line += " | ";
2469 }
Winson Chung892c74d2013-08-22 16:15:50 -07002470 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002471 ItemInfo[][] screen = occupied.get(screenId);
2472 if (x < screen.length && y < screen[x].length) {
2473 line += (screen[x][y] != null) ? "#" : ".";
2474 } else {
2475 line += "!";
2476 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 }
Joe Onorato36115782010-06-17 13:28:48 -04002478 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002481 }
Joe Onorato36115782010-06-17 13:28:48 -04002482 }
Adam Cohene25af792013-06-06 23:08:25 -07002483 }
2484
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002485 /**
2486 * Partially updates the item without any notification. Must be called on the worker thread.
2487 */
2488 private void updateItem(long itemId, ContentValues update) {
2489 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002490 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002491 update,
2492 BaseColumns._ID + "= ?",
2493 new String[]{Long.toString(itemId)});
2494 }
2495
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 /** Filters the set of items who are directly or indirectly (via another container) on the
2497 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002498 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<ItemInfo> allWorkspaceItems,
2500 ArrayList<ItemInfo> currentScreenItems,
2501 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002502 // Purge any null ItemInfos
2503 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2504 while (iter.hasNext()) {
2505 ItemInfo i = iter.next();
2506 if (i == null) {
2507 iter.remove();
2508 }
2509 }
2510
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 // Order the set of items by their containers first, this allows use to walk through the
2512 // list sequentially, build up a list of containers that are in the specified screen,
2513 // as well as all items in those containers.
2514 Set<Long> itemsOnScreen = new HashSet<Long>();
2515 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2516 @Override
2517 public int compare(ItemInfo lhs, ItemInfo rhs) {
2518 return (int) (lhs.container - rhs.container);
2519 }
2520 });
2521 for (ItemInfo info : allWorkspaceItems) {
2522 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 currentScreenItems.add(info);
2525 itemsOnScreen.add(info.id);
2526 } else {
2527 otherScreenItems.add(info);
2528 }
2529 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2530 currentScreenItems.add(info);
2531 itemsOnScreen.add(info.id);
2532 } else {
2533 if (itemsOnScreen.contains(info.container)) {
2534 currentScreenItems.add(info);
2535 itemsOnScreen.add(info.id);
2536 } else {
2537 otherScreenItems.add(info);
2538 }
2539 }
2540 }
2541 }
2542
2543 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002544 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 ArrayList<LauncherAppWidgetInfo> appWidgets,
2546 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2547 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548
2549 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002550 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002552 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553 currentScreenWidgets.add(widget);
2554 } else {
2555 otherScreenWidgets.add(widget);
2556 }
2557 }
2558 }
2559
2560 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002561 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 HashMap<Long, ItemInfo> itemsIdMap,
2563 HashMap<Long, FolderInfo> folders,
2564 HashMap<Long, FolderInfo> currentScreenFolders,
2565 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566
2567 for (long id : folders.keySet()) {
2568 ItemInfo info = itemsIdMap.get(id);
2569 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002570 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002572 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 currentScreenFolders.put(id, folder);
2574 } else {
2575 otherScreenFolders.put(id, folder);
2576 }
2577 }
2578 }
2579
2580 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2581 * right) */
2582 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002583 final LauncherAppState app = LauncherAppState.getInstance();
2584 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585 // XXX: review this
2586 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002587 @Override
2588 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002589 int cellCountX = (int) grid.numColumns;
2590 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002591 int screenOffset = cellCountX * cellCountY;
2592 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002593 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002594 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002595 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002596 rhs.cellY * cellCountX + rhs.cellX);
2597 return (int) (lr - rr);
2598 }
2599 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002600 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002601
Adam Cohendcd297f2013-06-18 13:13:40 -07002602 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2603 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002604 final Runnable r = new Runnable() {
2605 @Override
2606 public void run() {
2607 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2608 if (callbacks != null) {
2609 callbacks.bindScreens(orderedScreens);
2610 }
2611 }
2612 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002613 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002614 }
2615
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002616 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2617 final ArrayList<ItemInfo> workspaceItems,
2618 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2619 final HashMap<Long, FolderInfo> folders,
2620 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002621
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002622 final boolean postOnMainThread = (deferredBindRunnables != null);
2623
2624 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002625 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002626 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002627 final int start = i;
2628 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 final Runnable r = new Runnable() {
2630 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002631 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002632 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002633 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002634 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2635 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002636 }
2637 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002638 };
2639 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002640 synchronized (deferredBindRunnables) {
2641 deferredBindRunnables.add(r);
2642 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002644 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645 }
Joe Onorato36115782010-06-17 13:28:48 -04002646 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002647
2648 // Bind the folders
2649 if (!folders.isEmpty()) {
2650 final Runnable r = new Runnable() {
2651 public void run() {
2652 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2653 if (callbacks != null) {
2654 callbacks.bindFolders(folders);
2655 }
2656 }
2657 };
2658 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002659 synchronized (deferredBindRunnables) {
2660 deferredBindRunnables.add(r);
2661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002663 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664 }
2665 }
2666
2667 // Bind the widgets, one at a time
2668 N = appWidgets.size();
2669 for (int i = 0; i < N; i++) {
2670 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2671 final Runnable r = new Runnable() {
2672 public void run() {
2673 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2674 if (callbacks != null) {
2675 callbacks.bindAppWidget(widget);
2676 }
2677 }
2678 };
2679 if (postOnMainThread) {
2680 deferredBindRunnables.add(r);
2681 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002682 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002683 }
2684 }
2685 }
2686
2687 /**
2688 * Binds all loaded data to actual views on the main thread.
2689 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002690 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002691 final long t = SystemClock.uptimeMillis();
2692 Runnable r;
2693
2694 // Don't use these two variables in any of the callback runnables.
2695 // Otherwise we hold a reference to them.
2696 final Callbacks oldCallbacks = mCallbacks.get();
2697 if (oldCallbacks == null) {
2698 // This launcher has exited and nobody bothered to tell us. Just bail.
2699 Log.w(TAG, "LoaderTask running with no launcher");
2700 return;
2701 }
2702
Winson Chung9b9fb962013-11-15 15:39:34 -08002703 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002704 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2705 ArrayList<LauncherAppWidgetInfo> appWidgets =
2706 new ArrayList<LauncherAppWidgetInfo>();
2707 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2708 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002709 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002710 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002711 workspaceItems.addAll(sBgWorkspaceItems);
2712 appWidgets.addAll(sBgAppWidgets);
2713 folders.putAll(sBgFolders);
2714 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002715 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002716 }
2717
Derek Prothro7aff3992013-12-10 14:00:37 -05002718 final boolean isLoadingSynchronously =
2719 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002720 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002721 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002722 if (currScreen >= orderedScreenIds.size()) {
2723 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002724 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002725 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002726 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002727 final long currentScreenId = currentScreen < 0
2728 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002729
2730 // Load all the items that are on the current page first (and in the process, unbind
2731 // all the existing workspace items before we call startBinding() below.
2732 unbindWorkspaceItemsOnMainThread();
2733
2734 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002735 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2736 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2737 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2738 new ArrayList<LauncherAppWidgetInfo>();
2739 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2740 new ArrayList<LauncherAppWidgetInfo>();
2741 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2742 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2743
Winson Chung9b9fb962013-11-15 15:39:34 -08002744 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002745 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002746 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002747 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002748 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002749 otherFolders);
2750 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2751 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2752
2753 // Tell the workspace that we're about to start binding items
2754 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002755 public void run() {
2756 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2757 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002758 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002759 }
2760 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002761 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002762 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002763
Adam Cohendcd297f2013-06-18 13:13:40 -07002764 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2765
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002766 // Load items on the current page
2767 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2768 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002769 if (isLoadingSynchronously) {
2770 r = new Runnable() {
2771 public void run() {
2772 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002773 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002774 callbacks.onPageBoundSynchronously(currentScreen);
2775 }
2776 }
2777 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002778 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002779 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002780
Winson Chung4a2afa32012-07-19 14:53:05 -07002781 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2782 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002783 synchronized (mDeferredBindRunnables) {
2784 mDeferredBindRunnables.clear();
2785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002786 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002787 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002788
2789 // Tell the workspace that we're done binding items
2790 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002791 public void run() {
2792 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2793 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002794 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002795 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002796
Winson Chung98e030b2012-05-07 16:01:11 -07002797 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002798 if (DEBUG_LOADERS) {
2799 Log.d(TAG, "bound workspace in "
2800 + (SystemClock.uptimeMillis()-t) + "ms");
2801 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002802
2803 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002805 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002806 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002807 synchronized (mDeferredBindRunnables) {
2808 mDeferredBindRunnables.add(r);
2809 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002810 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002811 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002812 }
Joe Onorato36115782010-06-17 13:28:48 -04002813 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002814
Joe Onorato36115782010-06-17 13:28:48 -04002815 private void loadAndBindAllApps() {
2816 if (DEBUG_LOADERS) {
2817 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2818 }
2819 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002820 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002821 synchronized (LoaderTask.this) {
2822 if (mStopped) {
2823 return;
2824 }
2825 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002826 }
Joe Onorato36115782010-06-17 13:28:48 -04002827 } else {
2828 onlyBindAllApps();
2829 }
2830 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002831
Joe Onorato36115782010-06-17 13:28:48 -04002832 private void onlyBindAllApps() {
2833 final Callbacks oldCallbacks = mCallbacks.get();
2834 if (oldCallbacks == null) {
2835 // This launcher has exited and nobody bothered to tell us. Just bail.
2836 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2837 return;
2838 }
2839
2840 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002841 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002842 final ArrayList<AppInfo> list
2843 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002844 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002845 public void run() {
2846 final long t = SystemClock.uptimeMillis();
2847 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2848 if (callbacks != null) {
2849 callbacks.bindAllApplications(list);
2850 }
2851 if (DEBUG_LOADERS) {
2852 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2853 + (SystemClock.uptimeMillis()-t) + "ms");
2854 }
2855 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002856 };
2857 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002858 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002859 r.run();
2860 } else {
2861 mHandler.post(r);
2862 }
Joe Onorato36115782010-06-17 13:28:48 -04002863 }
2864
Winson Chung64359a52013-07-08 17:17:08 -07002865 private void loadAllApps() {
2866 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002867
Joe Onorato36115782010-06-17 13:28:48 -04002868 final Callbacks oldCallbacks = mCallbacks.get();
2869 if (oldCallbacks == null) {
2870 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002871 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002872 return;
2873 }
2874
2875 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2876 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2877
Kenny Guyed131872014-04-30 03:02:21 +01002878 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2879
Winson Chung64359a52013-07-08 17:17:08 -07002880 // Clear the list of apps
2881 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002882 for (UserHandleCompat user : profiles) {
2883 // Query for the set of apps
2884 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2885 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2886 if (DEBUG_LOADERS) {
2887 Log.d(TAG, "getActivityList took "
2888 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2889 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2890 }
2891 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002892 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002893 if (apps == null || apps.isEmpty()) {
2894 return;
2895 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002896
2897 // Update icon cache
2898 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2899
2900 // If any package icon has changed (app was updated while launcher was dead),
2901 // update the corresponding shortcuts.
2902 if (!updatedPackages.isEmpty()) {
2903 final ArrayList<ShortcutInfo> updates = new ArrayList<ShortcutInfo>();
2904 synchronized (sBgLock) {
2905 for (ItemInfo info : sBgItemsIdMap.values()) {
2906 if (info instanceof ShortcutInfo && user.equals(info.user)
2907 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2908 ShortcutInfo si = (ShortcutInfo) info;
2909 ComponentName cn = si.getTargetComponent();
2910 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2911 si.updateIcon(mIconCache);
2912 updates.add(si);
2913 }
2914 }
2915 }
2916 }
2917
2918 if (!updates.isEmpty()) {
2919 final UserHandleCompat userFinal = user;
2920 mHandler.post(new Runnable() {
2921
2922 public void run() {
2923 Callbacks cb = getCallback();
2924 if (cb != null) {
2925 cb.bindShortcutsChanged(
2926 updates, new ArrayList<ShortcutInfo>(), userFinal);
2927 }
2928 }
2929 });
2930 }
Kenny Guyed131872014-04-30 03:02:21 +01002931 }
Joe Onorato36115782010-06-17 13:28:48 -04002932
Kenny Guyed131872014-04-30 03:02:21 +01002933 // Create the ApplicationInfos
2934 for (int i = 0; i < apps.size(); i++) {
2935 LauncherActivityInfoCompat app = apps.get(i);
2936 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002937 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002938 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002939
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002940 if (!user.equals(UserHandleCompat.myUserHandle())) {
2941 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2942 if (heuristic != null) {
2943 heuristic.processUserApps(apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002944 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002945 }
Winson Chung64359a52013-07-08 17:17:08 -07002946 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002947 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002948 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2949 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002950
2951 // Post callback on main thread
2952 mHandler.post(new Runnable() {
2953 public void run() {
2954 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002955 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002956 if (callbacks != null) {
2957 callbacks.bindAllApplications(added);
2958 if (DEBUG_LOADERS) {
2959 Log.d(TAG, "bound " + added.size() + " apps in "
2960 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2961 }
2962 } else {
2963 Log.i(TAG, "not binding apps: no Launcher activity");
2964 }
2965 }
2966 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002967 // Cleanup any data stored for a deleted user.
2968 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002969
Joe Onorato36115782010-06-17 13:28:48 -04002970 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002971 Log.d(TAG, "Icons processed in "
2972 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002973 }
2974 }
2975
2976 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002977 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002978 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2979 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2980 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2981 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2982 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2983 }
Joe Onorato36115782010-06-17 13:28:48 -04002984 }
2985 }
2986
2987 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002988 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002989 }
2990
Adam Cohen091440a2015-03-18 14:16:05 -07002991 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002992
2993 @Override
2994 public void onReceive(Context context, Intent intent) {
2995 synchronized (sBgLock) {
2996 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2997 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002998 final PackageManager manager = context.getPackageManager();
2999 final ArrayList<String> packagesRemoved = new ArrayList<String>();
3000 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003001 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
3002 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003003 packagesRemoved.clear();
3004 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003005 for (String pkg : entry.getValue()) {
3006 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003007 boolean packageOnSdcard = launcherApps.isAppEnabled(
3008 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
3009 if (packageOnSdcard) {
3010 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
3011 packagesUnavailable.add(pkg);
3012 } else {
3013 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
3014 packagesRemoved.add(pkg);
3015 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003016 }
3017 }
3018 if (!packagesRemoved.isEmpty()) {
3019 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
3020 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3021 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003022 if (!packagesUnavailable.isEmpty()) {
3023 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
3024 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3025 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003026 }
Sunny Goyal34942622014-08-29 17:20:55 -07003027 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003028 }
3029 }
3030 }
3031
Joe Onorato36115782010-06-17 13:28:48 -04003032 private class PackageUpdatedTask implements Runnable {
3033 int mOp;
3034 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003035 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003036
3037 public static final int OP_NONE = 0;
3038 public static final int OP_ADD = 1;
3039 public static final int OP_UPDATE = 2;
3040 public static final int OP_REMOVE = 3; // uninstlled
3041 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3042
3043
Kenny Guyed131872014-04-30 03:02:21 +01003044 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003045 mOp = op;
3046 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003047 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
3049
3050 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003051 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003052
3053 final String[] packages = mPackages;
3054 final int N = packages.length;
3055 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003056 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003057 for (int i=0; i<N; i++) {
3058 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003059 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003060 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003062
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003063 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3064 if (heuristic != null) {
3065 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003066 }
Joe Onorato36115782010-06-17 13:28:48 -04003067 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003068 }
Joe Onorato36115782010-06-17 13:28:48 -04003069 case OP_UPDATE:
3070 for (int i=0; i<N; i++) {
3071 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003072 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003073 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003074 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003075 }
3076 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003077 case OP_REMOVE: {
3078 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3079 if (heuristic != null) {
3080 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003081 }
Joe Onorato36115782010-06-17 13:28:48 -04003082 for (int i=0; i<N; i++) {
3083 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003084 mIconCache.removeIconsForPkg(packages[i], mUser);
3085 }
3086 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003087 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003088 case OP_UNAVAILABLE:
3089 for (int i=0; i<N; i++) {
3090 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3091 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003092 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003093 }
3094 break;
3095 }
3096
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003097 ArrayList<AppInfo> added = null;
3098 ArrayList<AppInfo> modified = null;
3099 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003100
Adam Cohen487f7dd2012-06-28 18:12:10 -07003101 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003102 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003103 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003104 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003105 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003106 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003107 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003108 }
Winson Chung5d55f332012-07-16 20:45:03 -07003109 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003110 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003111 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003112 }
3113
Sunny Goyale0f58d72014-11-10 18:05:31 -08003114 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003115 if (callbacks == null) {
3116 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3117 return;
3118 }
3119
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3121 new HashMap<ComponentName, AppInfo>();
3122
Joe Onorato36115782010-06-17 13:28:48 -04003123 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003124 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 for (AppInfo ai : added) {
3126 addedOrUpdatedApps.put(ai.componentName, ai);
3127 }
Joe Onorato36115782010-06-17 13:28:48 -04003128 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003129
Joe Onorato36115782010-06-17 13:28:48 -04003130 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003131 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 for (AppInfo ai : modified) {
3133 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003134 }
3135
Joe Onorato36115782010-06-17 13:28:48 -04003136 mHandler.post(new Runnable() {
3137 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003138 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003139 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003140 callbacks.bindAppsUpdated(modifiedFinal);
3141 }
3142 }
3143 });
3144 }
Winson Chung83892cc2013-05-01 16:53:33 -07003145
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003147 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3149 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3150 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3151
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003152 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003153 synchronized (sBgLock) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003154 for (ItemInfo info : sBgItemsIdMap.values()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003155 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3156 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 boolean infoUpdated = false;
3158 boolean shortcutUpdated = false;
3159
3160 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003161 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003162 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3164 si.iconResource.resourceName, mIconCache, context);
3165 if (icon != null) {
3166 si.setIcon(icon);
3167 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003169 }
3170 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171
3172 ComponentName cn = si.getTargetComponent();
3173 if (cn != null && packageSet.contains(cn.getPackageName())) {
3174 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3175
3176 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3178 // Auto install icon
3179 PackageManager pm = context.getPackageManager();
3180 ResolveInfo matched = pm.resolveActivity(
3181 new Intent(Intent.ACTION_MAIN)
3182 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3183 PackageManager.MATCH_DEFAULT_ONLY);
3184 if (matched == null) {
3185 // Try to find the best match activity.
3186 Intent intent = pm.getLaunchIntentForPackage(
3187 cn.getPackageName());
3188 if (intent != null) {
3189 cn = intent.getComponent();
3190 appInfo = addedOrUpdatedApps.get(cn);
3191 }
3192
3193 if ((intent == null) || (appInfo == null)) {
3194 removedShortcuts.add(si);
3195 continue;
3196 }
3197 si.promisedIntent = intent;
3198 }
3199 }
3200
3201 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003202 if (appInfo != null) {
3203 si.flags = appInfo.flags;
3204 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205
Sunny Goyal756adbc2015-04-16 15:20:51 -07003206 si.intent = si.promisedIntent;
3207 si.promisedIntent = null;
3208 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 infoUpdated = true;
3210 si.updateIcon(mIconCache);
3211 }
3212
3213 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3214 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3215 si.updateIcon(mIconCache);
3216 si.title = appInfo.title.toString();
3217 si.contentDescription = appInfo.contentDescription;
3218 infoUpdated = true;
3219 }
3220
3221 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3222 // Since package was just updated, the target must be available now.
3223 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3224 shortcutUpdated = true;
3225 }
3226 }
3227
3228 if (infoUpdated || shortcutUpdated) {
3229 updatedShortcuts.add(si);
3230 }
3231 if (infoUpdated) {
3232 updateItemInDatabase(context, si);
3233 }
3234 } else if (info instanceof LauncherAppWidgetInfo) {
3235 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3236 if (mUser.equals(widgetInfo.user)
3237 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3238 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3239 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3240 widgets.add(widgetInfo);
3241 updateItemInDatabase(context, widgetInfo);
3242 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003243 }
3244 }
3245 }
3246
Sunny Goyal4390ace2014-10-13 11:33:11 -07003247 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3248 mHandler.post(new Runnable() {
3249
3250 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003251 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003252 if (callbacks == cb && cb != null) {
3253 callbacks.bindShortcutsChanged(
3254 updatedShortcuts, removedShortcuts, mUser);
3255 }
3256 }
3257 });
3258 if (!removedShortcuts.isEmpty()) {
3259 deleteItemsFromDatabase(context, removedShortcuts);
3260 }
3261 }
3262 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003263 mHandler.post(new Runnable() {
3264 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003265 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003266 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003267 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003268 }
3269 }
3270 });
3271 }
3272 }
3273
Winson Chungdf95eb12013-10-16 14:57:07 -07003274 final ArrayList<String> removedPackageNames =
3275 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003276 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003277 // Mark all packages in the broadcast to be removed
3278 removedPackageNames.addAll(Arrays.asList(packages));
3279 } else if (mOp == OP_UPDATE) {
3280 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003281 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003282 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003283 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003284 }
3285 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003286 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003287
Winson Chungdf95eb12013-10-16 14:57:07 -07003288 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003289 final int removeReason;
3290 if (mOp == OP_UNAVAILABLE) {
3291 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3292 } else {
3293 // Remove all the components associated with this package
3294 for (String pn : removedPackageNames) {
3295 deletePackageFromDatabase(context, pn, mUser);
3296 }
3297 // Remove all the specific components
3298 for (AppInfo a : removedApps) {
3299 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3300 deleteItemsFromDatabase(context, infos);
3301 }
3302 removeReason = 0;
3303 }
3304
Winson Chungdf95eb12013-10-16 14:57:07 -07003305 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003306 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003307 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003308 mHandler.post(new Runnable() {
3309 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003310 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003311 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003312 callbacks.bindComponentsRemoved(
3313 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003314 }
3315 }
3316 });
Joe Onoratobe386092009-11-17 17:32:16 -08003317 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003318 if (Build.VERSION.SDK_INT < 17) {
3319 loadAndBindWidgetsAndShortcuts(context, callbacks);
3320 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003321 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003322 mHandler.post(new Runnable() {
3323 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003324 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003325 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003326 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003327 }
3328 }
3329 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 }
3331 }
3332
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003333 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3334 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003335 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003336 if (sBgWidgetProviders == null || refresh) {
3337 sBgWidgetProviders = new HashMap<>();
3338 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3339 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003340
Robin Lee26ace122015-03-16 19:41:43 +00003341 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3342 for (AppWidgetProviderInfo pInfo : widgets) {
3343 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3344 UserHandleCompat user = wm.getUser(info);
3345 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3346 }
3347
3348 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3349 for (CustomAppWidget widget : customWidgets) {
3350 info = new LauncherAppWidgetProviderInfo(context, widget);
3351 UserHandleCompat user = wm.getUser(info);
3352 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3353 }
Adam Cohen59400422014-03-05 18:07:04 -08003354 }
Adam Cohen59400422014-03-05 18:07:04 -08003355 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3356 }
3357 }
3358
Robin Lee26ace122015-03-16 19:41:43 +00003359 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3360 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003361 synchronized (sBgLock) {
3362 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003363 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003364 }
Robin Lee26ace122015-03-16 19:41:43 +00003365 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003366 }
3367 }
3368
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003369 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks) {
3370 runOnWorkerThread(new Runnable(){
3371 @Override
3372 public void run() {
3373 final ArrayList<Object> list =
3374 getSortedWidgetsAndShortcuts(context, true /* refresh */);
3375 mHandler.post(new Runnable() {
3376 @Override
3377 public void run() {
3378 Callbacks cb = getCallback();
3379 if (callbacks == cb && cb != null) {
3380 callbacks.bindPackagesUpdated(list);
3381 }
3382 }
3383 });
3384 }
3385 });
3386 }
3387
Winson Chungb745afb2015-03-02 11:51:23 -08003388 // Returns a list of ResolveInfos/AppWidgetInfos in sorted order
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003389 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003390 PackageManager packageManager = context.getPackageManager();
3391 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003392 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003393 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3394 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003395 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003396 return widgetsAndShortcuts;
3397 }
3398
Adam Cohen091440a2015-03-18 14:16:05 -07003399 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003400 UserHandleCompat user) {
3401 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3402 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003403 }
Adam Cohen556f6132014-01-15 15:18:08 -08003404
Kenny Guyed131872014-04-30 03:02:21 +01003405 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3406 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003407 if (cn == null) {
3408 return false;
3409 }
Kenny Guyed131872014-04-30 03:02:21 +01003410 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3411 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003412 return false;
3413 }
Kenny Guyed131872014-04-30 03:02:21 +01003414 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003415 }
3416
Adam Cohena28b78e2014-05-20 17:03:04 -07003417 public static boolean isValidPackage(Context context, String packageName,
3418 UserHandleCompat user) {
3419 if (packageName == null) {
3420 return false;
3421 }
3422 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3423 return launcherApps.isPackageEnabledForProfile(packageName, user);
3424 }
3425
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003427 * Make an ShortcutInfo object for a restored application or shortcut item that points
3428 * to a package that is not yet installed on the system.
3429 */
Sunny Goyal34942622014-08-29 17:20:55 -07003430 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003431 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003432 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003433 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003434 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003435
3436 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3437 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3438 if (!TextUtils.isEmpty(title)) {
3439 info.title = title;
3440 }
Sunny Goyal34942622014-08-29 17:20:55 -07003441 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3442 if (TextUtils.isEmpty(info.title)) {
3443 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3444 }
Sunny Goyal34942622014-08-29 17:20:55 -07003445 } else {
3446 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3447 }
3448
Kenny Guyc2bd8102014-06-30 12:30:31 +01003449 info.contentDescription = mUserManager.getBadgedLabelForUser(
3450 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003451 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003452 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003453 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003454 return info;
3455 }
3456
3457 /**
3458 * Make an Intent object for a restored application or shortcut item that points
3459 * to the market page for the item.
3460 */
Adam Cohen091440a2015-03-18 14:16:05 -07003461 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003462 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003463 return getMarketIntent(componentName.getPackageName());
3464 }
3465
3466 static Intent getMarketIntent(String packageName) {
3467 return new Intent(Intent.ACTION_VIEW)
3468 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003469 .scheme("market")
3470 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003471 .appendQueryParameter("id", packageName)
3472 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003473 }
3474
3475 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003476 * Make an ShortcutInfo object for a shortcut that is an application.
3477 *
3478 * If c is not null, then it will be used to fill in missing data like the title and icon.
3479 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003480 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003481 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003482 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003483 if (user == null) {
3484 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003485 return null;
3486 }
3487
Kenny Guyed131872014-04-30 03:02:21 +01003488 ComponentName componentName = intent.getComponent();
3489 if (componentName == null) {
3490 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3491 return null;
3492 }
3493
3494 Intent newIntent = new Intent(intent.getAction(), null);
3495 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3496 newIntent.setComponent(componentName);
3497 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003498 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003499 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3500 return null;
3501 }
3502
3503 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003504 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003505 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3506 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3507 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003508 }
3509
Joe Onorato56d82912010-03-07 14:32:10 -05003510 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003511 if (TextUtils.isEmpty(info.title) && c != null) {
3512 info.title = c.getString(titleIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003513 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003514
Joe Onorato56d82912010-03-07 14:32:10 -05003515 // fall back to the class name of the activity
3516 if (info.title == null) {
3517 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003518 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003519
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003521 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003522 info.contentDescription = mUserManager.getBadgedLabelForUser(
3523 info.title.toString(), info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003524 if (lai != null) {
3525 info.flags = AppInfo.initFlags(lai);
3526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003529
Winson Chung64359a52013-07-08 17:17:08 -07003530 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3531 ItemInfoFilter f) {
3532 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3533 for (ItemInfo i : infos) {
3534 if (i instanceof ShortcutInfo) {
3535 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003536 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003537 if (cn != null && f.filterItem(null, info, cn)) {
3538 filtered.add(info);
3539 }
3540 } else if (i instanceof FolderInfo) {
3541 FolderInfo info = (FolderInfo) i;
3542 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003543 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003544 if (cn != null && f.filterItem(info, s, cn)) {
3545 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003546 }
3547 }
Winson Chung64359a52013-07-08 17:17:08 -07003548 } else if (i instanceof LauncherAppWidgetInfo) {
3549 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3550 ComponentName cn = info.providerName;
3551 if (cn != null && f.filterItem(null, info, cn)) {
3552 filtered.add(info);
3553 }
Winson Chung8a435102012-08-30 17:16:53 -07003554 }
3555 }
Winson Chung64359a52013-07-08 17:17:08 -07003556 return new ArrayList<ItemInfo>(filtered);
3557 }
3558
Adam Cohen091440a2015-03-18 14:16:05 -07003559 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003560 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003561 ItemInfoFilter filter = new ItemInfoFilter() {
3562 @Override
3563 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003564 if (info.user == null) {
3565 return cn.equals(cname);
3566 } else {
3567 return cn.equals(cname) && info.user.equals(user);
3568 }
Winson Chung64359a52013-07-08 17:17:08 -07003569 }
3570 };
3571 return filterItemInfos(sBgItemsIdMap.values(), filter);
3572 }
3573
Sunny Goyal1a745e82014-10-02 15:58:31 -07003574 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003575 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003576 */
Adam Cohen091440a2015-03-18 14:16:05 -07003577 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003578 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3579 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580
Joe Onorato56d82912010-03-07 14:32:10 -05003581 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003582 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003583 // Non-app shortcuts are only supported for current user.
3584 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003586
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003587 // TODO: If there's an explicit component and we can't install that, delete it.
3588
Joe Onorato56d82912010-03-07 14:32:10 -05003589 info.title = c.getString(titleIndex);
3590
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 int iconType = c.getInt(iconTypeIndex);
3592 switch (iconType) {
3593 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3594 String packageName = c.getString(iconPackageIndex);
3595 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003596 info.customIcon = false;
3597 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003598 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003599 // the db
3600 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003601 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003602 }
3603 // the fallback icon
3604 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003605 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003606 info.usingFallbackIcon = true;
3607 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 break;
3609 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003610 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003611 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003612 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003613 info.customIcon = false;
3614 info.usingFallbackIcon = true;
3615 } else {
3616 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 break;
3619 default:
Kenny Guyed131872014-04-30 03:02:21 +01003620 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003621 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 info.customIcon = false;
3623 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003624 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003625 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 return info;
3627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003628
Sunny Goyal2350bc92014-10-14 16:42:54 -07003629 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003630 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3631 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3632 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3633
Adam Cohend9198822011-11-22 16:42:47 -08003634 if (intent == null) {
3635 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3636 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3637 return null;
3638 }
3639
Joe Onorato0589f0f2010-02-08 13:44:00 -08003640 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003641 boolean customIcon = false;
3642 ShortcutIconResource iconResource = null;
3643
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003644 if (bitmap instanceof Bitmap) {
3645 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003646 customIcon = true;
3647 } else {
3648 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003649 if (extra instanceof ShortcutIconResource) {
3650 iconResource = (ShortcutIconResource) extra;
3651 icon = Utilities.createIconBitmap(iconResource.packageName,
3652 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003653 }
3654 }
3655
Michael Jurkac9d95c52011-08-29 14:03:34 -07003656 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003657
Kenny Guyed131872014-04-30 03:02:21 +01003658 // Only support intents for current user for now. Intents sent from other
3659 // users wouldn't get here without intent forwarding anyway.
3660 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003661 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003662 icon = mIconCache.getDefaultIcon(info.user);
3663 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003664 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003665 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003666
Joe Onorato0589f0f2010-02-08 13:44:00 -08003667 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003668 info.contentDescription = mUserManager.getBadgedLabelForUser(
3669 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003670 info.intent = intent;
3671 info.customIcon = customIcon;
3672 info.iconResource = iconResource;
3673
3674 return info;
3675 }
3676
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003678 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 * or make a new one.
3680 */
Adam Cohen091440a2015-03-18 14:16:05 -07003681 @Thunk static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 // See if a placeholder was created for us already
3683 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003684 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003686 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 folders.put(id, folderInfo);
3688 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003689 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690 }
3691
Winson Chung1ed747a2011-05-03 16:18:34 -07003692 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003693 private final AppWidgetManagerCompat mManager;
3694 private final PackageManager mPackageManager;
3695 private final HashMap<Object, String> mLabelCache;
3696 private final Collator mCollator;
3697
Hyunyoung Song3f471442015-04-08 19:01:34 -07003698 public WidgetAndShortcutNameComparator(Context context) {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003699 mManager = AppWidgetManagerCompat.getInstance(context);
3700 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003701 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003702 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003703 }
3704 public final int compare(Object a, Object b) {
3705 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003706 if (mLabelCache.containsKey(a)) {
3707 labelA = mLabelCache.get(a);
3708 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003709 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3710 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003711 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003712 mLabelCache.put(a, labelA);
3713 }
3714 if (mLabelCache.containsKey(b)) {
3715 labelB = mLabelCache.get(b);
3716 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003717 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c165aa2015-01-30 10:28:23 -08003718 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003719 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003720 mLabelCache.put(b, labelB);
3721 }
Winson Chung11904872012-09-17 16:58:46 -07003722 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003723 }
3724 };
Joe Onoratobe386092009-11-17 17:32:16 -08003725
Sunny Goyal651077b2014-06-30 14:15:31 -07003726 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3727 return (provider != null) && (provider.provider != null)
3728 && (provider.provider.getPackageName() != null);
3729 }
3730
Joe Onoratobe386092009-11-17 17:32:16 -08003731 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003732 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003733 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3734 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3735 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3736 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003737 if (mLoaderTask != null) {
3738 mLoaderTask.dumpState();
3739 } else {
3740 Log.d(TAG, "mLoaderTask=null");
3741 }
Joe Onoratobe386092009-11-17 17:32:16 -08003742 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003743
3744 public Callbacks getCallback() {
3745 return mCallbacks != null ? mCallbacks.get() : null;
3746 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003747
3748 /**
3749 * @return {@link FolderInfo} if its already loaded.
3750 */
3751 public FolderInfo findFolderById(Long folderId) {
3752 synchronized (sBgLock) {
3753 return sBgFolders.get(folderId);
3754 }
3755 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003756
3757 /**
3758 * @return the looper for the worker thread which can be used to start background tasks.
3759 */
3760 public static Looper getWorkerLooper() {
3761 return sWorkerThread.getLooper();
3762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763}