blob: 00afd98d004cb5311a473867588a1c13bdba0b12 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080038import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070040import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070044import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040049import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080050import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070053import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010054
Sunny Goyalffe83f12014-08-14 17:39:34 -070055import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.LauncherActivityInfoCompat;
57import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070058import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070059import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010060import com.android.launcher3.compat.UserHandleCompat;
61import com.android.launcher3.compat.UserManagerCompat;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.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
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700134 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700135 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700137 // The lock that must be acquired before referencing any static bg data structures. Unlike
138 // other locks, this one can generally be held long-term because we never expect any of these
139 // static data structures to be referenced outside of the worker thread except on the first
140 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700141 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700145 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
148 // created by LauncherModel that are directly on the home screen (however, no widgets or
149 // shortcuts within folders).
150 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
153 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154 new ArrayList<LauncherAppWidgetInfo>();
155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700158
Adam Cohendcd297f2013-06-18 13:13:40 -0700159 // sBgWorkspaceScreens is the ordered set of workspace screens.
160 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
161
Adam Cohen59400422014-03-05 18:07:04 -0800162 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000163 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800164
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700165 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700166 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
167 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700168
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 // </ only access in worker thread >
170
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Reena Lee99a73f32011-10-24 17:27:37 -0700173 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700174
Adam Cohen091440a2015-03-18 14:16:05 -0700175 @Thunk final LauncherAppsCompat mLauncherApps;
176 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700179 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public int getCurrentWorkspaceScreen();
181 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700182 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
183 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700185 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700186 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800187 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700190 public void bindAppsAdded(ArrayList<Long> newScreens,
191 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700192 ArrayList<ItemInfo> addAnimated,
193 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700195 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
196 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
197 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700198 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700199 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700200 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Michael Jurkac402cd92013-05-20 15:49:32 +0200201 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100202 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700203 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700204 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700205 public void dumpLogsToLocalData();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800206 public void bindAddPendingItem(PendingAddItemInfo info, long container, long screenId,
207 int[] cell, int spanX, int spanY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chung64359a52013-07-08 17:17:08 -0700210 public interface ItemInfoFilter {
211 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
212 }
213
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800214 public interface ScreenPosProvider {
215 int getScreenIndex(ArrayList<Long> screenIDs);
216 }
217
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800219 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400220
Winson Chungee055712013-07-30 14:46:24 -0700221 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700222 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400223 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
224 // resource string.
225 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
226 ProviderInfo providerInfo =
227 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
228 ProviderInfo redirectProvider =
229 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700230
231 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400232 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 if (mOldContentProviderExists) {
235 Log.d(TAG, "Old launcher provider exists.");
236 } else {
237 Log.d(TAG, "Old launcher provider does not exist.");
238 }
239
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100241 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
243
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400244 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700245 Configuration config = res.getConfiguration();
246 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100247 mLauncherApps = LauncherAppsCompat.getInstance(context);
248 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 }
250
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
252 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700254 if (sWorkerThread.getThreadId() == Process.myTid()) {
255 // If we are on the worker thread, post onto the main handler
256 mHandler.post(r);
257 } else {
258 r.run();
259 }
260 }
261
262 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
263 * posted on the worker thread handler. */
264 private static void runOnWorkerThread(Runnable r) {
265 if (sWorkerThread.getThreadId() == Process.myTid()) {
266 r.run();
267 } else {
268 // If we are not on the worker thread, then post to the worker handler
269 sWorker.post(r);
270 }
271 }
272
Winson Chunge43a1e72014-01-15 10:33:02 -0800273 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
274 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800275 }
276
Sunny Goyal756adbc2015-04-16 15:20:51 -0700277 public void setPackageState(final PackageInstallInfo installInfo) {
278 Runnable updateRunnable = new Runnable() {
279
280 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500281 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700282 synchronized (sBgLock) {
283 final HashSet<ItemInfo> updates = new HashSet<>();
284
285 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
286 // Ignore install success events as they are handled by Package add events.
287 return;
288 }
289
Sunny Goyale2df0622015-04-24 11:27:00 -0700290 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 if (info instanceof ShortcutInfo) {
292 ShortcutInfo si = (ShortcutInfo) info;
293 ComponentName cn = si.getTargetComponent();
294 if (si.isPromise() && (cn != null)
295 && installInfo.packageName.equals(cn.getPackageName())) {
296 si.setInstallProgress(installInfo.progress);
297
298 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
299 // Mark this info as broken.
300 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
301 }
302 updates.add(si);
303 }
304 }
305 }
306
307 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
308 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
309 widget.installProgress = installInfo.progress;
310 updates.add(widget);
311 }
312 }
313
314 if (!updates.isEmpty()) {
315 // Push changes to the callback.
316 Runnable r = new Runnable() {
317 public void run() {
318 Callbacks callbacks = getCallback();
319 if (callbacks != null) {
320 callbacks.bindRestoreItemsChange(updates);
321 }
322 }
323 };
324 mHandler.post(r);
325 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500326 }
327 }
328 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500330 }
331
Sunny Goyal756adbc2015-04-16 15:20:51 -0700332 /**
333 * Updates the icons and label of all pending icons for the provided package name.
334 */
335 public void updateSessionDisplayInfo(final String packageName) {
336 Runnable updateRunnable = new Runnable() {
337
338 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700339 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 synchronized (sBgLock) {
341 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
342 final UserHandleCompat user = UserHandleCompat.myUserHandle();
343
Sunny Goyale2df0622015-04-24 11:27:00 -0700344 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 if (info instanceof ShortcutInfo) {
346 ShortcutInfo si = (ShortcutInfo) info;
347 ComponentName cn = si.getTargetComponent();
348 if (si.isPromise() && (cn != null)
349 && packageName.equals(cn.getPackageName())) {
350 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
351 // For auto install apps update the icon as well as label.
352 mIconCache.getTitleAndIcon(si,
353 si.promisedIntent, user,
354 si.shouldUseLowResIcon());
355 } else {
356 // Only update the icon for restored apps.
357 si.updateIcon(mIconCache);
358 }
359 updates.add(si);
360 }
361 }
362 }
363
364 if (!updates.isEmpty()) {
365 // Push changes to the callback.
366 Runnable r = new Runnable() {
367 public void run() {
368 Callbacks callbacks = getCallback();
369 if (callbacks != null) {
370 callbacks.bindShortcutsChanged(updates,
371 new ArrayList<ShortcutInfo>(), user);
372 }
373 }
374 };
375 mHandler.post(r);
376 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700377 }
378 }
379 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700380 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700381 }
382
Adam Cohen76a47a12014-02-05 11:47:43 -0800383 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800384 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800385
386 if (allAppsApps == null) {
387 throw new RuntimeException("allAppsApps must not be null");
388 }
389 if (allAppsApps.isEmpty()) {
390 return;
391 }
392
393 // Process the newly added applications and add them to the database first
394 Runnable r = new Runnable() {
395 public void run() {
396 runOnMainThread(new Runnable() {
397 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800398 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800399 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500400 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800401 }
402 }
403 });
404 }
405 };
406 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700407 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800408
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700409 public void addAndBindAddedWorkspaceItems(final Context context,
Adam Cohen76a47a12014-02-05 11:47:43 -0800410 final ArrayList<ItemInfo> workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700411 addAndBindAddedWorkspaceItems(context, workspaceApps,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 new ScreenPosProvider() {
Adam Cohen76a47a12014-02-05 11:47:43 -0800413
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 @Override
415 public int getScreenIndex(ArrayList<Long> screenIDs) {
416 return screenIDs.isEmpty() ? 0 : 1;
417 }
418 }, 1, false);
419 }
420
421 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<Rect> occupiedPos,
422 int[] xy, int spanX, int spanY) {
423 LauncherAppState app = LauncherAppState.getInstance();
424 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
425 final int xCount = (int) grid.numColumns;
426 final int yCount = (int) grid.numRows;
427 boolean[][] occupied = new boolean[xCount][yCount];
428 if (occupiedPos != null) {
429 for (Rect r : occupiedPos) {
430 for (int x = r.left; 0 <= x && x < r.right && x < xCount; x++) {
431 for (int y = r.top; 0 <= y && y < r.bottom && y < yCount; y++) {
432 occupied[x][y] = true;
433 }
434 }
435 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800436 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700437 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 }
439
440 /**
441 * Find a position on the screen for the given size or adds a new screen.
442 * @return screenId and the coordinates for the item.
443 */
Adam Cohen091440a2015-03-18 14:16:05 -0700444 @Thunk static Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 Context context,
446 ScreenPosProvider preferredScreen,
447 int fallbackStartScreen,
448 ArrayList<Long> workspaceScreens,
449 ArrayList<Long> addedWorkspaceScreensFinal,
450 int spanX, int spanY) {
451 // Load position of items which are on the desktop. We can't use sBgItemsIdMap because
452 // loadWorkspace() may not have been called.
453 final ContentResolver cr = context.getContentResolver();
454 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
455 new String[] {
456 LauncherSettings.Favorites.SCREEN,
457 LauncherSettings.Favorites.CELLX,
458 LauncherSettings.Favorites.CELLY,
459 LauncherSettings.Favorites.SPANX,
460 LauncherSettings.Favorites.SPANY,
461 LauncherSettings.Favorites.CONTAINER
462 },
463 "container=?",
464 new String[] { Integer.toString(LauncherSettings.Favorites.CONTAINER_DESKTOP) },
465 null);
466
467 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
468 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
469 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
470 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
471 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
472 LongSparseArray<ArrayList<Rect>> screenItems = new LongSparseArray<ArrayList<Rect>>();
473 try {
474 while (c.moveToNext()) {
475 Rect rect = new Rect();
476 rect.left = c.getInt(cellXIndex);
477 rect.top = c.getInt(cellYIndex);
478 rect.right = rect.left + Math.max(1, c.getInt(spanXIndex));
479 rect.bottom = rect.top + Math.max(1, c.getInt(spanYIndex));
480
481 long screenId = c.getInt(screenIndex);
482 ArrayList<Rect> items = screenItems.get(screenId);
483 if (items == null) {
484 items = new ArrayList<Rect>();
485 screenItems.put(screenId, items);
486 }
487 items.add(rect);
488 }
489 } catch (Exception e) {
490 screenItems.clear();
491 } finally {
492 c.close();
493 }
494
495 // Find appropriate space for the item.
496 long screenId = 0;
497 int[] cordinates = new int[2];
498 boolean found = false;
499
500 int screenCount = workspaceScreens.size();
501 // First check the preferred screen.
502 int preferredScreenIndex = preferredScreen.getScreenIndex(workspaceScreens);
503 if (preferredScreenIndex < screenCount) {
504 screenId = workspaceScreens.get(preferredScreenIndex);
505 found = findNextAvailableIconSpaceInScreen(
506 screenItems.get(screenId), cordinates, spanX, spanY);
507 }
508
509 if (!found) {
510 // Search on any of the screens.
511 for (int screen = fallbackStartScreen; screen < screenCount; screen++) {
512 screenId = workspaceScreens.get(screen);
513 if (findNextAvailableIconSpaceInScreen(
514 screenItems.get(screenId), cordinates, spanX, spanY)) {
515 // We found a space for it
516 found = true;
517 break;
518 }
519 }
520 }
521
522 if (!found) {
523 // Still no position found. Add a new screen to the end.
524 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
525
526 // Save the screen id for binding in the workspace
527 workspaceScreens.add(screenId);
528 addedWorkspaceScreensFinal.add(screenId);
529
530 // If we still can't find an empty space, then God help us all!!!
531 if (!findNextAvailableIconSpaceInScreen(
532 screenItems.get(screenId), cordinates, spanX, spanY)) {
533 throw new RuntimeException("Can't find space to add the item");
534 }
535 }
536 return Pair.create(screenId, cordinates);
537 }
538
539 /**
540 * Adds the provided items to the workspace.
541 * @param preferredScreen the screen where we should try to add the app first
542 * @param fallbackStartScreen the screen to start search for empty space if
543 * preferredScreen is not available.
544 */
545 public void addAndBindPendingItem(
546 final Context context,
547 final PendingAddItemInfo addInfo,
548 final ScreenPosProvider preferredScreen,
549 final int fallbackStartScreen) {
550 final Callbacks callbacks = getCallback();
551 // Process the newly added applications and add them to the database first
552 Runnable r = new Runnable() {
553 public void run() {
554 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800555 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800556
557 // Find appropriate space for the item.
558 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
559 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
560 addInfo.spanX,
561 addInfo.spanY);
562 final long screenId = coords.first;
563 final int[] cordinates = coords.second;
564
565 // Update the workspace screens
566 updateWorkspaceScreenOrder(context, workspaceScreens);
567 runOnMainThread(new Runnable() {
568 public void run() {
569 Callbacks cb = getCallback();
570 if (callbacks == cb && cb != null) {
571 cb.bindAddScreens(addedWorkspaceScreensFinal);
572 cb.bindAddPendingItem(addInfo,
573 LauncherSettings.Favorites.CONTAINER_DESKTOP,
574 screenId, cordinates, addInfo.spanX, addInfo.spanY);
575 }
576 }
577 });
578 }
579 };
580 runOnWorkerThread(r);
581 }
582
583 /**
584 * Adds the provided items to the workspace.
585 * @param preferredScreen the screen where we should try to add the app first
586 * @param fallbackStartScreen the screen to start search for empty space if
587 * preferredScreen is not available.
588 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700589 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800590 final ArrayList<ItemInfo> workspaceApps,
591 final ScreenPosProvider preferredScreen,
592 final int fallbackStartScreen,
593 final boolean allowDuplicate) {
594 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800595 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700596 return;
Winson Chung997a9232013-07-24 15:33:46 -0700597 }
Winson Chung64359a52013-07-08 17:17:08 -0700598 // Process the newly added applications and add them to the database first
599 Runnable r = new Runnable() {
600 public void run() {
601 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
602 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
603
Winson Chung76828c82013-08-19 15:43:29 -0700604 // Get the list of workspace screens. We need to append to this list and
605 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
606 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800607 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700608 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800609 for (ItemInfo item : workspaceApps) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700610 if (!allowDuplicate && item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800611 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700612 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800613 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700614 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800615 }
Winson Chung76828c82013-08-19 15:43:29 -0700616
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800617 // Find appropriate space for the item.
618 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
619 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
620 1, 1);
621 long screenId = coords.first;
622 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700623
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700624 ItemInfo itemInfo;
625 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
626 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800627 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700628 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700629 } else {
630 throw new RuntimeException("Unexpected info type");
631 }
Winson Chung94d67682013-09-25 16:29:40 -0700632
Winson Chung64359a52013-07-08 17:17:08 -0700633 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700634 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700635 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700636 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700637 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700638 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700639 }
640 }
641
Winson Chung76828c82013-08-19 15:43:29 -0700642 // Update the workspace screens
643 updateWorkspaceScreenOrder(context, workspaceScreens);
644
Adam Cohen76a47a12014-02-05 11:47:43 -0800645 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700646 runOnMainThread(new Runnable() {
647 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800648 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700649 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700650 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
651 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700652 if (!addedShortcutsFinal.isEmpty()) {
653 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
654 long lastScreenId = info.screenId;
655 for (ItemInfo i : addedShortcutsFinal) {
656 if (i.screenId == lastScreenId) {
657 addAnimated.add(i);
658 } else {
659 addNotAnimated.add(i);
660 }
Winson Chung997a9232013-07-24 15:33:46 -0700661 }
662 }
Winson Chungd64d1762013-08-20 14:37:16 -0700663 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800664 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700665 }
Winson Chung64359a52013-07-08 17:17:08 -0700666 }
Winson Chung997a9232013-07-24 15:33:46 -0700667 });
668 }
Winson Chung64359a52013-07-08 17:17:08 -0700669 }
670 };
671 runOnWorkerThread(r);
672 }
673
Sunny Goyald33860f2015-04-23 16:02:20 -0700674 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700675 if (sWorkerThread.getThreadId() == Process.myTid()) {
676 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
677 "main thread");
678 }
679
680 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400681 synchronized (mDeferredBindRunnables) {
682 mDeferredBindRunnables.clear();
683 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700684
685 // Remove any queued UI runnables
686 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700687 // Unbind all the workspace items
688 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700689 }
690
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700691 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700692 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700693 // Ensure that we don't use the same workspace items data structure on the main thread
694 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700695 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700696 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700697 tmpItems.addAll(sBgWorkspaceItems);
698 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700699 }
700 Runnable r = new Runnable() {
701 @Override
702 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700703 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700704 item.unbind();
705 }
706 }
707 };
708 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700709 }
710
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 /**
712 * Adds an item to the DB if it was not created previously, or move it to a new
713 * <container, screen, cellX, cellY>
714 */
715 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 if (item.container == ItemInfo.NO_ID) {
718 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700719 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 } else {
721 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 }
724 }
725
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700726 static void checkItemInfoLocked(
727 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
728 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
729 if (modelItem != null && item != modelItem) {
730 // check all the data is consistent
731 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
732 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
733 ShortcutInfo shortcut = (ShortcutInfo) item;
734 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
735 modelShortcut.intent.filterEquals(shortcut.intent) &&
736 modelShortcut.id == shortcut.id &&
737 modelShortcut.itemType == shortcut.itemType &&
738 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700739 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700740 modelShortcut.cellX == shortcut.cellX &&
741 modelShortcut.cellY == shortcut.cellY &&
742 modelShortcut.spanX == shortcut.spanX &&
743 modelShortcut.spanY == shortcut.spanY &&
744 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
745 (modelShortcut.dropPos != null &&
746 shortcut.dropPos != null &&
747 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
748 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
749 // For all intents and purposes, this is the same object
750 return;
751 }
752 }
753
754 // the modelItem needs to match up perfectly with item if our model is
755 // to be consistent with the database-- for now, just require
756 // modelItem == item or the equality check above
757 String msg = "item: " + ((item != null) ? item.toString() : "null") +
758 "modelItem: " +
759 ((modelItem != null) ? modelItem.toString() : "null") +
760 "Error: ItemInfo passed to checkItemInfo doesn't match original";
761 RuntimeException e = new RuntimeException(msg);
762 if (stackTrace != null) {
763 e.setStackTrace(stackTrace);
764 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800765 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700766 }
767 }
768
Michael Jurka816474f2012-06-25 14:49:02 -0700769 static void checkItemInfo(final ItemInfo item) {
770 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
771 final long itemId = item.id;
772 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700773 public void run() {
774 synchronized (sBgLock) {
775 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700776 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700777 }
778 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700779 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700780 }
781
Michael Jurkac9d95c52011-08-29 14:03:34 -0700782 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
783 final ItemInfo item, final String callingFunction) {
784 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700785 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700786 final ContentResolver cr = context.getContentResolver();
787
Adam Cohen487f7dd2012-06-28 18:12:10 -0700788 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700789 Runnable r = new Runnable() {
790 public void run() {
791 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700792 updateItemArrays(item, itemId, stackTrace);
793 }
794 };
795 runOnWorkerThread(r);
796 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700798 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
799 final ArrayList<ItemInfo> items, final String callingFunction) {
800 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700801
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
803 Runnable r = new Runnable() {
804 public void run() {
805 ArrayList<ContentProviderOperation> ops =
806 new ArrayList<ContentProviderOperation>();
807 int count = items.size();
808 for (int i = 0; i < count; i++) {
809 ItemInfo item = items.get(i);
810 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700811 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700812 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700813
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
815 updateItemArrays(item, itemId, stackTrace);
816
817 }
818 try {
819 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
820 } catch (Exception e) {
821 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700822 }
823 }
824 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700825 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700826 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700827
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
829 // Lock on mBgLock *after* the db operation
830 synchronized (sBgLock) {
831 checkItemInfoLocked(itemId, item, stackTrace);
832
833 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
834 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
835 // Item is in a folder, make sure this folder exists
836 if (!sBgFolders.containsKey(item.container)) {
837 // An items container is being set to a that of an item which is not in
838 // the list of Folders.
839 String msg = "item: " + item + " container being set to: " +
840 item.container + ", not in the list of folders";
841 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 }
843 }
844
845 // Items are added/removed from the corresponding FolderInfo elsewhere, such
846 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
847 // that are on the desktop, as appropriate
848 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800849 if (modelItem != null &&
850 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
851 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700852 switch (modelItem.itemType) {
853 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
854 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
855 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
856 if (!sBgWorkspaceItems.contains(modelItem)) {
857 sBgWorkspaceItems.add(modelItem);
858 }
859 break;
860 default:
861 break;
862 }
863 } else {
864 sBgWorkspaceItems.remove(modelItem);
865 }
866 }
867 }
868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700871 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700872 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 item.cellX = cellX;
876 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 // We store hotseat items in canonical form which is this orientation invariant position
879 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700880 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700882 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886
887 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
890 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800891 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700892 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Michael Jurkac9d95c52011-08-29 14:03:34 -0700894 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700895 }
896
897 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700898 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
899 * cellX, cellY have already been updated on the ItemInfos.
900 */
901 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
902 final long container, final int screen) {
903
904 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
905 int count = items.size();
906
907 for (int i = 0; i < count; i++) {
908 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700909 item.container = container;
910
911 // We store hotseat items in canonical form which is this orientation invariant position
912 // in the hotseat
913 if (context instanceof Launcher && screen < 0 &&
914 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700915 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700916 item.cellY);
917 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700919 }
920
921 final ContentValues values = new ContentValues();
922 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
923 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
924 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800925 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700927
928 contentValues.add(values);
929 }
930 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
931 }
932
933 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700934 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800935 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700936 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700938 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800939 item.cellX = cellX;
940 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700941 item.spanX = spanX;
942 item.spanY = spanY;
943
944 // We store hotseat items in canonical form which is this orientation invariant position
945 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700946 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700947 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700948 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700949 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700951 }
Adam Cohend4844c32011-02-18 19:25:06 -0800952
Adam Cohend4844c32011-02-18 19:25:06 -0800953 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800954 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700955 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
956 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800957 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700958 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
959 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700960 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800961
Michael Jurka816474f2012-06-25 14:49:02 -0700962 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700963 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700964
965 /**
966 * Update an item to the database in a specified container.
967 */
968 static void updateItemInDatabase(Context context, final ItemInfo item) {
969 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100970 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700971 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800972 }
973
974 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700975 * Returns true if the shortcuts already exists on the workspace. This must be called after
976 * the workspace has been loaded. We identify a shortcut by its intent.
977 * TODO: Throw exception is above condition is not met.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 */
Sunny Goyal756adbc2015-04-16 15:20:51 -0700979 @Thunk static boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700980 final Intent intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700981 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700982 if (intent.getComponent() != null) {
983 // If component is not null, an intent with null package will produce
984 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700985 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700986 if (intent.getPackage() != null) {
987 intentWithPkg = intent;
988 intentWithoutPkg = new Intent(intent).setPackage(null);
989 } else {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700990 intentWithPkg = new Intent(intent).setPackage(packageName);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700991 intentWithoutPkg = intent;
992 }
993 } else {
994 intentWithPkg = intent;
995 intentWithoutPkg = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700996 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700997 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700998
999 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07001000 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001001 if (item instanceof ShortcutInfo) {
1002 ShortcutInfo info = (ShortcutInfo) item;
1003 if (intentWithPkg.equals(info.getIntent())
1004 || intentWithoutPkg.equals(info.getIntent())) {
1005 return true;
1006 }
1007 }
1008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001010 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001011 }
1012
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 /**
1014 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
1015 */
Sunny Goyale2df0622015-04-24 11:27:00 -07001016 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 final ContentResolver cr = context.getContentResolver();
1018 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
1019 "_id=? and (itemType=? or itemType=?)",
1020 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -07001021 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001022
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 try {
1024 if (c.moveToFirst()) {
1025 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1026 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1027 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1028 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1029 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1030 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001031 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 FolderInfo folderInfo = null;
1034 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001035 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1036 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001038 }
1039
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 folderInfo.title = c.getString(titleIndex);
1041 folderInfo.id = id;
1042 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -07001043 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001044 folderInfo.cellX = c.getInt(cellXIndex);
1045 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001046 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047
1048 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 } finally {
1051 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001052 }
1053
1054 return null;
1055 }
1056
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 /**
1058 * Add an item to the database in a specified container. Sets the container, screen, cellX and
1059 * cellY fields of the item. Also assigns an ID to the item.
1060 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001061 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001062 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 item.cellX = cellX;
1065 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001066 // We store hotseat items in canonical form which is this orientation invariant position
1067 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001072 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074
1075 final ContentValues values = new ContentValues();
1076 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001077 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001078
Michael Jurka414300a2013-08-27 15:42:35 +02001079 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001080 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001081
Jason Monk8e19cf22014-03-20 15:06:57 -04001082 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001083 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001084 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001085 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001087 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001088 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001089 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001090 sBgItemsIdMap.put(item.id, item);
1091 switch (item.itemType) {
1092 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1093 sBgFolders.put(item.id, (FolderInfo) item);
1094 // Fall through
1095 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1096 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1097 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1098 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1099 sBgWorkspaceItems.add(item);
1100 } else {
1101 if (!sBgFolders.containsKey(item.container)) {
1102 // Adding an item to a folder that doesn't exist.
1103 String msg = "adding item: " + item + " to a folder that " +
1104 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001105 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001107 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 break;
1109 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1110 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1111 break;
1112 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001113 }
1114 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001115 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001117 }
1118
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001120 * Creates a new unique child id, for a given cell span across all layouts.
1121 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001122 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001124 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001125 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001126 }
1127
Sunny Goyal34942622014-08-29 17:20:55 -07001128 private static ArrayList<ItemInfo> getItemsByPackageName(
1129 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001130 ItemInfoFilter filter = new ItemInfoFilter() {
1131 @Override
1132 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1133 return cn.getPackageName().equals(pn) && info.user.equals(user);
1134 }
1135 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001136 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001137 }
1138
1139 /**
1140 * Removes all the items from the database corresponding to the specified package.
1141 */
1142 static void deletePackageFromDatabase(Context context, final String pn,
1143 final UserHandleCompat user) {
1144 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001145 }
1146
1147 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001148 * Removes the specified item from the database
1149 * @param context
1150 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001152 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001153 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1154 items.add(item);
1155 deleteItemsFromDatabase(context, items);
1156 }
1157
1158 /**
1159 * Removes the specified items from the database
1160 * @param context
1161 * @param item
1162 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001163 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001165 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001166 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001167 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001168 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001169 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001170
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001171 // Lock on mBgLock *after* the db operation
1172 synchronized (sBgLock) {
1173 switch (item.itemType) {
1174 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1175 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001176 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001177 if (info.container == item.id) {
1178 // We are deleting a folder which still contains items that
1179 // think they are contained by that folder.
1180 String msg = "deleting a folder (" + item + ") which still " +
1181 "contains items (" + info + ")";
1182 Log.e(TAG, msg);
1183 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001185 sBgWorkspaceItems.remove(item);
1186 break;
1187 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1188 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1189 sBgWorkspaceItems.remove(item);
1190 break;
1191 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1192 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1193 break;
1194 }
1195 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001197 }
1198 }
Michael Jurka83df1882011-08-31 20:59:26 -07001199 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202
1203 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001204 * Update the order of the workspace screens in the database. The array list contains
1205 * a list of screen ids in the order that they should appear.
1206 */
Winson Chungc9168342013-06-26 14:54:55 -07001207 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001208 // Log to disk
1209 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1210 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1211
Winson Chung64359a52013-07-08 17:17:08 -07001212 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001213 final ContentResolver cr = context.getContentResolver();
1214 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1215
1216 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001217 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001218 while (iter.hasNext()) {
1219 long id = iter.next();
1220 if (id < 0) {
1221 iter.remove();
1222 }
1223 }
1224
1225 Runnable r = new Runnable() {
1226 @Override
1227 public void run() {
Yura085c8532014-02-11 15:15:29 +00001228 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001229 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001230 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001231 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001232 for (int i = 0; i < count; i++) {
1233 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001234 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001235 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1236 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001237 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001238 }
Yura085c8532014-02-11 15:15:29 +00001239
1240 try {
1241 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1242 } catch (Exception ex) {
1243 throw new RuntimeException(ex);
1244 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001245
Winson Chungba9c37f2013-08-30 14:11:37 -07001246 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001247 sBgWorkspaceScreens.clear();
1248 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001249 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001250 }
1251 };
1252 runOnWorkerThread(r);
1253 }
1254
1255 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 * Remove the contents of the specified folder from the database
1257 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001258 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259 final ContentResolver cr = context.getContentResolver();
1260
Michael Jurkac9d95c52011-08-29 14:03:34 -07001261 Runnable r = new Runnable() {
1262 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001263 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001264 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001265 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001266 sBgItemsIdMap.remove(info.id);
1267 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001268 sBgWorkspaceItems.remove(info);
1269 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001270
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001271 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001272 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001273 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001274 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001275 for (ItemInfo childInfo : info.contents) {
1276 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001277 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001278 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001279 }
1280 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001281 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 }
1283
1284 /**
1285 * Set this as the current Launcher activity object for the loader.
1286 */
1287 public void initialize(Callbacks callbacks) {
1288 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001289 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1290 // workspace to prevent leaking Launcher activities on orientation change.
1291 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 mCallbacks = new WeakReference<Callbacks>(callbacks);
1293 }
1294 }
1295
Kenny Guyed131872014-04-30 03:02:21 +01001296 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001297 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001298 int op = PackageUpdatedTask.OP_UPDATE;
1299 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1300 user));
1301 }
1302
1303 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001304 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001305 int op = PackageUpdatedTask.OP_REMOVE;
1306 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1307 user));
1308 }
1309
1310 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001311 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001312 int op = PackageUpdatedTask.OP_ADD;
1313 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1314 user));
1315 }
1316
1317 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001318 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001319 boolean replacing) {
1320 if (!replacing) {
1321 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1322 user));
1323 if (mAppsCanBeOnRemoveableStorage) {
1324 // Only rebind if we support removable storage. It catches the
1325 // case where
1326 // apps on the external sd card need to be reloaded
1327 startLoaderFromBackground();
1328 }
1329 } else {
1330 // If we are replacing then just update the packages in the list
1331 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1332 packageNames, user));
1333 }
1334 }
1335
1336 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001337 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001338 boolean replacing) {
1339 if (!replacing) {
1340 enqueuePackageUpdated(new PackageUpdatedTask(
1341 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1342 user));
1343 }
Kenny Guyed131872014-04-30 03:02:21 +01001344 }
1345
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001346 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1348 * ACTION_PACKAGE_CHANGED.
1349 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001350 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001351 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001352 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001353
Joe Onorato36115782010-06-17 13:28:48 -04001354 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001355 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001356 // If we have changed locale we need to clear out the labels in all apps/workspace.
1357 forceReload();
1358 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1359 // Check if configuration change was an mcc/mnc change which would affect app resources
1360 // and we would need to clear out the labels in all apps/workspace. Same handling as
1361 // above for ACTION_LOCALE_CHANGED
1362 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001363 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001364 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001365 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001366 forceReload();
1367 }
1368 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001369 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001370 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1371 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001372 Callbacks callbacks = getCallback();
1373 if (callbacks != null) {
1374 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001375 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001376 }
1377 }
1378
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001379 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001380 resetLoadedState(true, true);
1381
Reena Lee93f824a2011-09-23 17:20:28 -07001382 // Do this here because if the launcher activity is running it will be restarted.
1383 // If it's not running startLoaderFromBackground will merely tell it that it needs
1384 // to reload.
1385 startLoaderFromBackground();
1386 }
1387
Winson Chungf0c6ae02012-03-21 16:10:31 -07001388 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1389 synchronized (mLock) {
1390 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1391 // mWorkspaceLoaded to true later
1392 stopLoaderLocked();
1393 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1394 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1395 }
1396 }
1397
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001398 /**
1399 * When the launcher is in the background, it's possible for it to miss paired
1400 * configuration changes. So whenever we trigger the loader from the background
1401 * tell the launcher that it needs to re-run the loader when it comes back instead
1402 * of doing it now.
1403 */
1404 public void startLoaderFromBackground() {
1405 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001406 Callbacks callbacks = getCallback();
1407 if (callbacks != null) {
1408 // Only actually run the loader if they're not paused.
1409 if (!callbacks.setLoadOnResume()) {
1410 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001411 }
1412 }
1413 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001414 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001417
Reena Lee93f824a2011-09-23 17:20:28 -07001418 // If there is already a loader task running, tell it to stop.
1419 // returns true if isLaunching() was true on the old task
1420 private boolean stopLoaderLocked() {
1421 boolean isLaunching = false;
1422 LoaderTask oldTask = mLoaderTask;
1423 if (oldTask != null) {
1424 if (oldTask.isLaunching()) {
1425 isLaunching = true;
1426 }
1427 oldTask.stopLocked();
1428 }
1429 return isLaunching;
1430 }
1431
Adam Cohen1a85c582014-09-30 09:48:49 -07001432 public boolean isCurrentCallbacks(Callbacks callbacks) {
1433 return (mCallbacks != null && mCallbacks.get() == callbacks);
1434 }
1435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001436 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001437 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1438 }
1439
1440 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001441 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1442 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001443 synchronized (mLock) {
1444 if (DEBUG_LOADERS) {
1445 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1446 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 // Clear any deferred bind-runnables from the synchronized load process
1449 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001450 synchronized (mDeferredBindRunnables) {
1451 mDeferredBindRunnables.clear();
1452 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453
Joe Onorato36115782010-06-17 13:28:48 -04001454 // Don't bother to start the thread if we know it's not going to do anything
1455 if (mCallbacks != null && mCallbacks.get() != null) {
1456 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001457 // also, don't downgrade isLaunching if we're already running
1458 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001459 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001460 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1461 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001462 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1463 } else {
1464 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1465 sWorker.post(mLoaderTask);
1466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 }
1469 }
1470
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 void bindRemainingSynchronousPages() {
1472 // Post the remaining side pages to be loaded
1473 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001474 Runnable[] deferredBindRunnables = null;
1475 synchronized (mDeferredBindRunnables) {
1476 deferredBindRunnables = mDeferredBindRunnables.toArray(
1477 new Runnable[mDeferredBindRunnables.size()]);
1478 mDeferredBindRunnables.clear();
1479 }
1480 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001481 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483 }
1484 }
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 public void stopLoader() {
1487 synchronized (mLock) {
1488 if (mLoaderTask != null) {
1489 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
1491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001494 /**
1495 * Loads the workspace screen ids in an ordered list.
1496 */
Adam Cohen091440a2015-03-18 14:16:05 -07001497 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001498 final ContentResolver contentResolver = context.getContentResolver();
1499 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001500
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001501 // Get screens ordered by rank.
1502 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1503 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1504 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001505 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001506 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001507 while (sc.moveToNext()) {
1508 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001509 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001510 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001511 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1512 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001513 }
1514 }
1515 } finally {
1516 sc.close();
1517 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001518 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001519 }
1520
Michael Jurkac57b7a82011-08-09 22:02:20 -07001521 public boolean isAllAppsLoaded() {
1522 return mAllAppsLoaded;
1523 }
1524
Winson Chung36a62fe2012-05-06 18:04:42 -07001525 boolean isLoadingWorkspace() {
1526 synchronized (mLock) {
1527 if (mLoaderTask != null) {
1528 return mLoaderTask.isLoadingWorkspace();
1529 }
1530 }
1531 return false;
1532 }
1533
Joe Onorato36115782010-06-17 13:28:48 -04001534 /**
1535 * Runnable for the thread that loads the contents of the launcher:
1536 * - workspace icons
1537 * - widgets
1538 * - all apps icons
1539 */
1540 private class LoaderTask implements Runnable {
1541 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001542 private boolean mIsLaunching;
Adam Cohen091440a2015-03-18 14:16:05 -07001543 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001544 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001545 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001546 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001547
Dan Sandlerd5024042014-01-09 15:01:33 -05001548 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001549 mContext = context;
1550 mIsLaunching = isLaunching;
Dan Sandlerd5024042014-01-09 15:01:33 -05001551 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 }
1553
Joe Onorato36115782010-06-17 13:28:48 -04001554 boolean isLaunching() {
1555 return mIsLaunching;
1556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557
Winson Chung36a62fe2012-05-06 18:04:42 -07001558 boolean isLoadingWorkspace() {
1559 return mIsLoadingAndBindingWorkspace;
1560 }
1561
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001562 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001563 mIsLoadingAndBindingWorkspace = true;
1564
Joe Onorato36115782010-06-17 13:28:48 -04001565 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001566 if (DEBUG_LOADERS) {
1567 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001569
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001570 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001571 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001572 synchronized (LoaderTask.this) {
1573 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001574 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001575 }
1576 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 }
1578 }
1579
Joe Onorato36115782010-06-17 13:28:48 -04001580 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001581 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001582 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001583
Joe Onorato36115782010-06-17 13:28:48 -04001584 private void waitForIdle() {
1585 // Wait until the either we're stopped or the other threads are done.
1586 // This way we don't start loading all apps until the workspace has settled
1587 // down.
1588 synchronized (LoaderTask.this) {
1589 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001590
Joe Onorato36115782010-06-17 13:28:48 -04001591 mHandler.postIdle(new Runnable() {
1592 public void run() {
1593 synchronized (LoaderTask.this) {
1594 mLoadAndBindStepFinished = true;
1595 if (DEBUG_LOADERS) {
1596 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001597 }
Joe Onorato36115782010-06-17 13:28:48 -04001598 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001599 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001600 }
Joe Onorato36115782010-06-17 13:28:48 -04001601 });
1602
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001603 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001604 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001605 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1606 // wait no longer than 1sec at a time
1607 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001608 } catch (InterruptedException ex) {
1609 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001610 }
1611 }
Joe Onorato36115782010-06-17 13:28:48 -04001612 if (DEBUG_LOADERS) {
1613 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001614 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001615 + "ms for previous step to finish binding");
1616 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001617 }
Joe Onorato36115782010-06-17 13:28:48 -04001618 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001619
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001621 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001622 // Ensure that we have a valid page index to load synchronously
1623 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1624 "valid page index");
1625 }
1626 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1627 // Ensure that we don't try and bind a specified page when the pages have not been
1628 // loaded already (we should load everything asynchronously in that case)
1629 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1630 }
1631 synchronized (mLock) {
1632 if (mIsLoaderTaskRunning) {
1633 // Ensure that we are never running the background loading at this point since
1634 // we also touch the background collections
1635 throw new RuntimeException("Error! Background loading is already running");
1636 }
1637 }
1638
1639 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1640 // data structures, we can't allow any other thread to touch that data, but because
1641 // this call is synchronous, we can get away with not locking).
1642
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001643 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001644 // operations from the previous activity. We need to ensure that all queued operations
1645 // are executed before any synchronous binding work is done.
1646 mHandler.flush();
1647
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001648 // Divide the set of loaded items into those that we are binding synchronously, and
1649 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001650 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001651 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1652 // arise from that.
1653 onlyBindAllApps();
1654 }
1655
Joe Onorato36115782010-06-17 13:28:48 -04001656 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 synchronized (mLock) {
1658 mIsLoaderTaskRunning = true;
1659 }
Joe Onorato36115782010-06-17 13:28:48 -04001660 // Optimize for end-user experience: if the Launcher is up and // running with the
1661 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1662 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001663 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001664 // Elevate priority when Home launches for the first time to avoid
1665 // starving at boot time. Staring at a blank home is not cool.
1666 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001667 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1668 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001669 android.os.Process.setThreadPriority(mIsLaunching
1670 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1671 }
Winson Chung64359a52013-07-08 17:17:08 -07001672 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001673 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001674
Joe Onorato36115782010-06-17 13:28:48 -04001675 if (mStopped) {
1676 break keep_running;
1677 }
1678
1679 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1680 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001681 synchronized (mLock) {
1682 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001683 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001684 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1685 }
1686 }
Joe Onorato36115782010-06-17 13:28:48 -04001687 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001688
1689 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001690 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1691 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001692
Sunny Goyal5b0e6692015-03-19 14:31:19 -07001693 // Remove entries for packages which changed while the launcher was dead.
1694 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews();
1695
Winson Chung7ed37742011-09-08 15:45:51 -07001696 // Restore the default thread priority after we are done loading items
1697 synchronized (mLock) {
1698 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1699 }
Joe Onorato36115782010-06-17 13:28:48 -04001700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701
Joe Onorato36115782010-06-17 13:28:48 -04001702 // Clear out this reference, otherwise we end up holding it until all of the
1703 // callback runnables are done.
1704 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705
Joe Onorato36115782010-06-17 13:28:48 -04001706 synchronized (mLock) {
1707 // If we are still the last one to be scheduled, remove ourselves.
1708 if (mLoaderTask == this) {
1709 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001711 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001712 }
Joe Onorato36115782010-06-17 13:28:48 -04001713 }
1714
1715 public void stopLocked() {
1716 synchronized (LoaderTask.this) {
1717 mStopped = true;
1718 this.notify();
1719 }
1720 }
1721
1722 /**
1723 * Gets the callbacks object. If we've been stopped, or if the launcher object
1724 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1725 * object that was around when the deferred message was scheduled, and if there's
1726 * a new Callbacks object around then also return null. This will save us from
1727 * calling onto it with data that will be ignored.
1728 */
1729 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1730 synchronized (mLock) {
1731 if (mStopped) {
1732 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001733 }
Joe Onorato36115782010-06-17 13:28:48 -04001734
1735 if (mCallbacks == null) {
1736 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001737 }
Joe Onorato36115782010-06-17 13:28:48 -04001738
1739 final Callbacks callbacks = mCallbacks.get();
1740 if (callbacks != oldCallbacks) {
1741 return null;
1742 }
1743 if (callbacks == null) {
1744 Log.w(TAG, "no mCallbacks");
1745 return null;
1746 }
1747
1748 return callbacks;
1749 }
1750 }
1751
1752 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001753 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001754 LauncherAppState app = LauncherAppState.getInstance();
1755 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001756 final int countX = (int) grid.numColumns;
1757 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001758
Adam Cohendcd297f2013-06-18 13:13:40 -07001759 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001760 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001761 // Return early if we detect that an item is under the hotseat button
1762 if (mCallbacks == null ||
1763 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001764 Log.e(TAG, "Error loading shortcut into hotseat " + item
1765 + " into position (" + item.screenId + ":" + item.cellX + ","
1766 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001767 return false;
1768 }
1769
Dan Sandler295ae182013-12-10 16:05:47 -05001770 final ItemInfo[][] hotseatItems =
1771 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1772
Adam Cohenae4409d2013-11-26 10:34:59 -08001773 if (item.screenId >= grid.numHotseatIcons) {
1774 Log.e(TAG, "Error loading shortcut " + item
1775 + " into hotseat position " + item.screenId
1776 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1777 + ")");
1778 return false;
1779 }
1780
Dan Sandler295ae182013-12-10 16:05:47 -05001781 if (hotseatItems != null) {
1782 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 Log.e(TAG, "Error loading shortcut into hotseat " + item
1784 + " into position (" + item.screenId + ":" + item.cellX + ","
1785 + item.cellY + ") occupied by "
1786 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1787 [(int) item.screenId][0]);
1788 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001789 } else {
1790 hotseatItems[(int) item.screenId][0] = item;
1791 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001792 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001793 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001794 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001795 items[(int) item.screenId][0] = item;
1796 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001797 return true;
1798 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001799 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1800 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001801 return true;
1802 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001803
Adam Cohendcd297f2013-06-18 13:13:40 -07001804 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001805 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001806 occupied.put(item.screenId, items);
1807 }
1808
Dan Sandler295ae182013-12-10 16:05:47 -05001809 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001810 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1811 item.cellX < 0 || item.cellY < 0 ||
1812 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1813 Log.e(TAG, "Error loading shortcut " + item
1814 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1815 + item.cellX + "," + item.cellY
1816 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1817 return false;
1818 }
1819
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001820 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001821 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1822 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001823 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001824 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001825 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001826 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001827 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001828 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001829 return false;
1830 }
1831 }
1832 }
1833 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1834 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001835 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001836 }
1837 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001838
Joe Onorato36115782010-06-17 13:28:48 -04001839 return true;
1840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841
Winson Chungba9c37f2013-08-30 14:11:37 -07001842 /** Clears all the sBg data structures */
1843 private void clearSBgDataStructures() {
1844 synchronized (sBgLock) {
1845 sBgWorkspaceItems.clear();
1846 sBgAppWidgets.clear();
1847 sBgFolders.clear();
1848 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001849 sBgWorkspaceScreens.clear();
1850 }
1851 }
1852
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001853 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001854 // Log to disk
1855 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1856
Joe Onorato36115782010-06-17 13:28:48 -04001857 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858
Joe Onorato36115782010-06-17 13:28:48 -04001859 final Context context = mContext;
1860 final ContentResolver contentResolver = context.getContentResolver();
1861 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001862 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001863 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1864 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001865 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001866
Winson Chung892c74d2013-08-22 16:15:50 -07001867 LauncherAppState app = LauncherAppState.getInstance();
1868 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1869 int countX = (int) grid.numColumns;
1870 int countY = (int) grid.numRows;
1871
Dan Sandlerd5024042014-01-09 15:01:33 -05001872 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1873 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1874 LauncherAppState.getLauncherProvider().deleteDatabase();
1875 }
1876
1877 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1878 // append the user's Launcher2 shortcuts
1879 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1880 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1881 } else {
1882 // Make sure the default workspace is loaded
1883 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001884 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001885 }
Adam Cohene25af792013-06-06 23:08:25 -07001886
Winson Chung2abf94d2012-07-18 18:16:38 -07001887 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001888 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001889 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001890 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001891
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001893 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001894 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001895 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001896 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001897
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 // +1 for the hotseat (it can be larger than the workspace)
1899 // Load workspace in reverse order to ensure that latest items are loaded first (and
1900 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001901 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001902
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903 try {
1904 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1905 final int intentIndex = c.getColumnIndexOrThrow
1906 (LauncherSettings.Favorites.INTENT);
1907 final int titleIndex = c.getColumnIndexOrThrow
1908 (LauncherSettings.Favorites.TITLE);
1909 final int iconTypeIndex = c.getColumnIndexOrThrow(
1910 LauncherSettings.Favorites.ICON_TYPE);
1911 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1912 final int iconPackageIndex = c.getColumnIndexOrThrow(
1913 LauncherSettings.Favorites.ICON_PACKAGE);
1914 final int iconResourceIndex = c.getColumnIndexOrThrow(
1915 LauncherSettings.Favorites.ICON_RESOURCE);
1916 final int containerIndex = c.getColumnIndexOrThrow(
1917 LauncherSettings.Favorites.CONTAINER);
1918 final int itemTypeIndex = c.getColumnIndexOrThrow(
1919 LauncherSettings.Favorites.ITEM_TYPE);
1920 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1921 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001922 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1923 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 final int screenIndex = c.getColumnIndexOrThrow(
1925 LauncherSettings.Favorites.SCREEN);
1926 final int cellXIndex = c.getColumnIndexOrThrow
1927 (LauncherSettings.Favorites.CELLX);
1928 final int cellYIndex = c.getColumnIndexOrThrow
1929 (LauncherSettings.Favorites.CELLY);
1930 final int spanXIndex = c.getColumnIndexOrThrow
1931 (LauncherSettings.Favorites.SPANX);
1932 final int spanYIndex = c.getColumnIndexOrThrow(
1933 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001934 final int rankIndex = c.getColumnIndexOrThrow(
1935 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001936 final int restoredIndex = c.getColumnIndexOrThrow(
1937 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001938 final int profileIdIndex = c.getColumnIndexOrThrow(
1939 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001940 final int optionsIndex = c.getColumnIndexOrThrow(
1941 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942
Sunny Goyal7f834d22015-04-21 10:10:23 -07001943 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1944 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1945 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1946 }
1947
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 ShortcutInfo info;
1949 String intentDescription;
1950 LauncherAppWidgetInfo appWidgetInfo;
1951 int container;
1952 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001953 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001954 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001955 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 while (!mStopped && c.moveToNext()) {
1958 try {
1959 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001960 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001961 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001962
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 switch (itemType) {
1964 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1965 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001966 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001968 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001969 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001970 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001971 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001972 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001973 if (user == null) {
1974 // User has been deleted remove the item.
1975 itemsToRemove.add(id);
1976 continue;
1977 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 try {
1979 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001980 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001981 if (cn != null && cn.getPackageName() != null) {
1982 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1983 cn.getPackageName(), user);
1984 boolean validComponent = validPkg &&
1985 launcherApps.isActivityEnabledForProfile(cn, user);
1986
1987 if (validComponent) {
1988 if (restored) {
1989 // no special handling necessary for this item
1990 restoredRows.add(id);
1991 restored = false;
1992 }
1993 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001994 intent = null;
1995 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1996 // We allow auto install apps to have their intent
1997 // updated after an install.
1998 intent = manager.getLaunchIntentForPackage(
1999 cn.getPackageName());
2000 if (intent != null) {
2001 ContentValues values = new ContentValues();
2002 values.put(LauncherSettings.Favorites.INTENT,
2003 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002004 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07002005 }
2006 }
2007
2008 if (intent == null) {
2009 // The app is installed but the component is no
2010 // longer available.
2011 Launcher.addDumpLog(TAG,
2012 "Invalid component removed: " + cn, true);
2013 itemsToRemove.add(id);
2014 continue;
2015 } else {
2016 // no special handling necessary for this item
2017 restoredRows.add(id);
2018 restored = false;
2019 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002020 } else if (restored) {
2021 // Package is not yet available but might be
2022 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05002023 Launcher.addDumpLog(TAG,
2024 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002025
2026 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
2027 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002028 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07002029 // App restore has started. Update the flag
2030 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
2031 ContentValues values = new ContentValues();
2032 values.put(LauncherSettings.Favorites.RESTORED,
2033 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002034 updateItem(id, values);
2035 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
2036 // This is a common app. Try to replace this.
2037 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
2038 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
2039 if (parser.findDefaultApp()) {
2040 // Default app found. Replace it.
2041 intent = parser.parsedIntent;
2042 cn = intent.getComponent();
2043 ContentValues values = parser.parsedValues;
2044 values.put(LauncherSettings.Favorites.RESTORED, 0);
2045 updateItem(id, values);
2046 restored = false;
2047 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07002048
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002049 } else if (REMOVE_UNRESTORED_ICONS) {
2050 Launcher.addDumpLog(TAG,
2051 "Unrestored package removed: " + cn, true);
2052 itemsToRemove.add(id);
2053 continue;
2054 }
Sunny Goyal94485362014-09-18 16:13:58 -07002055 } else if (REMOVE_UNRESTORED_ICONS) {
2056 Launcher.addDumpLog(TAG,
2057 "Unrestored package removed: " + cn, true);
2058 itemsToRemove.add(id);
2059 continue;
2060 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002061 } else if (launcherApps.isAppEnabled(
2062 manager, cn.getPackageName(),
2063 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2064 // Package is present but not available.
2065 allowMissingTarget = true;
2066 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2067 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002068 // SdCard is not ready yet. Package might get available,
2069 // once it is ready.
2070 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2071 + " (check again later)", true);
2072 HashSet<String> pkgs = sPendingPackages.get(user);
2073 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002074 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002075 sPendingPackages.put(user, pkgs);
2076 }
2077 pkgs.add(cn.getPackageName());
2078 allowMissingTarget = true;
2079 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002080
2081 } else {
2082 // Do not wait for external media load anymore.
2083 // Log the invalid package, and remove it
2084 Launcher.addDumpLog(TAG,
2085 "Invalid package removed: " + cn, true);
2086 itemsToRemove.add(id);
2087 continue;
Winson Chungee055712013-07-30 14:46:24 -07002088 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002089 } else if (cn == null) {
2090 // For shortcuts with no component, keep them as they are
2091 restoredRows.add(id);
2092 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002093 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002095 Launcher.addDumpLog(TAG,
2096 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 continue;
2098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099
Sunny Goyal34b65272015-03-11 16:56:52 -07002100 container = c.getInt(containerIndex);
2101 boolean useLowResIcon = container >= 0 &&
2102 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2103
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002104 if (itemReplaced) {
2105 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002106 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002107 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002108 } else {
2109 // Don't replace items for other profiles.
2110 itemsToRemove.add(id);
2111 continue;
2112 }
2113 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002114 if (user.equals(UserHandleCompat.myUserHandle())) {
2115 Launcher.addDumpLog(TAG,
2116 "constructing info for partially restored package",
2117 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002118 info = getRestoredItemInfo(c, titleIndex, intent,
2119 promiseType, useLowResIcon);
Kenny Guyed131872014-04-30 03:02:21 +01002120 intent = getRestoredItemIntent(c, context, intent);
2121 } else {
2122 // Don't restore items for other profiles.
2123 itemsToRemove.add(id);
2124 continue;
2125 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002126 } else if (itemType ==
2127 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002128 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002129 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 } else {
2131 info = getShortcutInfo(c, context, iconTypeIndex,
2132 iconPackageIndex, iconResourceIndex, iconIndex,
2133 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002134
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002135 // App shortcuts that used to be automatically added to Launcher
2136 // didn't always have the correct intent flags set, so do that
2137 // here
2138 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002139 intent.getCategories() != null &&
2140 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002141 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 intent.addFlags(
2143 Intent.FLAG_ACTIVITY_NEW_TASK |
2144 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2145 }
Michael Jurka96879562012-03-22 05:54:33 -07002146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002149 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002152 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 info.cellX = c.getInt(cellXIndex);
2154 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002155 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002156 info.spanX = 1;
2157 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002158 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002159 info.isDisabled = disabledState;
2160 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2161 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2162 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002163
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002165 if (!checkItemPlacement(occupied, info)) {
2166 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 break;
2168 }
2169
Sunny Goyal756adbc2015-04-16 15:20:51 -07002170 if (restored) {
2171 ComponentName cn = info.getTargetComponent();
2172 if (cn != null) {
2173 Integer progress = installingPkgs.get(cn.getPackageName());
2174 if (progress != null) {
2175 info.setInstallProgress(progress);
2176 } else {
2177 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2178 }
2179 }
2180 }
2181
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 switch (container) {
2183 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2184 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2185 sBgWorkspaceItems.add(info);
2186 break;
2187 default:
2188 // Item is in a user folder
2189 FolderInfo folderInfo =
2190 findOrMakeFolder(sBgFolders, container);
2191 folderInfo.add(info);
2192 break;
2193 }
2194 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002195 } else {
2196 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 }
2198 break;
2199
2200 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2201 id = c.getLong(idIndex);
2202 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2203
2204 folderInfo.title = c.getString(titleIndex);
2205 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 folderInfo.cellX = c.getInt(cellXIndex);
2210 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002211 folderInfo.spanX = 1;
2212 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002213 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214
Daniel Sandler8802e962010-05-26 16:28:16 -04002215 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002216 if (!checkItemPlacement(occupied, folderInfo)) {
2217 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002218 break;
2219 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002220
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2223 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2224 sBgWorkspaceItems.add(folderInfo);
2225 break;
Joe Onorato36115782010-06-17 13:28:48 -04002226 }
Joe Onorato17a89222011-02-08 17:26:11 -08002227
Chris Wrenf4d08112014-01-16 18:13:56 -05002228 if (restored) {
2229 // no special handling required for restored folders
2230 restoredRows.add(id);
2231 }
2232
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2234 sBgFolders.put(folderInfo.id, folderInfo);
2235 break;
2236
2237 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002238 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002240 boolean customWidget = itemType ==
2241 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2242
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002244 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002245 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002246 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002247 user = allUsers.get(serialNumber);
2248 if (user == null) {
2249 itemsToRemove.add(id);
2250 continue;
2251 }
2252
Sunny Goyalff572272014-07-23 13:58:07 -07002253 final ComponentName component =
2254 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002255
Sunny Goyal651077b2014-06-30 14:15:31 -07002256 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002257 final boolean isIdValid = (restoreStatus &
2258 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002259 final boolean wasProviderReady = (restoreStatus &
2260 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002261
Adam Cohen59400422014-03-05 18:07:04 -08002262 final LauncherAppWidgetProviderInfo provider =
2263 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002264 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002265 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002266
2267 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002268 if (!isSafeMode && !customWidget &&
2269 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002270 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002271 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002272
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002274 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 itemsToRemove.add(id);
2276 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002277 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002278 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2279 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002280
2281 if (!customWidget) {
2282 int[] minSpan =
2283 Launcher.getMinSpanForWidget(context, provider);
2284 appWidgetInfo.minSpanX = minSpan[0];
2285 appWidgetInfo.minSpanY = minSpan[1];
2286 }
Sunny Goyalff572272014-07-23 13:58:07 -07002287
2288 int status = restoreStatus;
2289 if (!wasProviderReady) {
2290 // If provider was not previously ready, update the
2291 // status and UI flag.
2292
2293 // Id would be valid only if the widget restore broadcast was received.
2294 if (isIdValid) {
2295 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2296 } else {
2297 status &= ~LauncherAppWidgetInfo
2298 .FLAG_PROVIDER_NOT_READY;
2299 }
2300 }
2301 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002302 } else {
2303 Log.v(TAG, "Widget restore pending id=" + id
2304 + " appWidgetId=" + appWidgetId
2305 + " status =" + restoreStatus);
2306 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002307 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002308 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002309 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002310
2311 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2312 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002313 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002314 // App restore has started. Update the flag
2315 appWidgetInfo.restoreStatus |=
2316 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002317 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002318 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002319 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002320 itemsToRemove.add(id);
2321 continue;
2322 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002323
2324 appWidgetInfo.installProgress =
2325 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002326 }
Sunny Goyalff572272014-07-23 13:58:07 -07002327
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 appWidgetInfo.cellX = c.getInt(cellXIndex);
2331 appWidgetInfo.cellY = c.getInt(cellYIndex);
2332 appWidgetInfo.spanX = c.getInt(spanXIndex);
2333 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002334
Adam Cohen59400422014-03-05 18:07:04 -08002335 if (!customWidget) {
2336 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2337 appWidgetInfo.minSpanX = minSpan[0];
2338 appWidgetInfo.minSpanY = minSpan[1];
2339 }
2340
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 container = c.getInt(containerIndex);
2342 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2343 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2344 Log.e(TAG, "Widget found where container != " +
2345 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2346 continue;
2347 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348
Adam Cohene25af792013-06-06 23:08:25 -07002349 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002351 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2352 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 break;
2354 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002355
Adam Cohen59400422014-03-05 18:07:04 -08002356 if (!customWidget) {
2357 String providerName =
2358 appWidgetInfo.providerName.flattenToString();
2359 if (!providerName.equals(savedProvider) ||
2360 (appWidgetInfo.restoreStatus != restoreStatus)) {
2361 ContentValues values = new ContentValues();
2362 values.put(
2363 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2364 providerName);
2365 values.put(LauncherSettings.Favorites.RESTORED,
2366 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002367 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002368 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002369 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2371 sBgAppWidgets.add(appWidgetInfo);
2372 }
Joe Onorato36115782010-06-17 13:28:48 -04002373 break;
2374 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002376 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002377 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 }
2379 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002380 if (c != null) {
2381 c.close();
2382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 }
2384
Winson Chungba9c37f2013-08-30 14:11:37 -07002385 // Break early if we've stopped loading
2386 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002387 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002388 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002389 }
2390
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 if (itemsToRemove.size() > 0) {
2392 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002393 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 // Remove dead items
2395 for (long id : itemsToRemove) {
2396 if (DEBUG_LOADERS) {
2397 Log.d(TAG, "Removed id = " + id);
2398 }
2399 // Don't notify content observers
2400 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002401 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 } catch (RemoteException e) {
2403 Log.w(TAG, "Could not remove id = " + id);
2404 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002405 }
2406 }
2407
Chris Wrenf4d08112014-01-16 18:13:56 -05002408 if (restoredRows.size() > 0) {
2409 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002410 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002411 // Update restored items that no longer require special handling
2412 try {
2413 StringBuilder selectionBuilder = new StringBuilder();
2414 selectionBuilder.append(LauncherSettings.Favorites._ID);
2415 selectionBuilder.append(" IN (");
2416 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2417 selectionBuilder.append(")");
2418 ContentValues values = new ContentValues();
2419 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002420 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002421 values, selectionBuilder.toString(), null);
2422 } catch (RemoteException e) {
2423 Log.w(TAG, "Could not update restored rows");
2424 }
2425 }
2426
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002427 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2428 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002429 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002430 null, sWorker);
2431 }
2432
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002433 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2434 // Log to disk
2435 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2436 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002437
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002438 // Remove any empty screens
2439 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002440 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002441 long screenId = item.screenId;
2442 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2443 unusedScreens.contains(screenId)) {
2444 unusedScreens.remove(screenId);
2445 }
2446 }
2447
2448 // If there are any empty screens remove them, and update.
2449 if (unusedScreens.size() != 0) {
2450 // Log to disk
2451 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2452 TextUtils.join(", ", unusedScreens), true);
2453
2454 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002456 }
2457
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 if (DEBUG_LOADERS) {
2459 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2460 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002462 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002464
Sunny Goyale2df0622015-04-24 11:27:00 -07002465 for (int i = 0; i < nScreens; i++) {
2466 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002467 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 line += " | ";
2469 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002470 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002471 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002472 if (x < screen.length && y < screen[x].length) {
2473 line += (screen[x][y] != null) ? "#" : ".";
2474 } else {
2475 line += "!";
2476 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 }
Joe Onorato36115782010-06-17 13:28:48 -04002478 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002481 }
Joe Onorato36115782010-06-17 13:28:48 -04002482 }
Adam Cohene25af792013-06-06 23:08:25 -07002483 }
2484
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002485 /**
2486 * Partially updates the item without any notification. Must be called on the worker thread.
2487 */
2488 private void updateItem(long itemId, ContentValues update) {
2489 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002490 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002491 update,
2492 BaseColumns._ID + "= ?",
2493 new String[]{Long.toString(itemId)});
2494 }
2495
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 /** Filters the set of items who are directly or indirectly (via another container) on the
2497 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002498 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<ItemInfo> allWorkspaceItems,
2500 ArrayList<ItemInfo> currentScreenItems,
2501 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002502 // Purge any null ItemInfos
2503 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2504 while (iter.hasNext()) {
2505 ItemInfo i = iter.next();
2506 if (i == null) {
2507 iter.remove();
2508 }
2509 }
2510
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 // Order the set of items by their containers first, this allows use to walk through the
2512 // list sequentially, build up a list of containers that are in the specified screen,
2513 // as well as all items in those containers.
2514 Set<Long> itemsOnScreen = new HashSet<Long>();
2515 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2516 @Override
2517 public int compare(ItemInfo lhs, ItemInfo rhs) {
2518 return (int) (lhs.container - rhs.container);
2519 }
2520 });
2521 for (ItemInfo info : allWorkspaceItems) {
2522 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 currentScreenItems.add(info);
2525 itemsOnScreen.add(info.id);
2526 } else {
2527 otherScreenItems.add(info);
2528 }
2529 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2530 currentScreenItems.add(info);
2531 itemsOnScreen.add(info.id);
2532 } else {
2533 if (itemsOnScreen.contains(info.container)) {
2534 currentScreenItems.add(info);
2535 itemsOnScreen.add(info.id);
2536 } else {
2537 otherScreenItems.add(info);
2538 }
2539 }
2540 }
2541 }
2542
2543 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002544 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 ArrayList<LauncherAppWidgetInfo> appWidgets,
2546 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2547 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548
2549 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002550 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002552 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553 currentScreenWidgets.add(widget);
2554 } else {
2555 otherScreenWidgets.add(widget);
2556 }
2557 }
2558 }
2559
2560 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002561 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002562 LongArrayMap<ItemInfo> itemsIdMap,
2563 LongArrayMap<FolderInfo> folders,
2564 LongArrayMap<FolderInfo> currentScreenFolders,
2565 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566
Sunny Goyale2df0622015-04-24 11:27:00 -07002567 int total = folders.size();
2568 for (int i = 0; i < total; i++) {
2569 long id = folders.keyAt(i);
2570 FolderInfo folder = folders.valueAt(i);
2571
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002573 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002575 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 currentScreenFolders.put(id, folder);
2577 } else {
2578 otherScreenFolders.put(id, folder);
2579 }
2580 }
2581 }
2582
2583 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2584 * right) */
2585 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002586 final LauncherAppState app = LauncherAppState.getInstance();
2587 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002588 // XXX: review this
2589 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002590 @Override
2591 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002592 int cellCountX = (int) grid.numColumns;
2593 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002594 int screenOffset = cellCountX * cellCountY;
2595 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002596 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002597 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002598 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002599 rhs.cellY * cellCountX + rhs.cellX);
2600 return (int) (lr - rr);
2601 }
2602 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002604
Adam Cohendcd297f2013-06-18 13:13:40 -07002605 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2606 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002607 final Runnable r = new Runnable() {
2608 @Override
2609 public void run() {
2610 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2611 if (callbacks != null) {
2612 callbacks.bindScreens(orderedScreens);
2613 }
2614 }
2615 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002616 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002617 }
2618
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002619 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2620 final ArrayList<ItemInfo> workspaceItems,
2621 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002622 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002624
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 final boolean postOnMainThread = (deferredBindRunnables != null);
2626
2627 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002628 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002630 final int start = i;
2631 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 final Runnable r = new Runnable() {
2633 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002634 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002635 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002636 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002637 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2638 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002639 }
2640 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641 };
2642 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002643 synchronized (deferredBindRunnables) {
2644 deferredBindRunnables.add(r);
2645 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002646 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002647 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 }
Joe Onorato36115782010-06-17 13:28:48 -04002649 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002650
2651 // Bind the folders
2652 if (!folders.isEmpty()) {
2653 final Runnable r = new Runnable() {
2654 public void run() {
2655 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2656 if (callbacks != null) {
2657 callbacks.bindFolders(folders);
2658 }
2659 }
2660 };
2661 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002662 synchronized (deferredBindRunnables) {
2663 deferredBindRunnables.add(r);
2664 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002665 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002666 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002667 }
2668 }
2669
2670 // Bind the widgets, one at a time
2671 N = appWidgets.size();
2672 for (int i = 0; i < N; i++) {
2673 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2674 final Runnable r = new Runnable() {
2675 public void run() {
2676 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2677 if (callbacks != null) {
2678 callbacks.bindAppWidget(widget);
2679 }
2680 }
2681 };
2682 if (postOnMainThread) {
2683 deferredBindRunnables.add(r);
2684 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002685 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002686 }
2687 }
2688 }
2689
2690 /**
2691 * Binds all loaded data to actual views on the main thread.
2692 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002693 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694 final long t = SystemClock.uptimeMillis();
2695 Runnable r;
2696
2697 // Don't use these two variables in any of the callback runnables.
2698 // Otherwise we hold a reference to them.
2699 final Callbacks oldCallbacks = mCallbacks.get();
2700 if (oldCallbacks == null) {
2701 // This launcher has exited and nobody bothered to tell us. Just bail.
2702 Log.w(TAG, "LoaderTask running with no launcher");
2703 return;
2704 }
2705
Winson Chung9b9fb962013-11-15 15:39:34 -08002706 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002707 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2708 ArrayList<LauncherAppWidgetInfo> appWidgets =
2709 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002710 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002711
2712 final LongArrayMap<FolderInfo> folders;
2713 final LongArrayMap<ItemInfo> itemsIdMap;
2714
Winson Chung2abf94d2012-07-18 18:16:38 -07002715 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002716 workspaceItems.addAll(sBgWorkspaceItems);
2717 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002718 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002719
2720 folders = sBgFolders.clone();
2721 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002722 }
2723
Derek Prothro7aff3992013-12-10 14:00:37 -05002724 final boolean isLoadingSynchronously =
2725 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002726 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002727 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002728 if (currScreen >= orderedScreenIds.size()) {
2729 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002730 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002731 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002732 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002733 final long currentScreenId = currentScreen < 0
2734 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002735
2736 // Load all the items that are on the current page first (and in the process, unbind
2737 // all the existing workspace items before we call startBinding() below.
2738 unbindWorkspaceItemsOnMainThread();
2739
2740 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002741 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2742 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2743 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2744 new ArrayList<LauncherAppWidgetInfo>();
2745 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2746 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002747 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2748 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002749
Winson Chung9b9fb962013-11-15 15:39:34 -08002750 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002751 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002752 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002753 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002754 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002755 otherFolders);
2756 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2757 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2758
2759 // Tell the workspace that we're about to start binding items
2760 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002761 public void run() {
2762 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2763 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002764 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002765 }
2766 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002767 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002768 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002769
Adam Cohendcd297f2013-06-18 13:13:40 -07002770 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2771
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002772 // Load items on the current page
2773 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2774 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002775 if (isLoadingSynchronously) {
2776 r = new Runnable() {
2777 public void run() {
2778 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002779 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002780 callbacks.onPageBoundSynchronously(currentScreen);
2781 }
2782 }
2783 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002784 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002786
Winson Chung4a2afa32012-07-19 14:53:05 -07002787 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2788 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002789 synchronized (mDeferredBindRunnables) {
2790 mDeferredBindRunnables.clear();
2791 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002792 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002793 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002794
2795 // Tell the workspace that we're done binding items
2796 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002797 public void run() {
2798 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2799 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002800 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002801 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002802
Winson Chung98e030b2012-05-07 16:01:11 -07002803 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002804 if (DEBUG_LOADERS) {
2805 Log.d(TAG, "bound workspace in "
2806 + (SystemClock.uptimeMillis()-t) + "ms");
2807 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002808
2809 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002810 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002811 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002812 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002813 synchronized (mDeferredBindRunnables) {
2814 mDeferredBindRunnables.add(r);
2815 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002816 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002817 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002818 }
Joe Onorato36115782010-06-17 13:28:48 -04002819 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002820
Joe Onorato36115782010-06-17 13:28:48 -04002821 private void loadAndBindAllApps() {
2822 if (DEBUG_LOADERS) {
2823 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2824 }
2825 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002826 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002827 synchronized (LoaderTask.this) {
2828 if (mStopped) {
2829 return;
2830 }
2831 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002832 }
Joe Onorato36115782010-06-17 13:28:48 -04002833 } else {
2834 onlyBindAllApps();
2835 }
2836 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002837
Joe Onorato36115782010-06-17 13:28:48 -04002838 private void onlyBindAllApps() {
2839 final Callbacks oldCallbacks = mCallbacks.get();
2840 if (oldCallbacks == null) {
2841 // This launcher has exited and nobody bothered to tell us. Just bail.
2842 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2843 return;
2844 }
2845
2846 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002847 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002848 final ArrayList<AppInfo> list
2849 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002850 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002851 public void run() {
2852 final long t = SystemClock.uptimeMillis();
2853 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2854 if (callbacks != null) {
2855 callbacks.bindAllApplications(list);
2856 }
2857 if (DEBUG_LOADERS) {
2858 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2859 + (SystemClock.uptimeMillis()-t) + "ms");
2860 }
2861 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002862 };
2863 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002864 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002865 r.run();
2866 } else {
2867 mHandler.post(r);
2868 }
Joe Onorato36115782010-06-17 13:28:48 -04002869 }
2870
Winson Chung64359a52013-07-08 17:17:08 -07002871 private void loadAllApps() {
2872 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002873
Joe Onorato36115782010-06-17 13:28:48 -04002874 final Callbacks oldCallbacks = mCallbacks.get();
2875 if (oldCallbacks == null) {
2876 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002877 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002878 return;
2879 }
2880
2881 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2882 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2883
Kenny Guyed131872014-04-30 03:02:21 +01002884 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2885
Winson Chung64359a52013-07-08 17:17:08 -07002886 // Clear the list of apps
2887 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002888 for (UserHandleCompat user : profiles) {
2889 // Query for the set of apps
2890 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2891 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2892 if (DEBUG_LOADERS) {
2893 Log.d(TAG, "getActivityList took "
2894 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2895 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2896 }
2897 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002898 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002899 if (apps == null || apps.isEmpty()) {
2900 return;
2901 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002902
2903 // Update icon cache
2904 HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
2905
2906 // If any package icon has changed (app was updated while launcher was dead),
2907 // update the corresponding shortcuts.
2908 if (!updatedPackages.isEmpty()) {
2909 final ArrayList<ShortcutInfo> updates = new ArrayList<ShortcutInfo>();
2910 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002911 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002912 if (info instanceof ShortcutInfo && user.equals(info.user)
2913 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2914 ShortcutInfo si = (ShortcutInfo) info;
2915 ComponentName cn = si.getTargetComponent();
2916 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2917 si.updateIcon(mIconCache);
2918 updates.add(si);
2919 }
2920 }
2921 }
2922 }
2923
2924 if (!updates.isEmpty()) {
2925 final UserHandleCompat userFinal = user;
2926 mHandler.post(new Runnable() {
2927
2928 public void run() {
2929 Callbacks cb = getCallback();
2930 if (cb != null) {
2931 cb.bindShortcutsChanged(
2932 updates, new ArrayList<ShortcutInfo>(), userFinal);
2933 }
2934 }
2935 });
2936 }
Kenny Guyed131872014-04-30 03:02:21 +01002937 }
Joe Onorato36115782010-06-17 13:28:48 -04002938
Kenny Guyed131872014-04-30 03:02:21 +01002939 // Create the ApplicationInfos
2940 for (int i = 0; i < apps.size(); i++) {
2941 LauncherActivityInfoCompat app = apps.get(i);
2942 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002943 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002944 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002945
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002946 if (!user.equals(UserHandleCompat.myUserHandle())) {
2947 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2948 if (heuristic != null) {
2949 heuristic.processUserApps(apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002950 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002951 }
Winson Chung64359a52013-07-08 17:17:08 -07002952 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002953 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002954 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2955 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002956
2957 // Post callback on main thread
2958 mHandler.post(new Runnable() {
2959 public void run() {
2960 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002961 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002962 if (callbacks != null) {
2963 callbacks.bindAllApplications(added);
2964 if (DEBUG_LOADERS) {
2965 Log.d(TAG, "bound " + added.size() + " apps in "
2966 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2967 }
2968 } else {
2969 Log.i(TAG, "not binding apps: no Launcher activity");
2970 }
2971 }
2972 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002973 // Cleanup any data stored for a deleted user.
2974 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002975
Joe Onorato36115782010-06-17 13:28:48 -04002976 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002977 Log.d(TAG, "Icons processed in "
2978 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002979 }
2980 }
2981
2982 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002983 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002984 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2985 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2986 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2987 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2988 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2989 }
Joe Onorato36115782010-06-17 13:28:48 -04002990 }
2991 }
2992
2993 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002994 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
2996
Adam Cohen091440a2015-03-18 14:16:05 -07002997 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002998
2999 @Override
3000 public void onReceive(Context context, Intent intent) {
3001 synchronized (sBgLock) {
3002 final LauncherAppsCompat launcherApps = LauncherAppsCompat
3003 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07003004 final PackageManager manager = context.getPackageManager();
3005 final ArrayList<String> packagesRemoved = new ArrayList<String>();
3006 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003007 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
3008 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003009 packagesRemoved.clear();
3010 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003011 for (String pkg : entry.getValue()) {
3012 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003013 boolean packageOnSdcard = launcherApps.isAppEnabled(
3014 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
3015 if (packageOnSdcard) {
3016 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
3017 packagesUnavailable.add(pkg);
3018 } else {
3019 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
3020 packagesRemoved.add(pkg);
3021 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003022 }
3023 }
3024 if (!packagesRemoved.isEmpty()) {
3025 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
3026 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3027 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003028 if (!packagesUnavailable.isEmpty()) {
3029 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
3030 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3031 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003032 }
Sunny Goyal34942622014-08-29 17:20:55 -07003033 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003034 }
3035 }
3036 }
3037
Joe Onorato36115782010-06-17 13:28:48 -04003038 private class PackageUpdatedTask implements Runnable {
3039 int mOp;
3040 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003041 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003042
3043 public static final int OP_NONE = 0;
3044 public static final int OP_ADD = 1;
3045 public static final int OP_UPDATE = 2;
3046 public static final int OP_REMOVE = 3; // uninstlled
3047 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3048
3049
Kenny Guyed131872014-04-30 03:02:21 +01003050 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003051 mOp = op;
3052 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003053 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003054 }
3055
3056 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003057 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003058
3059 final String[] packages = mPackages;
3060 final int N = packages.length;
3061 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003062 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003063 for (int i=0; i<N; i++) {
3064 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003065 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003066 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003067 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003068
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003069 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3070 if (heuristic != null) {
3071 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003072 }
Joe Onorato36115782010-06-17 13:28:48 -04003073 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003074 }
Joe Onorato36115782010-06-17 13:28:48 -04003075 case OP_UPDATE:
3076 for (int i=0; i<N; i++) {
3077 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003078 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003079 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003080 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003081 }
3082 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003083 case OP_REMOVE: {
3084 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3085 if (heuristic != null) {
3086 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003087 }
Joe Onorato36115782010-06-17 13:28:48 -04003088 for (int i=0; i<N; i++) {
3089 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003090 mIconCache.removeIconsForPkg(packages[i], mUser);
3091 }
3092 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003093 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003094 case OP_UNAVAILABLE:
3095 for (int i=0; i<N; i++) {
3096 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3097 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003098 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003099 }
3100 break;
3101 }
3102
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003103 ArrayList<AppInfo> added = null;
3104 ArrayList<AppInfo> modified = null;
3105 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003106
Adam Cohen487f7dd2012-06-28 18:12:10 -07003107 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003108 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003109 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003110 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003111 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003112 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003113 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003114 }
Winson Chung5d55f332012-07-16 20:45:03 -07003115 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003116 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003117 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003118 }
3119
Sunny Goyale0f58d72014-11-10 18:05:31 -08003120 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003121 if (callbacks == null) {
3122 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3123 return;
3124 }
3125
Sunny Goyal4390ace2014-10-13 11:33:11 -07003126 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3127 new HashMap<ComponentName, AppInfo>();
3128
Joe Onorato36115782010-06-17 13:28:48 -04003129 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003130 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 for (AppInfo ai : added) {
3132 addedOrUpdatedApps.put(ai.componentName, ai);
3133 }
Joe Onorato36115782010-06-17 13:28:48 -04003134 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003135
Joe Onorato36115782010-06-17 13:28:48 -04003136 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003137 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003138 for (AppInfo ai : modified) {
3139 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003140 }
3141
Joe Onorato36115782010-06-17 13:28:48 -04003142 mHandler.post(new Runnable() {
3143 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003144 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003145 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003146 callbacks.bindAppsUpdated(modifiedFinal);
3147 }
3148 }
3149 });
3150 }
Winson Chung83892cc2013-05-01 16:53:33 -07003151
Sunny Goyal4390ace2014-10-13 11:33:11 -07003152 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003153 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003154 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3155 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3156 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3157
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003158 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003159 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003160 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003161 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3162 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163 boolean infoUpdated = false;
3164 boolean shortcutUpdated = false;
3165
3166 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003167 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003169 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3170 si.iconResource.resourceName, mIconCache, context);
3171 if (icon != null) {
3172 si.setIcon(icon);
3173 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003175 }
3176 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177
3178 ComponentName cn = si.getTargetComponent();
3179 if (cn != null && packageSet.contains(cn.getPackageName())) {
3180 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3181
3182 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003183 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3184 // Auto install icon
3185 PackageManager pm = context.getPackageManager();
3186 ResolveInfo matched = pm.resolveActivity(
3187 new Intent(Intent.ACTION_MAIN)
3188 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3189 PackageManager.MATCH_DEFAULT_ONLY);
3190 if (matched == null) {
3191 // Try to find the best match activity.
3192 Intent intent = pm.getLaunchIntentForPackage(
3193 cn.getPackageName());
3194 if (intent != null) {
3195 cn = intent.getComponent();
3196 appInfo = addedOrUpdatedApps.get(cn);
3197 }
3198
3199 if ((intent == null) || (appInfo == null)) {
3200 removedShortcuts.add(si);
3201 continue;
3202 }
3203 si.promisedIntent = intent;
3204 }
3205 }
3206
3207 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003208 if (appInfo != null) {
3209 si.flags = appInfo.flags;
3210 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003211
Sunny Goyal756adbc2015-04-16 15:20:51 -07003212 si.intent = si.promisedIntent;
3213 si.promisedIntent = null;
3214 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003215 infoUpdated = true;
3216 si.updateIcon(mIconCache);
3217 }
3218
3219 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3220 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3221 si.updateIcon(mIconCache);
3222 si.title = appInfo.title.toString();
3223 si.contentDescription = appInfo.contentDescription;
3224 infoUpdated = true;
3225 }
3226
3227 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3228 // Since package was just updated, the target must be available now.
3229 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3230 shortcutUpdated = true;
3231 }
3232 }
3233
3234 if (infoUpdated || shortcutUpdated) {
3235 updatedShortcuts.add(si);
3236 }
3237 if (infoUpdated) {
3238 updateItemInDatabase(context, si);
3239 }
3240 } else if (info instanceof LauncherAppWidgetInfo) {
3241 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3242 if (mUser.equals(widgetInfo.user)
3243 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3244 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3245 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3246 widgets.add(widgetInfo);
3247 updateItemInDatabase(context, widgetInfo);
3248 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003249 }
3250 }
3251 }
3252
Sunny Goyal4390ace2014-10-13 11:33:11 -07003253 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3254 mHandler.post(new Runnable() {
3255
3256 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003257 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003258 if (callbacks == cb && cb != null) {
3259 callbacks.bindShortcutsChanged(
3260 updatedShortcuts, removedShortcuts, mUser);
3261 }
3262 }
3263 });
3264 if (!removedShortcuts.isEmpty()) {
3265 deleteItemsFromDatabase(context, removedShortcuts);
3266 }
3267 }
3268 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003269 mHandler.post(new Runnable() {
3270 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003271 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003272 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003273 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003274 }
3275 }
3276 });
3277 }
3278 }
3279
Winson Chungdf95eb12013-10-16 14:57:07 -07003280 final ArrayList<String> removedPackageNames =
3281 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003282 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003283 // Mark all packages in the broadcast to be removed
3284 removedPackageNames.addAll(Arrays.asList(packages));
3285 } else if (mOp == OP_UPDATE) {
3286 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003287 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003288 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003289 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003290 }
3291 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003292 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003293
Winson Chungdf95eb12013-10-16 14:57:07 -07003294 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003295 final int removeReason;
3296 if (mOp == OP_UNAVAILABLE) {
3297 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3298 } else {
3299 // Remove all the components associated with this package
3300 for (String pn : removedPackageNames) {
3301 deletePackageFromDatabase(context, pn, mUser);
3302 }
3303 // Remove all the specific components
3304 for (AppInfo a : removedApps) {
3305 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3306 deleteItemsFromDatabase(context, infos);
3307 }
3308 removeReason = 0;
3309 }
3310
Winson Chungdf95eb12013-10-16 14:57:07 -07003311 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003312 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003313 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003314 mHandler.post(new Runnable() {
3315 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003316 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003317 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003318 callbacks.bindComponentsRemoved(
3319 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003320 }
3321 }
3322 });
Joe Onoratobe386092009-11-17 17:32:16 -08003323 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003324 if (Build.VERSION.SDK_INT < 17) {
3325 loadAndBindWidgetsAndShortcuts(context, callbacks);
3326 }
Adam Cohen4caf2982013-08-20 18:54:31 -07003327 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003328 mHandler.post(new Runnable() {
3329 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003330 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003331 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003332 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003333 }
3334 }
3335 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
3337 }
3338
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003339 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3340 boolean refresh) {
Adam Cohen59400422014-03-05 18:07:04 -08003341 synchronized (sBgLock) {
Robin Lee26ace122015-03-16 19:41:43 +00003342 if (sBgWidgetProviders == null || refresh) {
3343 sBgWidgetProviders = new HashMap<>();
3344 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3345 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003346
Robin Lee26ace122015-03-16 19:41:43 +00003347 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3348 for (AppWidgetProviderInfo pInfo : widgets) {
3349 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3350 UserHandleCompat user = wm.getUser(info);
3351 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3352 }
3353
3354 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3355 for (CustomAppWidget widget : customWidgets) {
3356 info = new LauncherAppWidgetProviderInfo(context, widget);
3357 UserHandleCompat user = wm.getUser(info);
3358 sBgWidgetProviders.put(new ComponentKey(info.provider, user), info);
3359 }
Adam Cohen59400422014-03-05 18:07:04 -08003360 }
Adam Cohen59400422014-03-05 18:07:04 -08003361 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3362 }
3363 }
3364
Robin Lee26ace122015-03-16 19:41:43 +00003365 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3366 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003367 synchronized (sBgLock) {
3368 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003369 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003370 }
Robin Lee26ace122015-03-16 19:41:43 +00003371 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003372 }
3373 }
3374
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003375 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks) {
3376 runOnWorkerThread(new Runnable(){
3377 @Override
3378 public void run() {
3379 final ArrayList<Object> list =
3380 getSortedWidgetsAndShortcuts(context, true /* refresh */);
3381 mHandler.post(new Runnable() {
3382 @Override
3383 public void run() {
3384 Callbacks cb = getCallback();
3385 if (callbacks == cb && cb != null) {
3386 callbacks.bindPackagesUpdated(list);
3387 }
3388 }
3389 });
3390 }
3391 });
3392 }
3393
Winson Chungb745afb2015-03-02 11:51:23 -08003394 // Returns a list of ResolveInfos/AppWidgetInfos in sorted order
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003395 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003396 PackageManager packageManager = context.getPackageManager();
3397 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003398 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003399 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3400 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003401 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003402 return widgetsAndShortcuts;
3403 }
3404
Adam Cohen091440a2015-03-18 14:16:05 -07003405 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003406 UserHandleCompat user) {
3407 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3408 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003409 }
Adam Cohen556f6132014-01-15 15:18:08 -08003410
Kenny Guyed131872014-04-30 03:02:21 +01003411 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3412 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003413 if (cn == null) {
3414 return false;
3415 }
Kenny Guyed131872014-04-30 03:02:21 +01003416 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3417 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003418 return false;
3419 }
Kenny Guyed131872014-04-30 03:02:21 +01003420 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003421 }
3422
Adam Cohena28b78e2014-05-20 17:03:04 -07003423 public static boolean isValidPackage(Context context, String packageName,
3424 UserHandleCompat user) {
3425 if (packageName == null) {
3426 return false;
3427 }
3428 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3429 return launcherApps.isPackageEnabledForProfile(packageName, user);
3430 }
3431
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003433 * Make an ShortcutInfo object for a restored application or shortcut item that points
3434 * to a package that is not yet installed on the system.
3435 */
Sunny Goyal34942622014-08-29 17:20:55 -07003436 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -07003437 int promiseType, boolean useLowResIcon) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003438 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003439 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34b65272015-03-11 16:56:52 -07003440 mIconCache.getTitleAndIcon(info, intent, info.user, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -07003441
3442 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3443 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3444 if (!TextUtils.isEmpty(title)) {
3445 info.title = title;
3446 }
Sunny Goyal34942622014-08-29 17:20:55 -07003447 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3448 if (TextUtils.isEmpty(info.title)) {
3449 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3450 }
Sunny Goyal34942622014-08-29 17:20:55 -07003451 } else {
3452 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3453 }
3454
Kenny Guyc2bd8102014-06-30 12:30:31 +01003455 info.contentDescription = mUserManager.getBadgedLabelForUser(
3456 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003457 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003458 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003459 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003460 return info;
3461 }
3462
3463 /**
3464 * Make an Intent object for a restored application or shortcut item that points
3465 * to the market page for the item.
3466 */
Adam Cohen091440a2015-03-18 14:16:05 -07003467 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003468 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003469 return getMarketIntent(componentName.getPackageName());
3470 }
3471
3472 static Intent getMarketIntent(String packageName) {
3473 return new Intent(Intent.ACTION_VIEW)
3474 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003475 .scheme("market")
3476 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003477 .appendQueryParameter("id", packageName)
3478 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003479 }
3480
3481 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003482 * Make an ShortcutInfo object for a shortcut that is an application.
3483 *
3484 * If c is not null, then it will be used to fill in missing data like the title and icon.
3485 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003486 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003487 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003488 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003489 if (user == null) {
3490 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003491 return null;
3492 }
3493
Kenny Guyed131872014-04-30 03:02:21 +01003494 ComponentName componentName = intent.getComponent();
3495 if (componentName == null) {
3496 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3497 return null;
3498 }
3499
3500 Intent newIntent = new Intent(intent.getAction(), null);
3501 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3502 newIntent.setComponent(componentName);
3503 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003504 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003505 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3506 return null;
3507 }
3508
3509 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003510 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003511 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3512 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3513 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003514 }
3515
Joe Onorato56d82912010-03-07 14:32:10 -05003516 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003517 if (TextUtils.isEmpty(info.title) && c != null) {
3518 info.title = c.getString(titleIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003519 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003520
Joe Onorato56d82912010-03-07 14:32:10 -05003521 // fall back to the class name of the activity
3522 if (info.title == null) {
3523 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003524 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003525
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003527 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003528 info.contentDescription = mUserManager.getBadgedLabelForUser(
3529 info.title.toString(), info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003530 if (lai != null) {
3531 info.flags = AppInfo.initFlags(lai);
3532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003534 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003535
Sunny Goyale2df0622015-04-24 11:27:00 -07003536 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003537 ItemInfoFilter f) {
3538 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3539 for (ItemInfo i : infos) {
3540 if (i instanceof ShortcutInfo) {
3541 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003542 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003543 if (cn != null && f.filterItem(null, info, cn)) {
3544 filtered.add(info);
3545 }
3546 } else if (i instanceof FolderInfo) {
3547 FolderInfo info = (FolderInfo) i;
3548 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003549 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003550 if (cn != null && f.filterItem(info, s, cn)) {
3551 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003552 }
3553 }
Winson Chung64359a52013-07-08 17:17:08 -07003554 } else if (i instanceof LauncherAppWidgetInfo) {
3555 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3556 ComponentName cn = info.providerName;
3557 if (cn != null && f.filterItem(null, info, cn)) {
3558 filtered.add(info);
3559 }
Winson Chung8a435102012-08-30 17:16:53 -07003560 }
3561 }
Winson Chung64359a52013-07-08 17:17:08 -07003562 return new ArrayList<ItemInfo>(filtered);
3563 }
3564
Adam Cohen091440a2015-03-18 14:16:05 -07003565 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003566 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003567 ItemInfoFilter filter = new ItemInfoFilter() {
3568 @Override
3569 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003570 if (info.user == null) {
3571 return cn.equals(cname);
3572 } else {
3573 return cn.equals(cname) && info.user.equals(user);
3574 }
Winson Chung64359a52013-07-08 17:17:08 -07003575 }
3576 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003577 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003578 }
3579
Sunny Goyal1a745e82014-10-02 15:58:31 -07003580 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003581 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003582 */
Adam Cohen091440a2015-03-18 14:16:05 -07003583 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003584 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3585 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003586
Joe Onorato56d82912010-03-07 14:32:10 -05003587 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003588 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003589 // Non-app shortcuts are only supported for current user.
3590 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003593 // TODO: If there's an explicit component and we can't install that, delete it.
3594
Joe Onorato56d82912010-03-07 14:32:10 -05003595 info.title = c.getString(titleIndex);
3596
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 int iconType = c.getInt(iconTypeIndex);
3598 switch (iconType) {
3599 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3600 String packageName = c.getString(iconPackageIndex);
3601 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003602 info.customIcon = false;
3603 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003604 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003605 // the db
3606 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003607 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003608 }
3609 // the fallback icon
3610 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003611 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003612 info.usingFallbackIcon = true;
3613 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 break;
3615 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003616 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003617 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003618 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003619 info.customIcon = false;
3620 info.usingFallbackIcon = true;
3621 } else {
3622 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 break;
3625 default:
Kenny Guyed131872014-04-30 03:02:21 +01003626 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003627 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 info.customIcon = false;
3629 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003630 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003631 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 return info;
3633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003634
Sunny Goyal2350bc92014-10-14 16:42:54 -07003635 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003636 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3637 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3638 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3639
Adam Cohend9198822011-11-22 16:42:47 -08003640 if (intent == null) {
3641 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3642 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3643 return null;
3644 }
3645
Joe Onorato0589f0f2010-02-08 13:44:00 -08003646 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003647 boolean customIcon = false;
3648 ShortcutIconResource iconResource = null;
3649
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003650 if (bitmap instanceof Bitmap) {
3651 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003652 customIcon = true;
3653 } else {
3654 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003655 if (extra instanceof ShortcutIconResource) {
3656 iconResource = (ShortcutIconResource) extra;
3657 icon = Utilities.createIconBitmap(iconResource.packageName,
3658 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003659 }
3660 }
3661
Michael Jurkac9d95c52011-08-29 14:03:34 -07003662 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003663
Kenny Guyed131872014-04-30 03:02:21 +01003664 // Only support intents for current user for now. Intents sent from other
3665 // users wouldn't get here without intent forwarding anyway.
3666 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003667 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003668 icon = mIconCache.getDefaultIcon(info.user);
3669 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003670 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003671 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003672
Joe Onorato0589f0f2010-02-08 13:44:00 -08003673 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003674 info.contentDescription = mUserManager.getBadgedLabelForUser(
3675 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003676 info.intent = intent;
3677 info.customIcon = customIcon;
3678 info.iconResource = iconResource;
3679
3680 return info;
3681 }
3682
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003684 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 * or make a new one.
3686 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003687 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 // See if a placeholder was created for us already
3689 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003690 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003692 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 folders.put(id, folderInfo);
3694 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003695 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 }
3697
Winson Chung1ed747a2011-05-03 16:18:34 -07003698 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003699 private final AppWidgetManagerCompat mManager;
3700 private final PackageManager mPackageManager;
3701 private final HashMap<Object, String> mLabelCache;
3702 private final Collator mCollator;
3703
Hyunyoung Song3f471442015-04-08 19:01:34 -07003704 public WidgetAndShortcutNameComparator(Context context) {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003705 mManager = AppWidgetManagerCompat.getInstance(context);
3706 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003707 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003708 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003709 }
3710 public final int compare(Object a, Object b) {
3711 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003712 if (mLabelCache.containsKey(a)) {
3713 labelA = mLabelCache.get(a);
3714 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003715 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3716 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003717 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003718 mLabelCache.put(a, labelA);
3719 }
3720 if (mLabelCache.containsKey(b)) {
3721 labelB = mLabelCache.get(b);
3722 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003723 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c165aa2015-01-30 10:28:23 -08003724 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003725 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003726 mLabelCache.put(b, labelB);
3727 }
Winson Chung11904872012-09-17 16:58:46 -07003728 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003729 }
3730 };
Joe Onoratobe386092009-11-17 17:32:16 -08003731
Sunny Goyal651077b2014-06-30 14:15:31 -07003732 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3733 return (provider != null) && (provider.provider != null)
3734 && (provider.provider.getPackageName() != null);
3735 }
3736
Joe Onoratobe386092009-11-17 17:32:16 -08003737 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003738 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003739 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3740 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3741 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3742 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003743 if (mLoaderTask != null) {
3744 mLoaderTask.dumpState();
3745 } else {
3746 Log.d(TAG, "mLoaderTask=null");
3747 }
Joe Onoratobe386092009-11-17 17:32:16 -08003748 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003749
3750 public Callbacks getCallback() {
3751 return mCallbacks != null ? mCallbacks.get() : null;
3752 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003753
3754 /**
3755 * @return {@link FolderInfo} if its already loaded.
3756 */
3757 public FolderInfo findFolderById(Long folderId) {
3758 synchronized (sBgLock) {
3759 return sBgFolders.get(folderId);
3760 }
3761 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003762
3763 /**
3764 * @return the looper for the worker thread which can be used to start background tasks.
3765 */
3766 public static Looper getWorkerLooper() {
3767 return sWorkerThread.getLooper();
3768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003769}