blob: 5a65cab82a6b240c5e4c51718e884a9db3a5729b [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;
31import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040033import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070041import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080042import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040043import android.os.Handler;
44import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070045import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080046import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040049import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040050import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080051import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080053import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070054import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010055
Sunny Goyalffe83f12014-08-14 17:39:34 -070056import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.LauncherActivityInfoCompat;
58import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070059import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070060import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010061import com.android.launcher3.compat.UserHandleCompat;
62import com.android.launcher3.compat.UserManagerCompat;
Robin Lee26ace122015-03-16 19:41:43 +000063import com.android.launcher3.util.ComponentKey;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.text.Collator;
71import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070072import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070073import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.Collections;
75import java.util.Comparator;
76import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070078import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070080import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * Maintains in-memory state of the Launcher. It is expected that there should be only one
85 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070086 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 */
Kenny Guyed131872014-04-30 03:02:21 +010088public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010089 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040091 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070092 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070095
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Winson Chung81b52252012-08-27 15:34:29 -0700112 // Specific runnable types that are run on the main thread deferred handler, this allows us to
113 // clear all queued binding runnables when the Launcher activity is destroyed.
114 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
115 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
116
Jason Monkbbe1e242014-05-16 17:37:34 -0400117 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk boolean mWorkspaceLoaded;
129 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700130
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700131 // When we are loading pages synchronously, we can't just post the binding of items on the side
132 // pages as this delays the rotation process. Instead, we wait for a callback from the first
133 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
134 // a normal load, we also clear this set of Runnables.
135 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
162 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700170 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700171 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
172 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700174 // </ only access in worker thread >
175
Adam Cohen091440a2015-03-18 14:16:05 -0700176 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Reena Lee99a73f32011-10-24 17:27:37 -0700178 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk final LauncherAppsCompat mLauncherApps;
181 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700184 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public int getCurrentWorkspaceScreen();
186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700190 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500191 public void bindFolders(HashMap<Long,FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800192 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700195 public void bindAppsAdded(ArrayList<Long> newScreens,
196 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700197 ArrayList<ItemInfo> addAnimated,
198 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
201 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700204 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700205 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Michael Jurkac402cd92013-05-20 15:49:32 +0200206 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100207 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700208 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700210 public void dumpLogsToLocalData();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800211 public void bindAddPendingItem(PendingAddItemInfo info, long container, long screenId,
212 int[] cell, int spanX, int spanY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800219 public interface ScreenPosProvider {
220 int getScreenIndex(ArrayList<Long> screenIDs);
221 }
222
Bjorn Bringert1307f632013-10-03 22:31:03 +0100223 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800224 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400225
Winson Chungee055712013-07-30 14:46:24 -0700226 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700227 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400228 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
229 // resource string.
230 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
231 ProviderInfo providerInfo =
232 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
233 ProviderInfo redirectProvider =
234 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700235
236 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400237 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700238
239 if (mOldContentProviderExists) {
240 Log.d(TAG, "Old launcher provider exists.");
241 } else {
242 Log.d(TAG, "Old launcher provider does not exist.");
243 }
244
Daniel Sandlere4f98912013-06-25 15:13:26 -0400245 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100246 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 mIconCache = iconCache;
248
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400249 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700250 Configuration config = res.getConfiguration();
251 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100252 mLauncherApps = LauncherAppsCompat.getInstance(context);
253 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 }
255
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
257 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700258 @Thunk void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700259 runOnMainThread(r, 0);
260 }
Adam Cohen091440a2015-03-18 14:16:05 -0700261 @Thunk void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700262 if (sWorkerThread.getThreadId() == Process.myTid()) {
263 // If we are on the worker thread, post onto the main handler
264 mHandler.post(r);
265 } else {
266 r.run();
267 }
268 }
269
270 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
271 * posted on the worker thread handler. */
272 private static void runOnWorkerThread(Runnable r) {
273 if (sWorkerThread.getThreadId() == Process.myTid()) {
274 r.run();
275 } else {
276 // If we are not on the worker thread, then post to the worker handler
277 sWorker.post(r);
278 }
279 }
280
Winson Chunge43a1e72014-01-15 10:33:02 -0800281 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
282 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800283 }
284
Sunny Goyal756adbc2015-04-16 15:20:51 -0700285 public void setPackageState(final PackageInstallInfo installInfo) {
286 Runnable updateRunnable = new Runnable() {
287
288 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500289 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 synchronized (sBgLock) {
291 final HashSet<ItemInfo> updates = new HashSet<>();
292
293 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
294 // Ignore install success events as they are handled by Package add events.
295 return;
296 }
297
298 for (ItemInfo info : sBgItemsIdMap.values()) {
299 if (info instanceof ShortcutInfo) {
300 ShortcutInfo si = (ShortcutInfo) info;
301 ComponentName cn = si.getTargetComponent();
302 if (si.isPromise() && (cn != null)
303 && installInfo.packageName.equals(cn.getPackageName())) {
304 si.setInstallProgress(installInfo.progress);
305
306 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
307 // Mark this info as broken.
308 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
309 }
310 updates.add(si);
311 }
312 }
313 }
314
315 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
316 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
317 widget.installProgress = installInfo.progress;
318 updates.add(widget);
319 }
320 }
321
322 if (!updates.isEmpty()) {
323 // Push changes to the callback.
324 Runnable r = new Runnable() {
325 public void run() {
326 Callbacks callbacks = getCallback();
327 if (callbacks != null) {
328 callbacks.bindRestoreItemsChange(updates);
329 }
330 }
331 };
332 mHandler.post(r);
333 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500334 }
335 }
336 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500338 }
339
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 /**
341 * Updates the icons and label of all pending icons for the provided package name.
342 */
343 public void updateSessionDisplayInfo(final String packageName) {
344 Runnable updateRunnable = new Runnable() {
345
346 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700347 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700348 synchronized (sBgLock) {
349 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
350 final UserHandleCompat user = UserHandleCompat.myUserHandle();
351
352 for (ItemInfo info : sBgItemsIdMap.values()) {
353 if (info instanceof ShortcutInfo) {
354 ShortcutInfo si = (ShortcutInfo) info;
355 ComponentName cn = si.getTargetComponent();
356 if (si.isPromise() && (cn != null)
357 && packageName.equals(cn.getPackageName())) {
358 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
359 // For auto install apps update the icon as well as label.
360 mIconCache.getTitleAndIcon(si,
361 si.promisedIntent, user,
362 si.shouldUseLowResIcon());
363 } else {
364 // Only update the icon for restored apps.
365 si.updateIcon(mIconCache);
366 }
367 updates.add(si);
368 }
369 }
370 }
371
372 if (!updates.isEmpty()) {
373 // Push changes to the callback.
374 Runnable r = new Runnable() {
375 public void run() {
376 Callbacks callbacks = getCallback();
377 if (callbacks != null) {
378 callbacks.bindShortcutsChanged(updates,
379 new ArrayList<ShortcutInfo>(), user);
380 }
381 }
382 };
383 mHandler.post(r);
384 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700385 }
386 }
387 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700388 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700389 }
390
Adam Cohen76a47a12014-02-05 11:47:43 -0800391 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800392 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800393
394 if (allAppsApps == null) {
395 throw new RuntimeException("allAppsApps must not be null");
396 }
397 if (allAppsApps.isEmpty()) {
398 return;
399 }
400
401 // Process the newly added applications and add them to the database first
402 Runnable r = new Runnable() {
403 public void run() {
404 runOnMainThread(new Runnable() {
405 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800406 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800407 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500408 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800409 }
410 }
411 });
412 }
413 };
414 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700415 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800416
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700417 public void addAndBindAddedWorkspaceItems(final Context context,
Adam Cohen76a47a12014-02-05 11:47:43 -0800418 final ArrayList<ItemInfo> workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700419 addAndBindAddedWorkspaceItems(context, workspaceApps,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 new ScreenPosProvider() {
Adam Cohen76a47a12014-02-05 11:47:43 -0800421
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 @Override
423 public int getScreenIndex(ArrayList<Long> screenIDs) {
424 return screenIDs.isEmpty() ? 0 : 1;
425 }
426 }, 1, false);
427 }
428
429 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<Rect> occupiedPos,
430 int[] xy, int spanX, int spanY) {
431 LauncherAppState app = LauncherAppState.getInstance();
432 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
433 final int xCount = (int) grid.numColumns;
434 final int yCount = (int) grid.numRows;
435 boolean[][] occupied = new boolean[xCount][yCount];
436 if (occupiedPos != null) {
437 for (Rect r : occupiedPos) {
438 for (int x = r.left; 0 <= x && x < r.right && x < xCount; x++) {
439 for (int y = r.top; 0 <= y && y < r.bottom && y < yCount; y++) {
440 occupied[x][y] = true;
441 }
442 }
443 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800444 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 return CellLayout.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
446 }
447
448 /**
449 * Find a position on the screen for the given size or adds a new screen.
450 * @return screenId and the coordinates for the item.
451 */
Adam Cohen091440a2015-03-18 14:16:05 -0700452 @Thunk static Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 Context context,
454 ScreenPosProvider preferredScreen,
455 int fallbackStartScreen,
456 ArrayList<Long> workspaceScreens,
457 ArrayList<Long> addedWorkspaceScreensFinal,
458 int spanX, int spanY) {
459 // Load position of items which are on the desktop. We can't use sBgItemsIdMap because
460 // loadWorkspace() may not have been called.
461 final ContentResolver cr = context.getContentResolver();
462 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
463 new String[] {
464 LauncherSettings.Favorites.SCREEN,
465 LauncherSettings.Favorites.CELLX,
466 LauncherSettings.Favorites.CELLY,
467 LauncherSettings.Favorites.SPANX,
468 LauncherSettings.Favorites.SPANY,
469 LauncherSettings.Favorites.CONTAINER
470 },
471 "container=?",
472 new String[] { Integer.toString(LauncherSettings.Favorites.CONTAINER_DESKTOP) },
473 null);
474
475 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
476 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
477 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
478 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
479 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
480 LongSparseArray<ArrayList<Rect>> screenItems = new LongSparseArray<ArrayList<Rect>>();
481 try {
482 while (c.moveToNext()) {
483 Rect rect = new Rect();
484 rect.left = c.getInt(cellXIndex);
485 rect.top = c.getInt(cellYIndex);
486 rect.right = rect.left + Math.max(1, c.getInt(spanXIndex));
487 rect.bottom = rect.top + Math.max(1, c.getInt(spanYIndex));
488
489 long screenId = c.getInt(screenIndex);
490 ArrayList<Rect> items = screenItems.get(screenId);
491 if (items == null) {
492 items = new ArrayList<Rect>();
493 screenItems.put(screenId, items);
494 }
495 items.add(rect);
496 }
497 } catch (Exception e) {
498 screenItems.clear();
499 } finally {
500 c.close();
501 }
502
503 // Find appropriate space for the item.
504 long screenId = 0;
505 int[] cordinates = new int[2];
506 boolean found = false;
507
508 int screenCount = workspaceScreens.size();
509 // First check the preferred screen.
510 int preferredScreenIndex = preferredScreen.getScreenIndex(workspaceScreens);
511 if (preferredScreenIndex < screenCount) {
512 screenId = workspaceScreens.get(preferredScreenIndex);
513 found = findNextAvailableIconSpaceInScreen(
514 screenItems.get(screenId), cordinates, spanX, spanY);
515 }
516
517 if (!found) {
518 // Search on any of the screens.
519 for (int screen = fallbackStartScreen; screen < screenCount; screen++) {
520 screenId = workspaceScreens.get(screen);
521 if (findNextAvailableIconSpaceInScreen(
522 screenItems.get(screenId), cordinates, spanX, spanY)) {
523 // We found a space for it
524 found = true;
525 break;
526 }
527 }
528 }
529
530 if (!found) {
531 // Still no position found. Add a new screen to the end.
532 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
533
534 // Save the screen id for binding in the workspace
535 workspaceScreens.add(screenId);
536 addedWorkspaceScreensFinal.add(screenId);
537
538 // If we still can't find an empty space, then God help us all!!!
539 if (!findNextAvailableIconSpaceInScreen(
540 screenItems.get(screenId), cordinates, spanX, spanY)) {
541 throw new RuntimeException("Can't find space to add the item");
542 }
543 }
544 return Pair.create(screenId, cordinates);
545 }
546
547 /**
548 * Adds the provided items to the workspace.
549 * @param preferredScreen the screen where we should try to add the app first
550 * @param fallbackStartScreen the screen to start search for empty space if
551 * preferredScreen is not available.
552 */
553 public void addAndBindPendingItem(
554 final Context context,
555 final PendingAddItemInfo addInfo,
556 final ScreenPosProvider preferredScreen,
557 final int fallbackStartScreen) {
558 final Callbacks callbacks = getCallback();
559 // Process the newly added applications and add them to the database first
560 Runnable r = new Runnable() {
561 public void run() {
562 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800563 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800564
565 // Find appropriate space for the item.
566 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
567 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
568 addInfo.spanX,
569 addInfo.spanY);
570 final long screenId = coords.first;
571 final int[] cordinates = coords.second;
572
573 // Update the workspace screens
574 updateWorkspaceScreenOrder(context, workspaceScreens);
575 runOnMainThread(new Runnable() {
576 public void run() {
577 Callbacks cb = getCallback();
578 if (callbacks == cb && cb != null) {
579 cb.bindAddScreens(addedWorkspaceScreensFinal);
580 cb.bindAddPendingItem(addInfo,
581 LauncherSettings.Favorites.CONTAINER_DESKTOP,
582 screenId, cordinates, addInfo.spanX, addInfo.spanY);
583 }
584 }
585 });
586 }
587 };
588 runOnWorkerThread(r);
589 }
590
591 /**
592 * Adds the provided items to the workspace.
593 * @param preferredScreen the screen where we should try to add the app first
594 * @param fallbackStartScreen the screen to start search for empty space if
595 * preferredScreen is not available.
596 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700597 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800598 final ArrayList<ItemInfo> workspaceApps,
599 final ScreenPosProvider preferredScreen,
600 final int fallbackStartScreen,
601 final boolean allowDuplicate) {
602 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800603 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700604 return;
Winson Chung997a9232013-07-24 15:33:46 -0700605 }
Winson Chung64359a52013-07-08 17:17:08 -0700606 // Process the newly added applications and add them to the database first
607 Runnable r = new Runnable() {
608 public void run() {
609 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
610 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
611
Winson Chung76828c82013-08-19 15:43:29 -0700612 // Get the list of workspace screens. We need to append to this list and
613 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
614 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800615 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700616 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800617 for (ItemInfo item : workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700618 if (!allowDuplicate && item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800619 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700620 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800621 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700622 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800623 }
Winson Chung76828c82013-08-19 15:43:29 -0700624
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800625 // Find appropriate space for the item.
626 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
627 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
628 1, 1);
629 long screenId = coords.first;
630 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700631
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700632 ItemInfo itemInfo;
633 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
634 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800635 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700636 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700637 } else {
638 throw new RuntimeException("Unexpected info type");
639 }
Winson Chung94d67682013-09-25 16:29:40 -0700640
Winson Chung64359a52013-07-08 17:17:08 -0700641 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700642 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700643 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700644 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700645 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700646 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700647 }
648 }
649
Winson Chung76828c82013-08-19 15:43:29 -0700650 // Update the workspace screens
651 updateWorkspaceScreenOrder(context, workspaceScreens);
652
Adam Cohen76a47a12014-02-05 11:47:43 -0800653 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700654 runOnMainThread(new Runnable() {
655 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800656 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700657 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700658 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
659 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700660 if (!addedShortcutsFinal.isEmpty()) {
661 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
662 long lastScreenId = info.screenId;
663 for (ItemInfo i : addedShortcutsFinal) {
664 if (i.screenId == lastScreenId) {
665 addAnimated.add(i);
666 } else {
667 addNotAnimated.add(i);
668 }
Winson Chung997a9232013-07-24 15:33:46 -0700669 }
670 }
Winson Chungd64d1762013-08-20 14:37:16 -0700671 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800672 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700673 }
Winson Chung64359a52013-07-08 17:17:08 -0700674 }
Winson Chung997a9232013-07-24 15:33:46 -0700675 });
676 }
Winson Chung64359a52013-07-08 17:17:08 -0700677 }
678 };
679 runOnWorkerThread(r);
680 }
681
Winson Chung81b52252012-08-27 15:34:29 -0700682 public void unbindItemInfosAndClearQueuedBindRunnables() {
683 if (sWorkerThread.getThreadId() == Process.myTid()) {
684 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
685 "main thread");
686 }
687
688 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400689 synchronized (mDeferredBindRunnables) {
690 mDeferredBindRunnables.clear();
691 }
Winson Chung81b52252012-08-27 15:34:29 -0700692 // Remove any queued bind runnables
693 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
694 // Unbind all the workspace items
695 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700696 }
697
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700698 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700699 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700700 // Ensure that we don't use the same workspace items data structure on the main thread
701 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700702 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
703 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700704 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700705 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
706 tmpAppWidgets.addAll(sBgAppWidgets);
707 }
708 Runnable r = new Runnable() {
709 @Override
710 public void run() {
711 for (ItemInfo item : tmpWorkspaceItems) {
712 item.unbind();
713 }
714 for (ItemInfo item : tmpAppWidgets) {
715 item.unbind();
716 }
717 }
718 };
719 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700720 }
721
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 /**
723 * Adds an item to the DB if it was not created previously, or move it to a new
724 * <container, screen, cellX, cellY>
725 */
726 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 if (item.container == ItemInfo.NO_ID) {
729 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700730 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 } else {
732 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735 }
736
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700737 static void checkItemInfoLocked(
738 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
739 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
740 if (modelItem != null && item != modelItem) {
741 // check all the data is consistent
742 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
743 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
744 ShortcutInfo shortcut = (ShortcutInfo) item;
745 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
746 modelShortcut.intent.filterEquals(shortcut.intent) &&
747 modelShortcut.id == shortcut.id &&
748 modelShortcut.itemType == shortcut.itemType &&
749 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700751 modelShortcut.cellX == shortcut.cellX &&
752 modelShortcut.cellY == shortcut.cellY &&
753 modelShortcut.spanX == shortcut.spanX &&
754 modelShortcut.spanY == shortcut.spanY &&
755 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
756 (modelShortcut.dropPos != null &&
757 shortcut.dropPos != null &&
758 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
759 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
760 // For all intents and purposes, this is the same object
761 return;
762 }
763 }
764
765 // the modelItem needs to match up perfectly with item if our model is
766 // to be consistent with the database-- for now, just require
767 // modelItem == item or the equality check above
768 String msg = "item: " + ((item != null) ? item.toString() : "null") +
769 "modelItem: " +
770 ((modelItem != null) ? modelItem.toString() : "null") +
771 "Error: ItemInfo passed to checkItemInfo doesn't match original";
772 RuntimeException e = new RuntimeException(msg);
773 if (stackTrace != null) {
774 e.setStackTrace(stackTrace);
775 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800776 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700777 }
778 }
779
Michael Jurka816474f2012-06-25 14:49:02 -0700780 static void checkItemInfo(final ItemInfo item) {
781 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
782 final long itemId = item.id;
783 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700784 public void run() {
785 synchronized (sBgLock) {
786 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700787 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700788 }
789 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700790 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700791 }
792
Michael Jurkac9d95c52011-08-29 14:03:34 -0700793 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
794 final ItemInfo item, final String callingFunction) {
795 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700796 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797 final ContentResolver cr = context.getContentResolver();
798
Adam Cohen487f7dd2012-06-28 18:12:10 -0700799 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800 Runnable r = new Runnable() {
801 public void run() {
802 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700803 updateItemArrays(item, itemId, stackTrace);
804 }
805 };
806 runOnWorkerThread(r);
807 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700808
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700809 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
810 final ArrayList<ItemInfo> items, final String callingFunction) {
811 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700812
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
814 Runnable r = new Runnable() {
815 public void run() {
816 ArrayList<ContentProviderOperation> ops =
817 new ArrayList<ContentProviderOperation>();
818 int count = items.size();
819 for (int i = 0; i < count; i++) {
820 ItemInfo item = items.get(i);
821 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700822 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700824
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
826 updateItemArrays(item, itemId, stackTrace);
827
828 }
829 try {
830 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
831 } catch (Exception e) {
832 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700833 }
834 }
835 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700836 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700837 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700838
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
840 // Lock on mBgLock *after* the db operation
841 synchronized (sBgLock) {
842 checkItemInfoLocked(itemId, item, stackTrace);
843
844 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
845 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
846 // Item is in a folder, make sure this folder exists
847 if (!sBgFolders.containsKey(item.container)) {
848 // An items container is being set to a that of an item which is not in
849 // the list of Folders.
850 String msg = "item: " + item + " container being set to: " +
851 item.container + ", not in the list of folders";
852 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700853 }
854 }
855
856 // Items are added/removed from the corresponding FolderInfo elsewhere, such
857 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
858 // that are on the desktop, as appropriate
859 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800860 if (modelItem != null &&
861 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
862 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700863 switch (modelItem.itemType) {
864 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
865 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
866 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
867 if (!sBgWorkspaceItems.contains(modelItem)) {
868 sBgWorkspaceItems.add(modelItem);
869 }
870 break;
871 default:
872 break;
873 }
874 } else {
875 sBgWorkspaceItems.remove(modelItem);
876 }
877 }
878 }
879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700882 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700883 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886 item.cellX = cellX;
887 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 // We store hotseat items in canonical form which is this orientation invariant position
890 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700891 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700895 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897
898 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700900 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
901 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800902 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Michael Jurkac9d95c52011-08-29 14:03:34 -0700905 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700906 }
907
908 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700909 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
910 * cellX, cellY have already been updated on the ItemInfos.
911 */
912 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
913 final long container, final int screen) {
914
915 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
916 int count = items.size();
917
918 for (int i = 0; i < count; i++) {
919 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700920 item.container = container;
921
922 // We store hotseat items in canonical form which is this orientation invariant position
923 // in the hotseat
924 if (context instanceof Launcher && screen < 0 &&
925 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700927 item.cellY);
928 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700929 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700930 }
931
932 final ContentValues values = new ContentValues();
933 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
934 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
935 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800936 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938
939 contentValues.add(values);
940 }
941 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
942 }
943
944 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700945 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800946 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700947 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700948 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700949 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800950 item.cellX = cellX;
951 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700952 item.spanX = spanX;
953 item.spanY = spanY;
954
955 // We store hotseat items in canonical form which is this orientation invariant position
956 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700958 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700960 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700962 }
Adam Cohend4844c32011-02-18 19:25:06 -0800963
Adam Cohend4844c32011-02-18 19:25:06 -0800964 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800965 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700966 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
967 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800968 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700969 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
970 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700971 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800972
Michael Jurka816474f2012-06-25 14:49:02 -0700973 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700974 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700975
976 /**
977 * Update an item to the database in a specified container.
978 */
979 static void updateItemInDatabase(Context context, final ItemInfo item) {
980 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100981 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700982 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800983 }
984
985 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700986 * Returns true if the shortcuts already exists on the workspace. This must be called after
987 * the workspace has been loaded. We identify a shortcut by its intent.
988 * TODO: Throw exception is above condition is not met.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 */
Sunny Goyal756adbc2015-04-16 15:20:51 -0700990 @Thunk static boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700991 final Intent intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700992 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700993 if (intent.getComponent() != null) {
994 // If component is not null, an intent with null package will produce
995 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700996 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700997 if (intent.getPackage() != null) {
998 intentWithPkg = intent;
999 intentWithoutPkg = new Intent(intent).setPackage(null);
1000 } else {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001001 intentWithPkg = new Intent(intent).setPackage(packageName);
Sunny Goyal2a6cf092014-06-26 15:27:14 -07001002 intentWithoutPkg = intent;
1003 }
1004 } else {
1005 intentWithPkg = intent;
1006 intentWithoutPkg = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001007 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -07001008 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001009
1010 synchronized (sBgLock) {
1011 for (ItemInfo item : sBgItemsIdMap.values()) {
1012 if (item instanceof ShortcutInfo) {
1013 ShortcutInfo info = (ShortcutInfo) item;
1014 if (intentWithPkg.equals(info.getIntent())
1015 || intentWithoutPkg.equals(info.getIntent())) {
1016 return true;
1017 }
1018 }
1019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001021 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001022 }
1023
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 /**
1025 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
1026 */
1027 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
1028 final ContentResolver cr = context.getContentResolver();
1029 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
1030 "_id=? and (itemType=? or itemType=?)",
1031 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -07001032 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001033
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 try {
1035 if (c.moveToFirst()) {
1036 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1037 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1038 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1039 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1040 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1041 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001042 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 FolderInfo folderInfo = null;
1045 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001046 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1047 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001049 }
1050
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 folderInfo.title = c.getString(titleIndex);
1052 folderInfo.id = id;
1053 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001055 folderInfo.cellX = c.getInt(cellXIndex);
1056 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001057 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058
1059 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 } finally {
1062 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001063 }
1064
1065 return null;
1066 }
1067
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 /**
1069 * Add an item to the database in a specified container. Sets the container, screen, cellX and
1070 * cellY fields of the item. Also assigns an ID to the item.
1071 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001072 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001073 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 item.cellX = cellX;
1076 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 // We store hotseat items in canonical form which is this orientation invariant position
1078 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001080 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001084 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085
1086 final ContentValues values = new ContentValues();
1087 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001088 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089
Michael Jurka414300a2013-08-27 15:42:35 +02001090 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001091 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001092
Jason Monk8e19cf22014-03-20 15:06:57 -04001093 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001094 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001095 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001096 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001099 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001100 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001101 sBgItemsIdMap.put(item.id, item);
1102 switch (item.itemType) {
1103 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1104 sBgFolders.put(item.id, (FolderInfo) item);
1105 // Fall through
1106 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1107 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1108 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1109 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1110 sBgWorkspaceItems.add(item);
1111 } else {
1112 if (!sBgFolders.containsKey(item.container)) {
1113 // Adding an item to a folder that doesn't exist.
1114 String msg = "adding item: " + item + " to a folder that " +
1115 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001116 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001118 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 break;
1120 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1121 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1122 break;
1123 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001124 }
1125 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001126 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001128 }
1129
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001131 * Creates a new unique child id, for a given cell span across all layouts.
1132 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001133 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001134 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001137 }
1138
Sunny Goyal34942622014-08-29 17:20:55 -07001139 private static ArrayList<ItemInfo> getItemsByPackageName(
1140 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001141 ItemInfoFilter filter = new ItemInfoFilter() {
1142 @Override
1143 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1144 return cn.getPackageName().equals(pn) && info.user.equals(user);
1145 }
1146 };
Sunny Goyal34942622014-08-29 17:20:55 -07001147 return filterItemInfos(sBgItemsIdMap.values(), filter);
1148 }
1149
1150 /**
1151 * Removes all the items from the database corresponding to the specified package.
1152 */
1153 static void deletePackageFromDatabase(Context context, final String pn,
1154 final UserHandleCompat user) {
1155 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001156 }
1157
1158 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001159 * Removes the specified item from the database
1160 * @param context
1161 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001163 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001164 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1165 items.add(item);
1166 deleteItemsFromDatabase(context, items);
1167 }
1168
1169 /**
1170 * Removes the specified items from the database
1171 * @param context
1172 * @param item
1173 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001174 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001176 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001177 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001178 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001179 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001180 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001182 // Lock on mBgLock *after* the db operation
1183 synchronized (sBgLock) {
1184 switch (item.itemType) {
1185 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1186 sBgFolders.remove(item.id);
1187 for (ItemInfo info: sBgItemsIdMap.values()) {
1188 if (info.container == item.id) {
1189 // We are deleting a folder which still contains items that
1190 // think they are contained by that folder.
1191 String msg = "deleting a folder (" + item + ") which still " +
1192 "contains items (" + info + ")";
1193 Log.e(TAG, msg);
1194 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001196 sBgWorkspaceItems.remove(item);
1197 break;
1198 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1199 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1200 sBgWorkspaceItems.remove(item);
1201 break;
1202 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1203 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1204 break;
1205 }
1206 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001208 }
1209 }
Michael Jurka83df1882011-08-31 20:59:26 -07001210 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001211 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
1213
1214 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001215 * Update the order of the workspace screens in the database. The array list contains
1216 * a list of screen ids in the order that they should appear.
1217 */
Winson Chungc9168342013-06-26 14:54:55 -07001218 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001219 // Log to disk
1220 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1221 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1222
Winson Chung64359a52013-07-08 17:17:08 -07001223 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001224 final ContentResolver cr = context.getContentResolver();
1225 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1226
1227 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001228 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001229 while (iter.hasNext()) {
1230 long id = iter.next();
1231 if (id < 0) {
1232 iter.remove();
1233 }
1234 }
1235
1236 Runnable r = new Runnable() {
1237 @Override
1238 public void run() {
Yura085c8532014-02-11 15:15:29 +00001239 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001240 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001241 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001242 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001243 for (int i = 0; i < count; i++) {
1244 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001245 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001246 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1247 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001248 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 }
Yura085c8532014-02-11 15:15:29 +00001250
1251 try {
1252 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1253 } catch (Exception ex) {
1254 throw new RuntimeException(ex);
1255 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001256
Winson Chungba9c37f2013-08-30 14:11:37 -07001257 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001258 sBgWorkspaceScreens.clear();
1259 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001260 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001261 }
1262 };
1263 runOnWorkerThread(r);
1264 }
1265
1266 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 * Remove the contents of the specified folder from the database
1268 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001269 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 final ContentResolver cr = context.getContentResolver();
1271
Michael Jurkac9d95c52011-08-29 14:03:34 -07001272 Runnable r = new Runnable() {
1273 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001274 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001275 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001276 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001277 sBgItemsIdMap.remove(info.id);
1278 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001279 sBgWorkspaceItems.remove(info);
1280 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001281
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001282 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001283 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001284 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001285 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286 for (ItemInfo childInfo : info.contents) {
1287 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001288 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001289 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001290 }
1291 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001292 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001293 }
1294
1295 /**
1296 * Set this as the current Launcher activity object for the loader.
1297 */
1298 public void initialize(Callbacks callbacks) {
1299 synchronized (mLock) {
1300 mCallbacks = new WeakReference<Callbacks>(callbacks);
1301 }
1302 }
1303
Kenny Guyed131872014-04-30 03:02:21 +01001304 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001305 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001306 int op = PackageUpdatedTask.OP_UPDATE;
1307 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1308 user));
1309 }
1310
1311 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001312 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001313 int op = PackageUpdatedTask.OP_REMOVE;
1314 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1315 user));
1316 }
1317
1318 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001319 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001320 int op = PackageUpdatedTask.OP_ADD;
1321 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1322 user));
1323 }
1324
1325 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001326 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001327 boolean replacing) {
1328 if (!replacing) {
1329 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1330 user));
1331 if (mAppsCanBeOnRemoveableStorage) {
1332 // Only rebind if we support removable storage. It catches the
1333 // case where
1334 // apps on the external sd card need to be reloaded
1335 startLoaderFromBackground();
1336 }
1337 } else {
1338 // If we are replacing then just update the packages in the list
1339 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1340 packageNames, user));
1341 }
1342 }
1343
1344 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001345 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001346 boolean replacing) {
1347 if (!replacing) {
1348 enqueuePackageUpdated(new PackageUpdatedTask(
1349 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1350 user));
1351 }
Kenny Guyed131872014-04-30 03:02:21 +01001352 }
1353
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001354 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1356 * ACTION_PACKAGE_CHANGED.
1357 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001358 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001359 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001360 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001361
Joe Onorato36115782010-06-17 13:28:48 -04001362 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001363 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001364 // If we have changed locale we need to clear out the labels in all apps/workspace.
1365 forceReload();
1366 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1367 // Check if configuration change was an mcc/mnc change which would affect app resources
1368 // and we would need to clear out the labels in all apps/workspace. Same handling as
1369 // above for ACTION_LOCALE_CHANGED
1370 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001371 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001372 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001373 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001374 forceReload();
1375 }
1376 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001377 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001378 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1379 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001380 Callbacks callbacks = getCallback();
1381 if (callbacks != null) {
1382 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001383 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001384 }
1385 }
1386
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001387 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001388 resetLoadedState(true, true);
1389
Reena Lee93f824a2011-09-23 17:20:28 -07001390 // Do this here because if the launcher activity is running it will be restarted.
1391 // If it's not running startLoaderFromBackground will merely tell it that it needs
1392 // to reload.
1393 startLoaderFromBackground();
1394 }
1395
Winson Chungf0c6ae02012-03-21 16:10:31 -07001396 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1397 synchronized (mLock) {
1398 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1399 // mWorkspaceLoaded to true later
1400 stopLoaderLocked();
1401 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1402 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1403 }
1404 }
1405
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001406 /**
1407 * When the launcher is in the background, it's possible for it to miss paired
1408 * configuration changes. So whenever we trigger the loader from the background
1409 * tell the launcher that it needs to re-run the loader when it comes back instead
1410 * of doing it now.
1411 */
1412 public void startLoaderFromBackground() {
1413 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001414 Callbacks callbacks = getCallback();
1415 if (callbacks != null) {
1416 // Only actually run the loader if they're not paused.
1417 if (!callbacks.setLoadOnResume()) {
1418 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001419 }
1420 }
1421 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001422 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001423 }
Joe Onorato36115782010-06-17 13:28:48 -04001424 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001425
Reena Lee93f824a2011-09-23 17:20:28 -07001426 // If there is already a loader task running, tell it to stop.
1427 // returns true if isLaunching() was true on the old task
1428 private boolean stopLoaderLocked() {
1429 boolean isLaunching = false;
1430 LoaderTask oldTask = mLoaderTask;
1431 if (oldTask != null) {
1432 if (oldTask.isLaunching()) {
1433 isLaunching = true;
1434 }
1435 oldTask.stopLocked();
1436 }
1437 return isLaunching;
1438 }
1439
Adam Cohen1a85c582014-09-30 09:48:49 -07001440 public boolean isCurrentCallbacks(Callbacks callbacks) {
1441 return (mCallbacks != null && mCallbacks.get() == callbacks);
1442 }
1443
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001445 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1446 }
1447
1448 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001449 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1450 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001451 synchronized (mLock) {
1452 if (DEBUG_LOADERS) {
1453 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 // Clear any deferred bind-runnables from the synchronized load process
1457 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001458 synchronized (mDeferredBindRunnables) {
1459 mDeferredBindRunnables.clear();
1460 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461
Joe Onorato36115782010-06-17 13:28:48 -04001462 // Don't bother to start the thread if we know it's not going to do anything
1463 if (mCallbacks != null && mCallbacks.get() != null) {
1464 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001465 // also, don't downgrade isLaunching if we're already running
1466 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001467 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001468 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1469 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001470 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1471 } else {
1472 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1473 sWorker.post(mLoaderTask);
1474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 }
1477 }
1478
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 void bindRemainingSynchronousPages() {
1480 // Post the remaining side pages to be loaded
1481 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001482 Runnable[] deferredBindRunnables = null;
1483 synchronized (mDeferredBindRunnables) {
1484 deferredBindRunnables = mDeferredBindRunnables.toArray(
1485 new Runnable[mDeferredBindRunnables.size()]);
1486 mDeferredBindRunnables.clear();
1487 }
1488 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001489 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001490 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001491 }
1492 }
1493
Joe Onorato36115782010-06-17 13:28:48 -04001494 public void stopLoader() {
1495 synchronized (mLock) {
1496 if (mLoaderTask != null) {
1497 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 }
1499 }
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001502 /**
1503 * Loads the workspace screen ids in an ordered list.
1504 */
Adam Cohen091440a2015-03-18 14:16:05 -07001505 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001506 final ContentResolver contentResolver = context.getContentResolver();
1507 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001508
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001509 // Get screens ordered by rank.
1510 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1511 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1512 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001513 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001514 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001515 while (sc.moveToNext()) {
1516 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001517 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001518 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001519 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1520 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001521 }
1522 }
1523 } finally {
1524 sc.close();
1525 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001526 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001527 }
1528
Michael Jurkac57b7a82011-08-09 22:02:20 -07001529 public boolean isAllAppsLoaded() {
1530 return mAllAppsLoaded;
1531 }
1532
Winson Chung36a62fe2012-05-06 18:04:42 -07001533 boolean isLoadingWorkspace() {
1534 synchronized (mLock) {
1535 if (mLoaderTask != null) {
1536 return mLoaderTask.isLoadingWorkspace();
1537 }
1538 }
1539 return false;
1540 }
1541
Joe Onorato36115782010-06-17 13:28:48 -04001542 /**
1543 * Runnable for the thread that loads the contents of the launcher:
1544 * - workspace icons
1545 * - widgets
1546 * - all apps icons
1547 */
1548 private class LoaderTask implements Runnable {
1549 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001550 private boolean mIsLaunching;
Adam Cohen091440a2015-03-18 14:16:05 -07001551 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001552 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001553 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001554 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555
Dan Sandlerd5024042014-01-09 15:01:33 -05001556 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001557 mContext = context;
1558 mIsLaunching = isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001559 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001560 }
1561
Joe Onorato36115782010-06-17 13:28:48 -04001562 boolean isLaunching() {
1563 return mIsLaunching;
1564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565
Winson Chung36a62fe2012-05-06 18:04:42 -07001566 boolean isLoadingWorkspace() {
1567 return mIsLoadingAndBindingWorkspace;
1568 }
1569
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001570 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001571 mIsLoadingAndBindingWorkspace = true;
1572
Joe Onorato36115782010-06-17 13:28:48 -04001573 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001574 if (DEBUG_LOADERS) {
1575 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001577
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001578 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001579 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001580 synchronized (LoaderTask.this) {
1581 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001582 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001583 }
1584 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 }
1586 }
1587
Joe Onorato36115782010-06-17 13:28:48 -04001588 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001589 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001590 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001591
Joe Onorato36115782010-06-17 13:28:48 -04001592 private void waitForIdle() {
1593 // Wait until the either we're stopped or the other threads are done.
1594 // This way we don't start loading all apps until the workspace has settled
1595 // down.
1596 synchronized (LoaderTask.this) {
1597 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001598
Joe Onorato36115782010-06-17 13:28:48 -04001599 mHandler.postIdle(new Runnable() {
1600 public void run() {
1601 synchronized (LoaderTask.this) {
1602 mLoadAndBindStepFinished = true;
1603 if (DEBUG_LOADERS) {
1604 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001605 }
Joe Onorato36115782010-06-17 13:28:48 -04001606 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001607 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001608 }
Joe Onorato36115782010-06-17 13:28:48 -04001609 });
1610
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001611 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001612 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001613 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1614 // wait no longer than 1sec at a time
1615 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001616 } catch (InterruptedException ex) {
1617 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001618 }
1619 }
Joe Onorato36115782010-06-17 13:28:48 -04001620 if (DEBUG_LOADERS) {
1621 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001622 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001623 + "ms for previous step to finish binding");
1624 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001627
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001628 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001629 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001630 // Ensure that we have a valid page index to load synchronously
1631 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1632 "valid page index");
1633 }
1634 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1635 // Ensure that we don't try and bind a specified page when the pages have not been
1636 // loaded already (we should load everything asynchronously in that case)
1637 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1638 }
1639 synchronized (mLock) {
1640 if (mIsLoaderTaskRunning) {
1641 // Ensure that we are never running the background loading at this point since
1642 // we also touch the background collections
1643 throw new RuntimeException("Error! Background loading is already running");
1644 }
1645 }
1646
1647 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1648 // data structures, we can't allow any other thread to touch that data, but because
1649 // this call is synchronous, we can get away with not locking).
1650
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001651 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001652 // operations from the previous activity. We need to ensure that all queued operations
1653 // are executed before any synchronous binding work is done.
1654 mHandler.flush();
1655
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 // Divide the set of loaded items into those that we are binding synchronously, and
1657 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001658 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001659 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1660 // arise from that.
1661 onlyBindAllApps();
1662 }
1663
Joe Onorato36115782010-06-17 13:28:48 -04001664 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001665 synchronized (mLock) {
1666 mIsLoaderTaskRunning = true;
1667 }
Joe Onorato36115782010-06-17 13:28:48 -04001668 // Optimize for end-user experience: if the Launcher is up and // running with the
1669 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1670 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001671 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001672 // Elevate priority when Home launches for the first time to avoid
1673 // starving at boot time. Staring at a blank home is not cool.
1674 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001675 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1676 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001677 android.os.Process.setThreadPriority(mIsLaunching
1678 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1679 }
Winson Chung64359a52013-07-08 17:17:08 -07001680 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001681 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001682
Joe Onorato36115782010-06-17 13:28:48 -04001683 if (mStopped) {
1684 break keep_running;
1685 }
1686
1687 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1688 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001689 synchronized (mLock) {
1690 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001691 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001692 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1693 }
1694 }
Joe Onorato36115782010-06-17 13:28:48 -04001695 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001696
1697 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001698 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1699 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001700
Sunny Goyal5b0e6692015-03-19 14:31:19 -07001701 // Remove entries for packages which changed while the launcher was dead.
1702 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews();
1703
Winson Chung7ed37742011-09-08 15:45:51 -07001704 // Restore the default thread priority after we are done loading items
1705 synchronized (mLock) {
1706 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1707 }
Joe Onorato36115782010-06-17 13:28:48 -04001708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709
Joe Onorato36115782010-06-17 13:28:48 -04001710 // Clear out this reference, otherwise we end up holding it until all of the
1711 // callback runnables are done.
1712 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713
Joe Onorato36115782010-06-17 13:28:48 -04001714 synchronized (mLock) {
1715 // If we are still the last one to be scheduled, remove ourselves.
1716 if (mLoaderTask == this) {
1717 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001720 }
Joe Onorato36115782010-06-17 13:28:48 -04001721 }
1722
1723 public void stopLocked() {
1724 synchronized (LoaderTask.this) {
1725 mStopped = true;
1726 this.notify();
1727 }
1728 }
1729
1730 /**
1731 * Gets the callbacks object. If we've been stopped, or if the launcher object
1732 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1733 * object that was around when the deferred message was scheduled, and if there's
1734 * a new Callbacks object around then also return null. This will save us from
1735 * calling onto it with data that will be ignored.
1736 */
1737 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1738 synchronized (mLock) {
1739 if (mStopped) {
1740 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001741 }
Joe Onorato36115782010-06-17 13:28:48 -04001742
1743 if (mCallbacks == null) {
1744 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001745 }
Joe Onorato36115782010-06-17 13:28:48 -04001746
1747 final Callbacks callbacks = mCallbacks.get();
1748 if (callbacks != oldCallbacks) {
1749 return null;
1750 }
1751 if (callbacks == null) {
1752 Log.w(TAG, "no mCallbacks");
1753 return null;
1754 }
1755
1756 return callbacks;
1757 }
1758 }
1759
1760 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001761 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001762 LauncherAppState app = LauncherAppState.getInstance();
1763 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001764 final int countX = (int) grid.numColumns;
1765 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001766
Adam Cohendcd297f2013-06-18 13:13:40 -07001767 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001768 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001769 // Return early if we detect that an item is under the hotseat button
1770 if (mCallbacks == null ||
1771 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001772 Log.e(TAG, "Error loading shortcut into hotseat " + item
1773 + " into position (" + item.screenId + ":" + item.cellX + ","
1774 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001775 return false;
1776 }
1777
Dan Sandler295ae182013-12-10 16:05:47 -05001778 final ItemInfo[][] hotseatItems =
1779 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1780
Adam Cohenae4409d2013-11-26 10:34:59 -08001781 if (item.screenId >= grid.numHotseatIcons) {
1782 Log.e(TAG, "Error loading shortcut " + item
1783 + " into hotseat position " + item.screenId
1784 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1785 + ")");
1786 return false;
1787 }
1788
Dan Sandler295ae182013-12-10 16:05:47 -05001789 if (hotseatItems != null) {
1790 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 Log.e(TAG, "Error loading shortcut into hotseat " + item
1792 + " into position (" + item.screenId + ":" + item.cellX + ","
1793 + item.cellY + ") occupied by "
1794 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1795 [(int) item.screenId][0]);
1796 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001797 } else {
1798 hotseatItems[(int) item.screenId][0] = item;
1799 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001800 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001801 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001802 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 items[(int) item.screenId][0] = item;
1804 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001805 return true;
1806 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001807 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1808 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001809 return true;
1810 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001811
Adam Cohendcd297f2013-06-18 13:13:40 -07001812 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001813 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001814 occupied.put(item.screenId, items);
1815 }
1816
Dan Sandler295ae182013-12-10 16:05:47 -05001817 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001818 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1819 item.cellX < 0 || item.cellY < 0 ||
1820 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1821 Log.e(TAG, "Error loading shortcut " + item
1822 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1823 + item.cellX + "," + item.cellY
1824 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1825 return false;
1826 }
1827
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001828 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001829 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1830 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001831 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001832 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001833 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001834 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001835 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001836 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001837 return false;
1838 }
1839 }
1840 }
1841 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1842 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001843 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001844 }
1845 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001846
Joe Onorato36115782010-06-17 13:28:48 -04001847 return true;
1848 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849
Winson Chungba9c37f2013-08-30 14:11:37 -07001850 /** Clears all the sBg data structures */
1851 private void clearSBgDataStructures() {
1852 synchronized (sBgLock) {
1853 sBgWorkspaceItems.clear();
1854 sBgAppWidgets.clear();
1855 sBgFolders.clear();
1856 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001857 sBgWorkspaceScreens.clear();
1858 }
1859 }
1860
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001861 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001862 // Log to disk
1863 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1864
Joe Onorato36115782010-06-17 13:28:48 -04001865 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866
Joe Onorato36115782010-06-17 13:28:48 -04001867 final Context context = mContext;
1868 final ContentResolver contentResolver = context.getContentResolver();
1869 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001870 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001871 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1872 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001873 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001874
Winson Chung892c74d2013-08-22 16:15:50 -07001875 LauncherAppState app = LauncherAppState.getInstance();
1876 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1877 int countX = (int) grid.numColumns;
1878 int countY = (int) grid.numRows;
1879
Dan Sandlerd5024042014-01-09 15:01:33 -05001880 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1881 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1882 LauncherAppState.getLauncherProvider().deleteDatabase();
1883 }
1884
1885 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1886 // append the user's Launcher2 shortcuts
1887 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1888 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1889 } else {
1890 // Make sure the default workspace is loaded
1891 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001892 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001893 }
Adam Cohene25af792013-06-06 23:08:25 -07001894
Winson Chung2abf94d2012-07-18 18:16:38 -07001895 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001896 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001897 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001898 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001899
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001900 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001902 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001903 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001904 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001905
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 // +1 for the hotseat (it can be larger than the workspace)
1907 // Load workspace in reverse order to ensure that latest items are loaded first (and
1908 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001909 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 try {
1912 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1913 final int intentIndex = c.getColumnIndexOrThrow
1914 (LauncherSettings.Favorites.INTENT);
1915 final int titleIndex = c.getColumnIndexOrThrow
1916 (LauncherSettings.Favorites.TITLE);
1917 final int iconTypeIndex = c.getColumnIndexOrThrow(
1918 LauncherSettings.Favorites.ICON_TYPE);
1919 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1920 final int iconPackageIndex = c.getColumnIndexOrThrow(
1921 LauncherSettings.Favorites.ICON_PACKAGE);
1922 final int iconResourceIndex = c.getColumnIndexOrThrow(
1923 LauncherSettings.Favorites.ICON_RESOURCE);
1924 final int containerIndex = c.getColumnIndexOrThrow(
1925 LauncherSettings.Favorites.CONTAINER);
1926 final int itemTypeIndex = c.getColumnIndexOrThrow(
1927 LauncherSettings.Favorites.ITEM_TYPE);
1928 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1929 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001930 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1931 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 final int screenIndex = c.getColumnIndexOrThrow(
1933 LauncherSettings.Favorites.SCREEN);
1934 final int cellXIndex = c.getColumnIndexOrThrow
1935 (LauncherSettings.Favorites.CELLX);
1936 final int cellYIndex = c.getColumnIndexOrThrow
1937 (LauncherSettings.Favorites.CELLY);
1938 final int spanXIndex = c.getColumnIndexOrThrow
1939 (LauncherSettings.Favorites.SPANX);
1940 final int spanYIndex = c.getColumnIndexOrThrow(
1941 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001942 final int rankIndex = c.getColumnIndexOrThrow(
1943 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001944 final int restoredIndex = c.getColumnIndexOrThrow(
1945 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001946 final int profileIdIndex = c.getColumnIndexOrThrow(
1947 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001948 final int optionsIndex = c.getColumnIndexOrThrow(
1949 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950
Sunny Goyal7f834d22015-04-21 10:10:23 -07001951 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1952 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1953 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1954 }
1955
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 ShortcutInfo info;
1957 String intentDescription;
1958 LauncherAppWidgetInfo appWidgetInfo;
1959 int container;
1960 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001961 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001963 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001964
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 while (!mStopped && c.moveToNext()) {
1966 try {
1967 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001968 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001969 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 switch (itemType) {
1972 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1973 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001974 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001976 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001977 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001978 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001979 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001980 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001981 if (user == null) {
1982 // User has been deleted remove the item.
1983 itemsToRemove.add(id);
1984 continue;
1985 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 try {
1987 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001988 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001989 if (cn != null && cn.getPackageName() != null) {
1990 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1991 cn.getPackageName(), user);
1992 boolean validComponent = validPkg &&
1993 launcherApps.isActivityEnabledForProfile(cn, user);
1994
1995 if (validComponent) {
1996 if (restored) {
1997 // no special handling necessary for this item
1998 restoredRows.add(id);
1999 restored = false;
2000 }
2001 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07002002 intent = null;
2003 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
2004 // We allow auto install apps to have their intent
2005 // updated after an install.
2006 intent = manager.getLaunchIntentForPackage(
2007 cn.getPackageName());
2008 if (intent != null) {
2009 ContentValues values = new ContentValues();
2010 values.put(LauncherSettings.Favorites.INTENT,
2011 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002012 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07002013 }
2014 }
2015
2016 if (intent == null) {
2017 // The app is installed but the component is no
2018 // longer available.
2019 Launcher.addDumpLog(TAG,
2020 "Invalid component removed: " + cn, true);
2021 itemsToRemove.add(id);
2022 continue;
2023 } else {
2024 // no special handling necessary for this item
2025 restoredRows.add(id);
2026 restored = false;
2027 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002028 } else if (restored) {
2029 // Package is not yet available but might be
2030 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05002031 Launcher.addDumpLog(TAG,
2032 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002033
2034 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
2035 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002036 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07002037 // App restore has started. Update the flag
2038 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
2039 ContentValues values = new ContentValues();
2040 values.put(LauncherSettings.Favorites.RESTORED,
2041 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002042 updateItem(id, values);
2043 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
2044 // This is a common app. Try to replace this.
2045 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
2046 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
2047 if (parser.findDefaultApp()) {
2048 // Default app found. Replace it.
2049 intent = parser.parsedIntent;
2050 cn = intent.getComponent();
2051 ContentValues values = parser.parsedValues;
2052 values.put(LauncherSettings.Favorites.RESTORED, 0);
2053 updateItem(id, values);
2054 restored = false;
2055 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07002056
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002057 } else if (REMOVE_UNRESTORED_ICONS) {
2058 Launcher.addDumpLog(TAG,
2059 "Unrestored package removed: " + cn, true);
2060 itemsToRemove.add(id);
2061 continue;
2062 }
Sunny Goyal94485362014-09-18 16:13:58 -07002063 } else if (REMOVE_UNRESTORED_ICONS) {
2064 Launcher.addDumpLog(TAG,
2065 "Unrestored package removed: " + cn, true);
2066 itemsToRemove.add(id);
2067 continue;
2068 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002069 } else if (launcherApps.isAppEnabled(
2070 manager, cn.getPackageName(),
2071 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2072 // Package is present but not available.
2073 allowMissingTarget = true;
2074 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2075 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002076 // SdCard is not ready yet. Package might get available,
2077 // once it is ready.
2078 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2079 + " (check again later)", true);
2080 HashSet<String> pkgs = sPendingPackages.get(user);
2081 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002082 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002083 sPendingPackages.put(user, pkgs);
2084 }
2085 pkgs.add(cn.getPackageName());
2086 allowMissingTarget = true;
2087 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002088
2089 } else {
2090 // Do not wait for external media load anymore.
2091 // Log the invalid package, and remove it
2092 Launcher.addDumpLog(TAG,
2093 "Invalid package removed: " + cn, true);
2094 itemsToRemove.add(id);
2095 continue;
Winson Chungee055712013-07-30 14:46:24 -07002096 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002097 } else if (cn == null) {
2098 // For shortcuts with no component, keep them as they are
2099 restoredRows.add(id);
2100 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002101 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002103 Launcher.addDumpLog(TAG,
2104 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 continue;
2106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002107
Sunny Goyal34b65272015-03-11 16:56:52 -07002108 container = c.getInt(containerIndex);
2109 boolean useLowResIcon = container >= 0 &&
2110 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2111
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002112 if (itemReplaced) {
2113 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002114 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002115 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002116 } else {
2117 // Don't replace items for other profiles.
2118 itemsToRemove.add(id);
2119 continue;
2120 }
2121 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002122 if (user.equals(UserHandleCompat.myUserHandle())) {
2123 Launcher.addDumpLog(TAG,
2124 "constructing info for partially restored package",
2125 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002126 info = getRestoredItemInfo(c, titleIndex, intent,
2127 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002128 intent = getRestoredItemIntent(c, context, intent);
2129 } else {
2130 // Don't restore items for other profiles.
2131 itemsToRemove.add(id);
2132 continue;
2133 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002134 } else if (itemType ==
2135 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002136 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002137 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 } else {
2139 info = getShortcutInfo(c, context, iconTypeIndex,
2140 iconPackageIndex, iconResourceIndex, iconIndex,
2141 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002142
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 // App shortcuts that used to be automatically added to Launcher
2144 // didn't always have the correct intent flags set, so do that
2145 // here
2146 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002147 intent.getCategories() != null &&
2148 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002149 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 intent.addFlags(
2151 Intent.FLAG_ACTIVITY_NEW_TASK |
2152 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2153 }
Michael Jurka96879562012-03-22 05:54:33 -07002154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002155
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002157 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002160 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 info.cellX = c.getInt(cellXIndex);
2162 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002163 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002164 info.spanX = 1;
2165 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002166 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002167 info.isDisabled = disabledState;
2168 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2169 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2170 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002171
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002173 if (!checkItemPlacement(occupied, info)) {
2174 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 break;
2176 }
2177
Sunny Goyal756adbc2015-04-16 15:20:51 -07002178 if (restored) {
2179 ComponentName cn = info.getTargetComponent();
2180 if (cn != null) {
2181 Integer progress = installingPkgs.get(cn.getPackageName());
2182 if (progress != null) {
2183 info.setInstallProgress(progress);
2184 } else {
2185 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2186 }
2187 }
2188 }
2189
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 switch (container) {
2191 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2192 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2193 sBgWorkspaceItems.add(info);
2194 break;
2195 default:
2196 // Item is in a user folder
2197 FolderInfo folderInfo =
2198 findOrMakeFolder(sBgFolders, container);
2199 folderInfo.add(info);
2200 break;
2201 }
2202 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002203 } else {
2204 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 }
2206 break;
2207
2208 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2209 id = c.getLong(idIndex);
2210 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2211
2212 folderInfo.title = c.getString(titleIndex);
2213 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 folderInfo.cellX = c.getInt(cellXIndex);
2218 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002219 folderInfo.spanX = 1;
2220 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002221 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222
Daniel Sandler8802e962010-05-26 16:28:16 -04002223 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002224 if (!checkItemPlacement(occupied, folderInfo)) {
2225 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002226 break;
2227 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002228
Joe Onorato9c1289c2009-08-17 11:03:03 -04002229 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2231 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2232 sBgWorkspaceItems.add(folderInfo);
2233 break;
Joe Onorato36115782010-06-17 13:28:48 -04002234 }
Joe Onorato17a89222011-02-08 17:26:11 -08002235
Chris Wrenf4d08112014-01-16 18:13:56 -05002236 if (restored) {
2237 // no special handling required for restored folders
2238 restoredRows.add(id);
2239 }
2240
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2242 sBgFolders.put(folderInfo.id, folderInfo);
2243 break;
2244
2245 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002246 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002248 boolean customWidget = itemType ==
2249 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002252 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002253 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002254 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002255 user = allUsers.get(serialNumber);
2256 if (user == null) {
2257 itemsToRemove.add(id);
2258 continue;
2259 }
2260
Sunny Goyalff572272014-07-23 13:58:07 -07002261 final ComponentName component =
2262 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002263
Sunny Goyal651077b2014-06-30 14:15:31 -07002264 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002265 final boolean isIdValid = (restoreStatus &
2266 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002267 final boolean wasProviderReady = (restoreStatus &
2268 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002269
Adam Cohen59400422014-03-05 18:07:04 -08002270 final LauncherAppWidgetProviderInfo provider =
2271 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002272 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002273 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002274
2275 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002276 if (!isSafeMode && !customWidget &&
2277 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002278 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002279 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002280
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002282 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 itemsToRemove.add(id);
2284 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002285 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002286 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2287 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002288
2289 if (!customWidget) {
2290 int[] minSpan =
2291 Launcher.getMinSpanForWidget(context, provider);
2292 appWidgetInfo.minSpanX = minSpan[0];
2293 appWidgetInfo.minSpanY = minSpan[1];
2294 }
Sunny Goyalff572272014-07-23 13:58:07 -07002295
2296 int status = restoreStatus;
2297 if (!wasProviderReady) {
2298 // If provider was not previously ready, update the
2299 // status and UI flag.
2300
2301 // Id would be valid only if the widget restore broadcast was received.
2302 if (isIdValid) {
2303 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2304 } else {
2305 status &= ~LauncherAppWidgetInfo
2306 .FLAG_PROVIDER_NOT_READY;
2307 }
2308 }
2309 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002310 } else {
2311 Log.v(TAG, "Widget restore pending id=" + id
2312 + " appWidgetId=" + appWidgetId
2313 + " status =" + restoreStatus);
2314 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002315 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002316 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002317 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002318
2319 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2320 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002321 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002322 // App restore has started. Update the flag
2323 appWidgetInfo.restoreStatus |=
2324 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002325 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002326 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002327 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002328 itemsToRemove.add(id);
2329 continue;
2330 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002331
2332 appWidgetInfo.installProgress =
2333 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002334 }
Sunny Goyalff572272014-07-23 13:58:07 -07002335
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 appWidgetInfo.cellX = c.getInt(cellXIndex);
2339 appWidgetInfo.cellY = c.getInt(cellYIndex);
2340 appWidgetInfo.spanX = c.getInt(spanXIndex);
2341 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002342
Adam Cohen59400422014-03-05 18:07:04 -08002343 if (!customWidget) {
2344 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2345 appWidgetInfo.minSpanX = minSpan[0];
2346 appWidgetInfo.minSpanY = minSpan[1];
2347 }
2348
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 container = c.getInt(containerIndex);
2350 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2351 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2352 Log.e(TAG, "Widget found where container != " +
2353 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2354 continue;
2355 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356
Adam Cohene25af792013-06-06 23:08:25 -07002357 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002359 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2360 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 break;
2362 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002363
Adam Cohen59400422014-03-05 18:07:04 -08002364 if (!customWidget) {
2365 String providerName =
2366 appWidgetInfo.providerName.flattenToString();
2367 if (!providerName.equals(savedProvider) ||
2368 (appWidgetInfo.restoreStatus != restoreStatus)) {
2369 ContentValues values = new ContentValues();
2370 values.put(
2371 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2372 providerName);
2373 values.put(LauncherSettings.Favorites.RESTORED,
2374 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002375 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002376 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002377 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2379 sBgAppWidgets.add(appWidgetInfo);
2380 }
Joe Onorato36115782010-06-17 13:28:48 -04002381 break;
2382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002384 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002385 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 }
2387 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002388 if (c != null) {
2389 c.close();
2390 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 }
2392
Winson Chungba9c37f2013-08-30 14:11:37 -07002393 // Break early if we've stopped loading
2394 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002395 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002396 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002397 }
2398
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399 if (itemsToRemove.size() > 0) {
2400 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002401 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 // Remove dead items
2403 for (long id : itemsToRemove) {
2404 if (DEBUG_LOADERS) {
2405 Log.d(TAG, "Removed id = " + id);
2406 }
2407 // Don't notify content observers
2408 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002409 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 } catch (RemoteException e) {
2411 Log.w(TAG, "Could not remove id = " + id);
2412 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002413 }
2414 }
2415
Chris Wrenf4d08112014-01-16 18:13:56 -05002416 if (restoredRows.size() > 0) {
2417 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002418 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002419 // Update restored items that no longer require special handling
2420 try {
2421 StringBuilder selectionBuilder = new StringBuilder();
2422 selectionBuilder.append(LauncherSettings.Favorites._ID);
2423 selectionBuilder.append(" IN (");
2424 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2425 selectionBuilder.append(")");
2426 ContentValues values = new ContentValues();
2427 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002428 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002429 values, selectionBuilder.toString(), null);
2430 } catch (RemoteException e) {
2431 Log.w(TAG, "Could not update restored rows");
2432 }
2433 }
2434
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002435 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2436 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002437 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002438 null, sWorker);
2439 }
2440
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002441 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2442 // Log to disk
2443 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2444 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002445
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002446 // Remove any empty screens
2447 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2448 for (ItemInfo item: sBgItemsIdMap.values()) {
2449 long screenId = item.screenId;
2450 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2451 unusedScreens.contains(screenId)) {
2452 unusedScreens.remove(screenId);
2453 }
2454 }
2455
2456 // If there are any empty screens remove them, and update.
2457 if (unusedScreens.size() != 0) {
2458 // Log to disk
2459 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2460 TextUtils.join(", ", unusedScreens), true);
2461
2462 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002463 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002464 }
2465
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002466 if (DEBUG_LOADERS) {
2467 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2468 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002469 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002470 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002472
Daniel Sandler566da102013-06-25 23:43:45 -04002473 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002474 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002476 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 line += " | ";
2478 }
Winson Chung892c74d2013-08-22 16:15:50 -07002479 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002480 ItemInfo[][] screen = occupied.get(screenId);
2481 if (x < screen.length && y < screen[x].length) {
2482 line += (screen[x][y] != null) ? "#" : ".";
2483 } else {
2484 line += "!";
2485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 }
Joe Onorato36115782010-06-17 13:28:48 -04002487 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002489 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 }
Joe Onorato36115782010-06-17 13:28:48 -04002491 }
Adam Cohene25af792013-06-06 23:08:25 -07002492 }
2493
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002494 /**
2495 * Partially updates the item without any notification. Must be called on the worker thread.
2496 */
2497 private void updateItem(long itemId, ContentValues update) {
2498 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002499 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002500 update,
2501 BaseColumns._ID + "= ?",
2502 new String[]{Long.toString(itemId)});
2503 }
2504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 /** Filters the set of items who are directly or indirectly (via another container) on the
2506 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002507 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 ArrayList<ItemInfo> allWorkspaceItems,
2509 ArrayList<ItemInfo> currentScreenItems,
2510 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002511 // Purge any null ItemInfos
2512 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2513 while (iter.hasNext()) {
2514 ItemInfo i = iter.next();
2515 if (i == null) {
2516 iter.remove();
2517 }
2518 }
2519
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 // Order the set of items by their containers first, this allows use to walk through the
2521 // list sequentially, build up a list of containers that are in the specified screen,
2522 // as well as all items in those containers.
2523 Set<Long> itemsOnScreen = new HashSet<Long>();
2524 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2525 @Override
2526 public int compare(ItemInfo lhs, ItemInfo rhs) {
2527 return (int) (lhs.container - rhs.container);
2528 }
2529 });
2530 for (ItemInfo info : allWorkspaceItems) {
2531 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 currentScreenItems.add(info);
2534 itemsOnScreen.add(info.id);
2535 } else {
2536 otherScreenItems.add(info);
2537 }
2538 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2539 currentScreenItems.add(info);
2540 itemsOnScreen.add(info.id);
2541 } else {
2542 if (itemsOnScreen.contains(info.container)) {
2543 currentScreenItems.add(info);
2544 itemsOnScreen.add(info.id);
2545 } else {
2546 otherScreenItems.add(info);
2547 }
2548 }
2549 }
2550 }
2551
2552 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002553 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 ArrayList<LauncherAppWidgetInfo> appWidgets,
2555 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2556 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557
2558 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002559 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002561 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 currentScreenWidgets.add(widget);
2563 } else {
2564 otherScreenWidgets.add(widget);
2565 }
2566 }
2567 }
2568
2569 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002570 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571 HashMap<Long, ItemInfo> itemsIdMap,
2572 HashMap<Long, FolderInfo> folders,
2573 HashMap<Long, FolderInfo> currentScreenFolders,
2574 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575
2576 for (long id : folders.keySet()) {
2577 ItemInfo info = itemsIdMap.get(id);
2578 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002579 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002580 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002581 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002582 currentScreenFolders.put(id, folder);
2583 } else {
2584 otherScreenFolders.put(id, folder);
2585 }
2586 }
2587 }
2588
2589 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2590 * right) */
2591 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002592 final LauncherAppState app = LauncherAppState.getInstance();
2593 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 // XXX: review this
2595 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002596 @Override
2597 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002598 int cellCountX = (int) grid.numColumns;
2599 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002600 int screenOffset = cellCountX * cellCountY;
2601 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002602 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002603 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002604 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002605 rhs.cellY * cellCountX + rhs.cellX);
2606 return (int) (lr - rr);
2607 }
2608 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002609 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002610
Adam Cohendcd297f2013-06-18 13:13:40 -07002611 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2612 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002613 final Runnable r = new Runnable() {
2614 @Override
2615 public void run() {
2616 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2617 if (callbacks != null) {
2618 callbacks.bindScreens(orderedScreens);
2619 }
2620 }
2621 };
2622 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2623 }
2624
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2626 final ArrayList<ItemInfo> workspaceItems,
2627 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2628 final HashMap<Long, FolderInfo> folders,
2629 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002630
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002631 final boolean postOnMainThread = (deferredBindRunnables != null);
2632
2633 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002634 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002636 final int start = i;
2637 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002638 final Runnable r = new Runnable() {
2639 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002641 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002642 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002643 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2644 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002645 }
2646 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002647 };
2648 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002649 synchronized (deferredBindRunnables) {
2650 deferredBindRunnables.add(r);
2651 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002652 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002653 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002654 }
Joe Onorato36115782010-06-17 13:28:48 -04002655 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002656
2657 // Bind the folders
2658 if (!folders.isEmpty()) {
2659 final Runnable r = new Runnable() {
2660 public void run() {
2661 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2662 if (callbacks != null) {
2663 callbacks.bindFolders(folders);
2664 }
2665 }
2666 };
2667 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002668 synchronized (deferredBindRunnables) {
2669 deferredBindRunnables.add(r);
2670 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002671 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002672 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002673 }
2674 }
2675
2676 // Bind the widgets, one at a time
2677 N = appWidgets.size();
2678 for (int i = 0; i < N; i++) {
2679 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2680 final Runnable r = new Runnable() {
2681 public void run() {
2682 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2683 if (callbacks != null) {
2684 callbacks.bindAppWidget(widget);
2685 }
2686 }
2687 };
2688 if (postOnMainThread) {
2689 deferredBindRunnables.add(r);
2690 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002691 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002692 }
2693 }
2694 }
2695
2696 /**
2697 * Binds all loaded data to actual views on the main thread.
2698 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002699 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002700 final long t = SystemClock.uptimeMillis();
2701 Runnable r;
2702
2703 // Don't use these two variables in any of the callback runnables.
2704 // Otherwise we hold a reference to them.
2705 final Callbacks oldCallbacks = mCallbacks.get();
2706 if (oldCallbacks == null) {
2707 // This launcher has exited and nobody bothered to tell us. Just bail.
2708 Log.w(TAG, "LoaderTask running with no launcher");
2709 return;
2710 }
2711
Winson Chung9b9fb962013-11-15 15:39:34 -08002712 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002713 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2714 ArrayList<LauncherAppWidgetInfo> appWidgets =
2715 new ArrayList<LauncherAppWidgetInfo>();
2716 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2717 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002718 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002719 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002720 workspaceItems.addAll(sBgWorkspaceItems);
2721 appWidgets.addAll(sBgAppWidgets);
2722 folders.putAll(sBgFolders);
2723 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002724 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002725 }
2726
Derek Prothro7aff3992013-12-10 14:00:37 -05002727 final boolean isLoadingSynchronously =
2728 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002729 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002730 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002731 if (currScreen >= orderedScreenIds.size()) {
2732 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002733 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002734 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002735 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002736 final long currentScreenId = currentScreen < 0
2737 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002738
2739 // Load all the items that are on the current page first (and in the process, unbind
2740 // all the existing workspace items before we call startBinding() below.
2741 unbindWorkspaceItemsOnMainThread();
2742
2743 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002744 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2745 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2746 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2747 new ArrayList<LauncherAppWidgetInfo>();
2748 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2749 new ArrayList<LauncherAppWidgetInfo>();
2750 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2751 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2752
Winson Chung9b9fb962013-11-15 15:39:34 -08002753 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002754 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002755 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002756 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002757 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002758 otherFolders);
2759 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2760 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2761
2762 // Tell the workspace that we're about to start binding items
2763 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002764 public void run() {
2765 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2766 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002767 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002768 }
2769 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002770 };
Winson Chung81b52252012-08-27 15:34:29 -07002771 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002772
Adam Cohendcd297f2013-06-18 13:13:40 -07002773 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002775 // Load items on the current page
2776 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2777 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002778 if (isLoadingSynchronously) {
2779 r = new Runnable() {
2780 public void run() {
2781 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002782 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002783 callbacks.onPageBoundSynchronously(currentScreen);
2784 }
2785 }
2786 };
Winson Chung81b52252012-08-27 15:34:29 -07002787 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002788 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002789
Winson Chung4a2afa32012-07-19 14:53:05 -07002790 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2791 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002792 synchronized (mDeferredBindRunnables) {
2793 mDeferredBindRunnables.clear();
2794 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002795 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002796 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002797
2798 // Tell the workspace that we're done binding items
2799 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002800 public void run() {
2801 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2802 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002803 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002805
Winson Chung98e030b2012-05-07 16:01:11 -07002806 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002807 if (DEBUG_LOADERS) {
2808 Log.d(TAG, "bound workspace in "
2809 + (SystemClock.uptimeMillis()-t) + "ms");
2810 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002811
2812 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002813 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002814 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002815 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002816 synchronized (mDeferredBindRunnables) {
2817 mDeferredBindRunnables.add(r);
2818 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002819 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002820 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002821 }
Joe Onorato36115782010-06-17 13:28:48 -04002822 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002823
Joe Onorato36115782010-06-17 13:28:48 -04002824 private void loadAndBindAllApps() {
2825 if (DEBUG_LOADERS) {
2826 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2827 }
2828 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002829 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002830 synchronized (LoaderTask.this) {
2831 if (mStopped) {
2832 return;
2833 }
2834 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002835 }
Joe Onorato36115782010-06-17 13:28:48 -04002836 } else {
2837 onlyBindAllApps();
2838 }
2839 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002840
Joe Onorato36115782010-06-17 13:28:48 -04002841 private void onlyBindAllApps() {
2842 final Callbacks oldCallbacks = mCallbacks.get();
2843 if (oldCallbacks == null) {
2844 // This launcher has exited and nobody bothered to tell us. Just bail.
2845 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2846 return;
2847 }
2848
2849 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002850 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002851 final ArrayList<AppInfo> list
2852 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002853 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002854 public void run() {
2855 final long t = SystemClock.uptimeMillis();
2856 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2857 if (callbacks != null) {
2858 callbacks.bindAllApplications(list);
2859 }
2860 if (DEBUG_LOADERS) {
2861 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2862 + (SystemClock.uptimeMillis()-t) + "ms");
2863 }
2864 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002865 };
2866 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002867 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002868 r.run();
2869 } else {
2870 mHandler.post(r);
2871 }
Joe Onorato36115782010-06-17 13:28:48 -04002872 }
2873
Winson Chung64359a52013-07-08 17:17:08 -07002874 private void loadAllApps() {
2875 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002876
Joe Onorato36115782010-06-17 13:28:48 -04002877 final Callbacks oldCallbacks = mCallbacks.get();
2878 if (oldCallbacks == null) {
2879 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002880 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002881 return;
2882 }
2883
2884 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2885 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2886
Kenny Guyed131872014-04-30 03:02:21 +01002887 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2888
Winson Chung64359a52013-07-08 17:17:08 -07002889 // Clear the list of apps
2890 mBgAllAppsList.clear();
Sunny Goyale0f58d72014-11-10 18:05:31 -08002891 SharedPreferences prefs = mContext.getSharedPreferences(
2892 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
Kenny Guyed131872014-04-30 03:02:21 +01002893 for (UserHandleCompat user : profiles) {
2894 // Query for the set of apps
2895 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2896 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2897 if (DEBUG_LOADERS) {
2898 Log.d(TAG, "getActivityList took "
2899 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2900 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2901 }
2902 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002903 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002904 if (apps == null || apps.isEmpty()) {
2905 return;
2906 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002907
2908 // Update icon cache
2909 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2910
2911 // If any package icon has changed (app was updated while launcher was dead),
2912 // update the corresponding shortcuts.
2913 if (!updatedPackages.isEmpty()) {
2914 final ArrayList<ShortcutInfo> updates = new ArrayList<ShortcutInfo>();
2915 synchronized (sBgLock) {
2916 for (ItemInfo info : sBgItemsIdMap.values()) {
2917 if (info instanceof ShortcutInfo && user.equals(info.user)
2918 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2919 ShortcutInfo si = (ShortcutInfo) info;
2920 ComponentName cn = si.getTargetComponent();
2921 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2922 si.updateIcon(mIconCache);
2923 updates.add(si);
2924 }
2925 }
2926 }
2927 }
2928
2929 if (!updates.isEmpty()) {
2930 final UserHandleCompat userFinal = user;
2931 mHandler.post(new Runnable() {
2932
2933 public void run() {
2934 Callbacks cb = getCallback();
2935 if (cb != null) {
2936 cb.bindShortcutsChanged(
2937 updates, new ArrayList<ShortcutInfo>(), userFinal);
2938 }
2939 }
2940 });
2941 }
Kenny Guyed131872014-04-30 03:02:21 +01002942 }
Joe Onorato36115782010-06-17 13:28:48 -04002943
Kenny Guyed131872014-04-30 03:02:21 +01002944 // Create the ApplicationInfos
2945 for (int i = 0; i < apps.size(); i++) {
2946 LauncherActivityInfoCompat app = apps.get(i);
2947 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002948 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002949 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002950
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002951 if (!user.equals(UserHandleCompat.myUserHandle())) {
2952 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2953 if (heuristic != null) {
2954 heuristic.processUserApps(apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002955 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002956 }
Winson Chung64359a52013-07-08 17:17:08 -07002957 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002958 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002959 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2960 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002961
2962 // Post callback on main thread
2963 mHandler.post(new Runnable() {
2964 public void run() {
2965 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002966 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002967 if (callbacks != null) {
2968 callbacks.bindAllApplications(added);
2969 if (DEBUG_LOADERS) {
2970 Log.d(TAG, "bound " + added.size() + " apps in "
2971 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2972 }
2973 } else {
2974 Log.i(TAG, "not binding apps: no Launcher activity");
2975 }
2976 }
2977 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002978 // Cleanup any data stored for a deleted user.
2979 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002980
Joe Onorato36115782010-06-17 13:28:48 -04002981 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002982 Log.d(TAG, "Icons processed in "
2983 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002984 }
2985 }
2986
2987 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002988 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002989 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2990 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2991 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2992 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2993 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2994 }
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
2996 }
2997
2998 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002999 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04003000 }
3001
Adam Cohen091440a2015-03-18 14:16:05 -07003002 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003003
3004 @Override
3005 public void onReceive(Context context, Intent intent) {
3006 synchronized (sBgLock) {
3007 final LauncherAppsCompat launcherApps = LauncherAppsCompat
3008 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07003009 final PackageManager manager = context.getPackageManager();
3010 final ArrayList<String> packagesRemoved = new ArrayList<String>();
3011 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003012 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
3013 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003014 packagesRemoved.clear();
3015 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003016 for (String pkg : entry.getValue()) {
3017 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003018 boolean packageOnSdcard = launcherApps.isAppEnabled(
3019 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
3020 if (packageOnSdcard) {
3021 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
3022 packagesUnavailable.add(pkg);
3023 } else {
3024 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
3025 packagesRemoved.add(pkg);
3026 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003027 }
3028 }
3029 if (!packagesRemoved.isEmpty()) {
3030 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
3031 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3032 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003033 if (!packagesUnavailable.isEmpty()) {
3034 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
3035 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3036 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003037 }
Sunny Goyal34942622014-08-29 17:20:55 -07003038 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003039 }
3040 }
3041 }
3042
Joe Onorato36115782010-06-17 13:28:48 -04003043 private class PackageUpdatedTask implements Runnable {
3044 int mOp;
3045 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003046 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003047
3048 public static final int OP_NONE = 0;
3049 public static final int OP_ADD = 1;
3050 public static final int OP_UPDATE = 2;
3051 public static final int OP_REMOVE = 3; // uninstlled
3052 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3053
3054
Kenny Guyed131872014-04-30 03:02:21 +01003055 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003056 mOp = op;
3057 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003058 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003059 }
3060
3061 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003062 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003063
3064 final String[] packages = mPackages;
3065 final int N = packages.length;
3066 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003067 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003068 for (int i=0; i<N; i++) {
3069 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003070 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003071 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003072 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003073
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003074 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3075 if (heuristic != null) {
3076 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003077 }
Joe Onorato36115782010-06-17 13:28:48 -04003078 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003079 }
Joe Onorato36115782010-06-17 13:28:48 -04003080 case OP_UPDATE:
3081 for (int i=0; i<N; i++) {
3082 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003083 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003084 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003085 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003086 }
3087 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003088 case OP_REMOVE: {
3089 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3090 if (heuristic != null) {
3091 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003092 }
Joe Onorato36115782010-06-17 13:28:48 -04003093 for (int i=0; i<N; i++) {
3094 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003095 mIconCache.removeIconsForPkg(packages[i], mUser);
3096 }
3097 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003098 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003099 case OP_UNAVAILABLE:
3100 for (int i=0; i<N; i++) {
3101 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3102 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003103 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003104 }
3105 break;
3106 }
3107
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003108 ArrayList<AppInfo> added = null;
3109 ArrayList<AppInfo> modified = null;
3110 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003111
Adam Cohen487f7dd2012-06-28 18:12:10 -07003112 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003113 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003114 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003115 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003116 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003117 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003118 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003119 }
Winson Chung5d55f332012-07-16 20:45:03 -07003120 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003121 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003122 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003123 }
3124
Sunny Goyale0f58d72014-11-10 18:05:31 -08003125 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003126 if (callbacks == null) {
3127 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3128 return;
3129 }
3130
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3132 new HashMap<ComponentName, AppInfo>();
3133
Joe Onorato36115782010-06-17 13:28:48 -04003134 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003135 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 for (AppInfo ai : added) {
3137 addedOrUpdatedApps.put(ai.componentName, ai);
3138 }
Joe Onorato36115782010-06-17 13:28:48 -04003139 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003140
Joe Onorato36115782010-06-17 13:28:48 -04003141 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003142 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003143 for (AppInfo ai : modified) {
3144 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003145 }
3146
Joe Onorato36115782010-06-17 13:28:48 -04003147 mHandler.post(new Runnable() {
3148 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003149 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003150 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003151 callbacks.bindAppsUpdated(modifiedFinal);
3152 }
3153 }
3154 });
3155 }
Winson Chung83892cc2013-05-01 16:53:33 -07003156
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003158 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3160 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3161 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3162
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003164 synchronized (sBgLock) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165 for (ItemInfo info : sBgItemsIdMap.values()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003166 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3167 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 boolean infoUpdated = false;
3169 boolean shortcutUpdated = false;
3170
3171 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003172 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003173 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003174 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3175 si.iconResource.resourceName, mIconCache, context);
3176 if (icon != null) {
3177 si.setIcon(icon);
3178 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003180 }
3181 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182
3183 ComponentName cn = si.getTargetComponent();
3184 if (cn != null && packageSet.contains(cn.getPackageName())) {
3185 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3186
3187 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003188 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3189 // Auto install icon
3190 PackageManager pm = context.getPackageManager();
3191 ResolveInfo matched = pm.resolveActivity(
3192 new Intent(Intent.ACTION_MAIN)
3193 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3194 PackageManager.MATCH_DEFAULT_ONLY);
3195 if (matched == null) {
3196 // Try to find the best match activity.
3197 Intent intent = pm.getLaunchIntentForPackage(
3198 cn.getPackageName());
3199 if (intent != null) {
3200 cn = intent.getComponent();
3201 appInfo = addedOrUpdatedApps.get(cn);
3202 }
3203
3204 if ((intent == null) || (appInfo == null)) {
3205 removedShortcuts.add(si);
3206 continue;
3207 }
3208 si.promisedIntent = intent;
3209 }
3210 }
3211
3212 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003213 if (appInfo != null) {
3214 si.flags = appInfo.flags;
3215 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003216
Sunny Goyal756adbc2015-04-16 15:20:51 -07003217 si.intent = si.promisedIntent;
3218 si.promisedIntent = null;
3219 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003220 infoUpdated = true;
3221 si.updateIcon(mIconCache);
3222 }
3223
3224 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3225 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3226 si.updateIcon(mIconCache);
3227 si.title = appInfo.title.toString();
3228 si.contentDescription = appInfo.contentDescription;
3229 infoUpdated = true;
3230 }
3231
3232 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3233 // Since package was just updated, the target must be available now.
3234 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3235 shortcutUpdated = true;
3236 }
3237 }
3238
3239 if (infoUpdated || shortcutUpdated) {
3240 updatedShortcuts.add(si);
3241 }
3242 if (infoUpdated) {
3243 updateItemInDatabase(context, si);
3244 }
3245 } else if (info instanceof LauncherAppWidgetInfo) {
3246 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3247 if (mUser.equals(widgetInfo.user)
3248 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3249 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3250 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3251 widgets.add(widgetInfo);
3252 updateItemInDatabase(context, widgetInfo);
3253 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003254 }
3255 }
3256 }
3257
Sunny Goyal4390ace2014-10-13 11:33:11 -07003258 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3259 mHandler.post(new Runnable() {
3260
3261 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003262 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003263 if (callbacks == cb && cb != null) {
3264 callbacks.bindShortcutsChanged(
3265 updatedShortcuts, removedShortcuts, mUser);
3266 }
3267 }
3268 });
3269 if (!removedShortcuts.isEmpty()) {
3270 deleteItemsFromDatabase(context, removedShortcuts);
3271 }
3272 }
3273 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003274 mHandler.post(new Runnable() {
3275 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003276 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003277 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003278 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003279 }
3280 }
3281 });
3282 }
3283 }
3284
Winson Chungdf95eb12013-10-16 14:57:07 -07003285 final ArrayList<String> removedPackageNames =
3286 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003287 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003288 // Mark all packages in the broadcast to be removed
3289 removedPackageNames.addAll(Arrays.asList(packages));
3290 } else if (mOp == OP_UPDATE) {
3291 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003292 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003293 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003294 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003295 }
3296 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003297 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003298
Winson Chungdf95eb12013-10-16 14:57:07 -07003299 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003300 final int removeReason;
3301 if (mOp == OP_UNAVAILABLE) {
3302 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3303 } else {
3304 // Remove all the components associated with this package
3305 for (String pn : removedPackageNames) {
3306 deletePackageFromDatabase(context, pn, mUser);
3307 }
3308 // Remove all the specific components
3309 for (AppInfo a : removedApps) {
3310 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3311 deleteItemsFromDatabase(context, infos);
3312 }
3313 removeReason = 0;
3314 }
3315
Winson Chungdf95eb12013-10-16 14:57:07 -07003316 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003317 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003318 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003319 mHandler.post(new Runnable() {
3320 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003321 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003322 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003323 callbacks.bindComponentsRemoved(
3324 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003325 }
3326 }
3327 });
Joe Onoratobe386092009-11-17 17:32:16 -08003328 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003329 if (Build.VERSION.SDK_INT < 17) {
3330 loadAndBindWidgetsAndShortcuts(context, callbacks);
3331 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003332 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003333 mHandler.post(new Runnable() {
3334 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003335 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003336 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003337 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003338 }
3339 }
3340 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 }
3342 }
3343
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003344 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3345 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003346 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003347 if (sBgWidgetProviders == null || refresh) {
3348 sBgWidgetProviders = new HashMap<>();
3349 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3350 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003351
Robin Lee26ace122015-03-16 19:41:43 +00003352 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3353 for (AppWidgetProviderInfo pInfo : widgets) {
3354 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3355 UserHandleCompat user = wm.getUser(info);
3356 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3357 }
3358
3359 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3360 for (CustomAppWidget widget : customWidgets) {
3361 info = new LauncherAppWidgetProviderInfo(context, widget);
3362 UserHandleCompat user = wm.getUser(info);
3363 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3364 }
Adam Cohen59400422014-03-05 18:07:04 -08003365 }
Adam Cohen59400422014-03-05 18:07:04 -08003366 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3367 }
3368 }
3369
Robin Lee26ace122015-03-16 19:41:43 +00003370 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3371 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003372 synchronized (sBgLock) {
3373 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003374 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003375 }
Robin Lee26ace122015-03-16 19:41:43 +00003376 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003377 }
3378 }
3379
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003380 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks) {
3381 runOnWorkerThread(new Runnable(){
3382 @Override
3383 public void run() {
3384 final ArrayList<Object> list =
3385 getSortedWidgetsAndShortcuts(context, true /* refresh */);
3386 mHandler.post(new Runnable() {
3387 @Override
3388 public void run() {
3389 Callbacks cb = getCallback();
3390 if (callbacks == cb && cb != null) {
3391 callbacks.bindPackagesUpdated(list);
3392 }
3393 }
3394 });
3395 }
3396 });
3397 }
3398
Winson Chungb745afb2015-03-02 11:51:23 -08003399 // Returns a list of ResolveInfos/AppWidgetInfos in sorted order
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003400 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003401 PackageManager packageManager = context.getPackageManager();
3402 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003403 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003404 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3405 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003406 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003407 return widgetsAndShortcuts;
3408 }
3409
Adam Cohen091440a2015-03-18 14:16:05 -07003410 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003411 UserHandleCompat user) {
3412 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3413 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003414 }
Adam Cohen556f6132014-01-15 15:18:08 -08003415
Kenny Guyed131872014-04-30 03:02:21 +01003416 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3417 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003418 if (cn == null) {
3419 return false;
3420 }
Kenny Guyed131872014-04-30 03:02:21 +01003421 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3422 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003423 return false;
3424 }
Kenny Guyed131872014-04-30 03:02:21 +01003425 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003426 }
3427
Adam Cohena28b78e2014-05-20 17:03:04 -07003428 public static boolean isValidPackage(Context context, String packageName,
3429 UserHandleCompat user) {
3430 if (packageName == null) {
3431 return false;
3432 }
3433 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3434 return launcherApps.isPackageEnabledForProfile(packageName, user);
3435 }
3436
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003438 * Make an ShortcutInfo object for a restored application or shortcut item that points
3439 * to a package that is not yet installed on the system.
3440 */
Sunny Goyal34942622014-08-29 17:20:55 -07003441 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003442 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003443 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003444 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003445 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003446
3447 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3448 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3449 if (!TextUtils.isEmpty(title)) {
3450 info.title = title;
3451 }
Sunny Goyal34942622014-08-29 17:20:55 -07003452 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3453 if (TextUtils.isEmpty(info.title)) {
3454 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3455 }
Sunny Goyal34942622014-08-29 17:20:55 -07003456 } else {
3457 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3458 }
3459
Kenny Guyc2bd8102014-06-30 12:30:31 +01003460 info.contentDescription = mUserManager.getBadgedLabelForUser(
3461 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003462 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003463 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003464 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003465 return info;
3466 }
3467
3468 /**
3469 * Make an Intent object for a restored application or shortcut item that points
3470 * to the market page for the item.
3471 */
Adam Cohen091440a2015-03-18 14:16:05 -07003472 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003473 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003474 return getMarketIntent(componentName.getPackageName());
3475 }
3476
3477 static Intent getMarketIntent(String packageName) {
3478 return new Intent(Intent.ACTION_VIEW)
3479 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003480 .scheme("market")
3481 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003482 .appendQueryParameter("id", packageName)
3483 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003484 }
3485
3486 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003487 * Make an ShortcutInfo object for a shortcut that is an application.
3488 *
3489 * If c is not null, then it will be used to fill in missing data like the title and icon.
3490 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003491 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003492 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003493 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003494 if (user == null) {
3495 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003496 return null;
3497 }
3498
Kenny Guyed131872014-04-30 03:02:21 +01003499 ComponentName componentName = intent.getComponent();
3500 if (componentName == null) {
3501 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3502 return null;
3503 }
3504
3505 Intent newIntent = new Intent(intent.getAction(), null);
3506 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3507 newIntent.setComponent(componentName);
3508 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003509 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003510 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3511 return null;
3512 }
3513
3514 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003515 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003516 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3517 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3518 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003519 }
3520
Joe Onorato56d82912010-03-07 14:32:10 -05003521 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003522 if (TextUtils.isEmpty(info.title) && c != null) {
3523 info.title = c.getString(titleIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003524 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003525
Joe Onorato56d82912010-03-07 14:32:10 -05003526 // fall back to the class name of the activity
3527 if (info.title == null) {
3528 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003529 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003530
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003532 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003533 info.contentDescription = mUserManager.getBadgedLabelForUser(
3534 info.title.toString(), info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003535 if (lai != null) {
3536 info.flags = AppInfo.initFlags(lai);
3537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003539 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003540
Winson Chung64359a52013-07-08 17:17:08 -07003541 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3542 ItemInfoFilter f) {
3543 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3544 for (ItemInfo i : infos) {
3545 if (i instanceof ShortcutInfo) {
3546 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003547 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003548 if (cn != null && f.filterItem(null, info, cn)) {
3549 filtered.add(info);
3550 }
3551 } else if (i instanceof FolderInfo) {
3552 FolderInfo info = (FolderInfo) i;
3553 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003554 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003555 if (cn != null && f.filterItem(info, s, cn)) {
3556 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003557 }
3558 }
Winson Chung64359a52013-07-08 17:17:08 -07003559 } else if (i instanceof LauncherAppWidgetInfo) {
3560 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3561 ComponentName cn = info.providerName;
3562 if (cn != null && f.filterItem(null, info, cn)) {
3563 filtered.add(info);
3564 }
Winson Chung8a435102012-08-30 17:16:53 -07003565 }
3566 }
Winson Chung64359a52013-07-08 17:17:08 -07003567 return new ArrayList<ItemInfo>(filtered);
3568 }
3569
Adam Cohen091440a2015-03-18 14:16:05 -07003570 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003571 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003572 ItemInfoFilter filter = new ItemInfoFilter() {
3573 @Override
3574 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003575 if (info.user == null) {
3576 return cn.equals(cname);
3577 } else {
3578 return cn.equals(cname) && info.user.equals(user);
3579 }
Winson Chung64359a52013-07-08 17:17:08 -07003580 }
3581 };
3582 return filterItemInfos(sBgItemsIdMap.values(), filter);
3583 }
3584
Sunny Goyal1a745e82014-10-02 15:58:31 -07003585 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003587 */
Adam Cohen091440a2015-03-18 14:16:05 -07003588 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003589 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3590 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003591
Joe Onorato56d82912010-03-07 14:32:10 -05003592 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003593 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003594 // Non-app shortcuts are only supported for current user.
3595 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003598 // TODO: If there's an explicit component and we can't install that, delete it.
3599
Joe Onorato56d82912010-03-07 14:32:10 -05003600 info.title = c.getString(titleIndex);
3601
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 int iconType = c.getInt(iconTypeIndex);
3603 switch (iconType) {
3604 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3605 String packageName = c.getString(iconPackageIndex);
3606 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003607 info.customIcon = false;
3608 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003609 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003610 // the db
3611 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003612 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003613 }
3614 // the fallback icon
3615 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003616 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003617 info.usingFallbackIcon = true;
3618 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 break;
3620 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003621 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003622 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003623 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003624 info.customIcon = false;
3625 info.usingFallbackIcon = true;
3626 } else {
3627 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 break;
3630 default:
Kenny Guyed131872014-04-30 03:02:21 +01003631 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003632 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 info.customIcon = false;
3634 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003635 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003636 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 return info;
3638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639
Sunny Goyal2350bc92014-10-14 16:42:54 -07003640 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003641 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3642 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3643 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3644
Adam Cohend9198822011-11-22 16:42:47 -08003645 if (intent == null) {
3646 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3647 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3648 return null;
3649 }
3650
Joe Onorato0589f0f2010-02-08 13:44:00 -08003651 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003652 boolean customIcon = false;
3653 ShortcutIconResource iconResource = null;
3654
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003655 if (bitmap instanceof Bitmap) {
3656 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003657 customIcon = true;
3658 } else {
3659 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003660 if (extra instanceof ShortcutIconResource) {
3661 iconResource = (ShortcutIconResource) extra;
3662 icon = Utilities.createIconBitmap(iconResource.packageName,
3663 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003664 }
3665 }
3666
Michael Jurkac9d95c52011-08-29 14:03:34 -07003667 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003668
Kenny Guyed131872014-04-30 03:02:21 +01003669 // Only support intents for current user for now. Intents sent from other
3670 // users wouldn't get here without intent forwarding anyway.
3671 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003672 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003673 icon = mIconCache.getDefaultIcon(info.user);
3674 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003675 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003676 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003677
Joe Onorato0589f0f2010-02-08 13:44:00 -08003678 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003679 info.contentDescription = mUserManager.getBadgedLabelForUser(
3680 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003681 info.intent = intent;
3682 info.customIcon = customIcon;
3683 info.iconResource = iconResource;
3684
3685 return info;
3686 }
3687
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003689 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 * or make a new one.
3691 */
Adam Cohen091440a2015-03-18 14:16:05 -07003692 @Thunk static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 // See if a placeholder was created for us already
3694 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003695 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003697 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 folders.put(id, folderInfo);
3699 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003700 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003701 }
3702
Winson Chung1ed747a2011-05-03 16:18:34 -07003703 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003704 private final AppWidgetManagerCompat mManager;
3705 private final PackageManager mPackageManager;
3706 private final HashMap<Object, String> mLabelCache;
3707 private final Collator mCollator;
3708
Hyunyoung Song3f471442015-04-08 19:01:34 -07003709 public WidgetAndShortcutNameComparator(Context context) {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003710 mManager = AppWidgetManagerCompat.getInstance(context);
3711 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003712 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003713 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003714 }
3715 public final int compare(Object a, Object b) {
3716 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003717 if (mLabelCache.containsKey(a)) {
3718 labelA = mLabelCache.get(a);
3719 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003720 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3721 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003722 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003723 mLabelCache.put(a, labelA);
3724 }
3725 if (mLabelCache.containsKey(b)) {
3726 labelB = mLabelCache.get(b);
3727 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003728 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c1652015-01-30 10:28:23 -08003729 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003730 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003731 mLabelCache.put(b, labelB);
3732 }
Winson Chung11904872012-09-17 16:58:46 -07003733 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003734 }
3735 };
Joe Onoratobe386092009-11-17 17:32:16 -08003736
Sunny Goyal651077b2014-06-30 14:15:31 -07003737 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3738 return (provider != null) && (provider.provider != null)
3739 && (provider.provider.getPackageName() != null);
3740 }
3741
Joe Onoratobe386092009-11-17 17:32:16 -08003742 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003743 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003744 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3745 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3746 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3747 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003748 if (mLoaderTask != null) {
3749 mLoaderTask.dumpState();
3750 } else {
3751 Log.d(TAG, "mLoaderTask=null");
3752 }
Joe Onoratobe386092009-11-17 17:32:16 -08003753 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003754
3755 public Callbacks getCallback() {
3756 return mCallbacks != null ? mCallbacks.get() : null;
3757 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003758
3759 /**
3760 * @return {@link FolderInfo} if its already loaded.
3761 */
3762 public FolderInfo findFolderById(Long folderId) {
3763 synchronized (sBgLock) {
3764 return sBgFolders.get(folderId);
3765 }
3766 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003767
3768 /**
3769 * @return the looper for the worker thread which can be used to start background tasks.
3770 */
3771 public static Looper getWorkerLooper() {
3772 return sWorkerThread.getLooper();
3773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003774}