blob: 79e7b881c742ba8c1b8a9bfd01948be0aef1c140 [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;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import 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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Sunny Goyal3dc7bee2015-09-15 11:32:58 -070037import android.os.DeadObjectException;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070059import com.android.launcher3.config.ProviderConfig;
Sunny Goyale5bb7052015-07-27 14:36:07 -070060import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070066import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070067import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080068
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.lang.ref.WeakReference;
70import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070071import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070073import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070074import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.Collections;
76import java.util.Comparator;
77import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070079import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070081import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * Maintains in-memory state of the Launcher. It is expected that there should be only one
87 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070088 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 */
Kenny Guyed131872014-04-30 03:02:21 +010090public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010091 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080092 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040093 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070094 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070097
Dan Sandlerd5024042014-01-09 15:01:33 -050098 public static final int LOADER_FLAG_NONE = 0;
99 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
100 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
101
Joe Onorato36115782010-06-17 13:28:48 -0400102 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500103 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800106 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final LauncherAppState mApp;
109 @Thunk final Object mLock = new Object();
110 @Thunk DeferredHandler mHandler = new DeferredHandler();
111 @Thunk LoaderTask mLoaderTask;
112 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700113 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Jason Monkbbe1e242014-05-16 17:37:34 -0400115 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118 static {
119 sWorkerThread.start();
120 }
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 // We start off with everything not loaded. After that, we assume that
124 // our monitoring of the package manager provides all updates and we never
125 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk boolean mWorkspaceLoaded;
127 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700128
Sunny Goyal756a28a2015-04-23 17:07:55 -0700129 /**
130 * Set of runnables to be called on the background thread after the workspace binding
131 * is complete.
132 */
133 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700138 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700139 // Entire list of widgets.
140 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyal31860582015-09-18 08:38:57 -0700170 // sBgShortcutProviders is the set of custom shortcut providers
171 public static List<ResolveInfo> sBgShortcutProviders;
172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700187 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
190 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700191 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700193 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800194 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700197 public void bindAppsAdded(ArrayList<Long> newScreens,
198 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700199 ArrayList<ItemInfo> addAnimated,
200 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
203 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
204 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700205 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700208 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700209 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700212 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700243 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 mIconCache = iconCache;
245
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700263 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Winson Chunge43a1e72014-01-15 10:33:02 -0800272 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
273 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800274 }
275
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 public void setPackageState(final PackageInstallInfo installInfo) {
277 Runnable updateRunnable = new Runnable() {
278
279 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500280 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 synchronized (sBgLock) {
282 final HashSet<ItemInfo> updates = new HashSet<>();
283
284 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
285 // Ignore install success events as they are handled by Package add events.
286 return;
287 }
288
Sunny Goyale2df0622015-04-24 11:27:00 -0700289 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 if (info instanceof ShortcutInfo) {
291 ShortcutInfo si = (ShortcutInfo) info;
292 ComponentName cn = si.getTargetComponent();
293 if (si.isPromise() && (cn != null)
294 && installInfo.packageName.equals(cn.getPackageName())) {
295 si.setInstallProgress(installInfo.progress);
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
298 // Mark this info as broken.
299 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
300 }
301 updates.add(si);
302 }
303 }
304 }
305
306 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
307 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
308 widget.installProgress = installInfo.progress;
309 updates.add(widget);
310 }
311 }
312
313 if (!updates.isEmpty()) {
314 // Push changes to the callback.
315 Runnable r = new Runnable() {
316 public void run() {
317 Callbacks callbacks = getCallback();
318 if (callbacks != null) {
319 callbacks.bindRestoreItemsChange(updates);
320 }
321 }
322 };
323 mHandler.post(r);
324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326 }
327 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500329 }
330
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 /**
332 * Updates the icons and label of all pending icons for the provided package name.
333 */
334 public void updateSessionDisplayInfo(final String packageName) {
335 Runnable updateRunnable = new Runnable() {
336
337 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 synchronized (sBgLock) {
340 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
341 final UserHandleCompat user = UserHandleCompat.myUserHandle();
342
Sunny Goyale2df0622015-04-24 11:27:00 -0700343 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 if (info instanceof ShortcutInfo) {
345 ShortcutInfo si = (ShortcutInfo) info;
346 ComponentName cn = si.getTargetComponent();
347 if (si.isPromise() && (cn != null)
348 && packageName.equals(cn.getPackageName())) {
349 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
350 // For auto install apps update the icon as well as label.
351 mIconCache.getTitleAndIcon(si,
352 si.promisedIntent, user,
353 si.shouldUseLowResIcon());
354 } else {
355 // Only update the icon for restored apps.
356 si.updateIcon(mIconCache);
357 }
358 updates.add(si);
359 }
360 }
361 }
362
363 if (!updates.isEmpty()) {
364 // Push changes to the callback.
365 Runnable r = new Runnable() {
366 public void run() {
367 Callbacks callbacks = getCallback();
368 if (callbacks != null) {
369 callbacks.bindShortcutsChanged(updates,
370 new ArrayList<ShortcutInfo>(), user);
371 }
372 }
373 };
374 mHandler.post(r);
375 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377 }
378 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700379 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700380 }
381
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384
385 if (allAppsApps == null) {
386 throw new RuntimeException("allAppsApps must not be null");
387 }
388 if (allAppsApps.isEmpty()) {
389 return;
390 }
391
392 // Process the newly added applications and add them to the database first
393 Runnable r = new Runnable() {
394 public void run() {
395 runOnMainThread(new Runnable() {
396 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 }
401 }
402 });
403 }
404 };
405 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700406 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800407
Sunny Goyala9116722015-04-29 13:55:58 -0700408 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 int[] xy, int spanX, int spanY) {
410 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
412 final int xCount = (int) profile.numColumns;
413 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 boolean[][] occupied = new boolean[xCount][yCount];
415 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700416 for (ItemInfo r : occupiedPos) {
417 int right = r.cellX + r.spanX;
418 int bottom = r.cellY + r.spanY;
419 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
420 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 occupied[x][y] = true;
422 }
423 }
424 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800425 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700426 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
428
429 /**
430 * Find a position on the screen for the given size or adds a new screen.
431 * @return screenId and the coordinates for the item.
432 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700433 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 ArrayList<Long> workspaceScreens,
435 ArrayList<Long> addedWorkspaceScreensFinal,
436 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700437 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438
Sunny Goyala9116722015-04-29 13:55:58 -0700439 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700440 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700441 synchronized (sBgLock) {
442 for (ItemInfo info : sBgItemsIdMap) {
443 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
444 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
445 if (items == null) {
446 items = new ArrayList<>();
447 screenItems.put(info.screenId, items);
448 }
449 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
453
454 // Find appropriate space for the item.
455 long screenId = 0;
456 int[] cordinates = new int[2];
457 boolean found = false;
458
459 int screenCount = workspaceScreens.size();
460 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700461 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 if (preferredScreenIndex < screenCount) {
463 screenId = workspaceScreens.get(preferredScreenIndex);
464 found = findNextAvailableIconSpaceInScreen(
465 screenItems.get(screenId), cordinates, spanX, spanY);
466 }
467
468 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700469 // Search on any of the screens starting from the first screen.
470 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800471 screenId = workspaceScreens.get(screen);
472 if (findNextAvailableIconSpaceInScreen(
473 screenItems.get(screenId), cordinates, spanX, spanY)) {
474 // We found a space for it
475 found = true;
476 break;
477 }
478 }
479 }
480
481 if (!found) {
482 // Still no position found. Add a new screen to the end.
483 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
484
485 // Save the screen id for binding in the workspace
486 workspaceScreens.add(screenId);
487 addedWorkspaceScreensFinal.add(screenId);
488
489 // If we still can't find an empty space, then God help us all!!!
490 if (!findNextAvailableIconSpaceInScreen(
491 screenItems.get(screenId), cordinates, spanX, spanY)) {
492 throw new RuntimeException("Can't find space to add the item");
493 }
494 }
495 return Pair.create(screenId, cordinates);
496 }
497
498 /**
499 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700501 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700502 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800504 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700505 return;
Winson Chung997a9232013-07-24 15:33:46 -0700506 }
Winson Chung64359a52013-07-08 17:17:08 -0700507 // Process the newly added applications and add them to the database first
508 Runnable r = new Runnable() {
509 public void run() {
510 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
511 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
512
Winson Chung76828c82013-08-19 15:43:29 -0700513 // Get the list of workspace screens. We need to append to this list and
514 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
515 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800516 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700517 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700519 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700521 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700523 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 }
Winson Chung76828c82013-08-19 15:43:29 -0700525
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 // Find appropriate space for the item.
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700527 Pair<Long, int[]> coords = findSpaceForItem(
528 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 long screenId = coords.first;
530 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700531
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 ItemInfo itemInfo;
533 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
534 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700536 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700537 } else {
538 throw new RuntimeException("Unexpected info type");
539 }
Winson Chung94d67682013-09-25 16:29:40 -0700540
Winson Chung64359a52013-07-08 17:17:08 -0700541 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700543 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700544 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700545 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700547 }
548 }
549
Winson Chung76828c82013-08-19 15:43:29 -0700550 // Update the workspace screens
551 updateWorkspaceScreenOrder(context, workspaceScreens);
552
Adam Cohen76a47a12014-02-05 11:47:43 -0800553 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700554 runOnMainThread(new Runnable() {
555 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800556 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700557 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700558 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
559 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700560 if (!addedShortcutsFinal.isEmpty()) {
561 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
562 long lastScreenId = info.screenId;
563 for (ItemInfo i : addedShortcutsFinal) {
564 if (i.screenId == lastScreenId) {
565 addAnimated.add(i);
566 } else {
567 addNotAnimated.add(i);
568 }
Winson Chung997a9232013-07-24 15:33:46 -0700569 }
570 }
Winson Chungd64d1762013-08-20 14:37:16 -0700571 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800572 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700573 }
Winson Chung64359a52013-07-08 17:17:08 -0700574 }
Winson Chung997a9232013-07-24 15:33:46 -0700575 });
576 }
Winson Chung64359a52013-07-08 17:17:08 -0700577 }
578 };
579 runOnWorkerThread(r);
580 }
581
Sunny Goyald33860f2015-04-23 16:02:20 -0700582 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700583 if (sWorkerThread.getThreadId() == Process.myTid()) {
584 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
585 "main thread");
586 }
587
Sunny Goyald33860f2015-04-23 16:02:20 -0700588 // Remove any queued UI runnables
589 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700590 // Unbind all the workspace items
591 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700592 }
593
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700594 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700595 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700596 // Ensure that we don't use the same workspace items data structure on the main thread
597 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700598 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700599 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700600 tmpItems.addAll(sBgWorkspaceItems);
601 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602 }
603 Runnable r = new Runnable() {
604 @Override
605 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700606 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 item.unbind();
608 }
609 }
610 };
611 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700612 }
613
Joe Onorato9c1289c2009-08-17 11:03:03 -0400614 /**
615 * Adds an item to the DB if it was not created previously, or move it to a new
616 * <container, screen, cellX, cellY>
617 */
618 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700619 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620 if (item.container == ItemInfo.NO_ID) {
621 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700622 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 } else {
624 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 }
627 }
628
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700629 static void checkItemInfoLocked(
630 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
631 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
632 if (modelItem != null && item != modelItem) {
633 // check all the data is consistent
634 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
635 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
636 ShortcutInfo shortcut = (ShortcutInfo) item;
637 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
638 modelShortcut.intent.filterEquals(shortcut.intent) &&
639 modelShortcut.id == shortcut.id &&
640 modelShortcut.itemType == shortcut.itemType &&
641 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700643 modelShortcut.cellX == shortcut.cellX &&
644 modelShortcut.cellY == shortcut.cellY &&
645 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700646 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700647 // For all intents and purposes, this is the same object
648 return;
649 }
650 }
651
652 // the modelItem needs to match up perfectly with item if our model is
653 // to be consistent with the database-- for now, just require
654 // modelItem == item or the equality check above
655 String msg = "item: " + ((item != null) ? item.toString() : "null") +
656 "modelItem: " +
657 ((modelItem != null) ? modelItem.toString() : "null") +
658 "Error: ItemInfo passed to checkItemInfo doesn't match original";
659 RuntimeException e = new RuntimeException(msg);
660 if (stackTrace != null) {
661 e.setStackTrace(stackTrace);
662 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800663 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 }
665 }
666
Michael Jurka816474f2012-06-25 14:49:02 -0700667 static void checkItemInfo(final ItemInfo item) {
668 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
669 final long itemId = item.id;
670 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700671 public void run() {
672 synchronized (sBgLock) {
673 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700674 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700675 }
676 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700677 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700678 }
679
Michael Jurkac9d95c52011-08-29 14:03:34 -0700680 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
681 final ItemInfo item, final String callingFunction) {
682 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700683 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700684 final ContentResolver cr = context.getContentResolver();
685
Adam Cohen487f7dd2012-06-28 18:12:10 -0700686 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700687 Runnable r = new Runnable() {
688 public void run() {
689 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 updateItemArrays(item, itemId, stackTrace);
691 }
692 };
693 runOnWorkerThread(r);
694 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700695
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
697 final ArrayList<ItemInfo> items, final String callingFunction) {
698 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700699
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
701 Runnable r = new Runnable() {
702 public void run() {
703 ArrayList<ContentProviderOperation> ops =
704 new ArrayList<ContentProviderOperation>();
705 int count = items.size();
706 for (int i = 0; i < count; i++) {
707 ItemInfo item = items.get(i);
708 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700709 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700711
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700712 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
713 updateItemArrays(item, itemId, stackTrace);
714
715 }
716 try {
717 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
718 } catch (Exception e) {
719 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720 }
721 }
722 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700723 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700725
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
727 // Lock on mBgLock *after* the db operation
728 synchronized (sBgLock) {
729 checkItemInfoLocked(itemId, item, stackTrace);
730
731 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
732 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
733 // Item is in a folder, make sure this folder exists
734 if (!sBgFolders.containsKey(item.container)) {
735 // An items container is being set to a that of an item which is not in
736 // the list of Folders.
737 String msg = "item: " + item + " container being set to: " +
738 item.container + ", not in the list of folders";
739 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 }
741 }
742
743 // Items are added/removed from the corresponding FolderInfo elsewhere, such
744 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
745 // that are on the desktop, as appropriate
746 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800747 if (modelItem != null &&
748 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
749 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 switch (modelItem.itemType) {
751 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
752 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
753 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
754 if (!sBgWorkspaceItems.contains(modelItem)) {
755 sBgWorkspaceItems.add(modelItem);
756 }
757 break;
758 default:
759 break;
760 }
761 } else {
762 sBgWorkspaceItems.remove(modelItem);
763 }
764 }
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700769 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700770 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 item.cellX = cellX;
774 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700775
Winson Chung3d503fb2011-07-13 17:25:49 -0700776 // We store hotseat items in canonical form which is this orientation invariant position
777 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700781 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784
785 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
788 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800789 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700793 }
794
795 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700796 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
797 * cellX, cellY have already been updated on the ItemInfos.
798 */
799 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
800 final long container, final int screen) {
801
802 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
803 int count = items.size();
804
805 for (int i = 0; i < count; i++) {
806 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700807 item.container = container;
808
809 // We store hotseat items in canonical form which is this orientation invariant position
810 // in the hotseat
811 if (context instanceof Launcher && screen < 0 &&
812 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700813 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 item.cellY);
815 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 }
818
819 final ContentValues values = new ContentValues();
820 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
821 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
822 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800823 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825
826 contentValues.add(values);
827 }
828 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
829 }
830
831 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700832 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800833 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700834 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700836 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800837 item.cellX = cellX;
838 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700839 item.spanX = spanX;
840 item.spanY = spanY;
841
842 // We store hotseat items in canonical form which is this orientation invariant position
843 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700847 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 }
Adam Cohend4844c32011-02-18 19:25:06 -0800850
Adam Cohend4844c32011-02-18 19:25:06 -0800851 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800852 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
854 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800855 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
857 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800859
Michael Jurka816474f2012-06-25 14:49:02 -0700860 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862
863 /**
864 * Update an item to the database in a specified container.
865 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700866 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700867 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100868 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800870 }
871
Sunny Goyal756a28a2015-04-23 17:07:55 -0700872 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700873 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700874 synchronized (mLock) {
875 if (!mHasLoaderCompletedOnce ||
876 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
877 throw new RuntimeException("Trying to add shortcut while loader is running");
878 }
879 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700880 }
881 }
882
Adam Cohend4844c32011-02-18 19:25:06 -0800883 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700884 * Returns true if the shortcuts already exists on the workspace. This must be called after
885 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700887 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
888 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700889 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700890 if (intent.getComponent() != null) {
891 // If component is not null, an intent with null package will produce
892 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700893 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700894 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700895 intentWithPkg = intent.toUri(0);
896 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700897 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
899 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 }
901 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700902 intentWithPkg = intent.toUri(0);
903 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700905
906 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700907 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700908 if (item instanceof ShortcutInfo) {
909 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700910 Intent targetIntent = info.promisedIntent == null
911 ? info.intent : info.promisedIntent;
912 if (targetIntent != null && info.user.equals(user)) {
913 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700914 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
915 return true;
916 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917 }
918 }
919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700921 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700922 }
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 * Add an item to the database in a specified container. Sets the container, screen, cellX and
926 * cellY fields of the item. Also assigns an ID to the item.
927 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700928 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700929 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 item.cellX = cellX;
932 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 // We store hotseat items in canonical form which is this orientation invariant position
934 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 final ContentValues values = new ContentValues();
943 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100944 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945
Michael Jurka414300a2013-08-27 15:42:35 +0200946 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700948
Jason Monk8e19cf22014-03-20 15:06:57 -0400949 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700952 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700955 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400956 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 sBgItemsIdMap.put(item.id, item);
958 switch (item.itemType) {
959 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
960 sBgFolders.put(item.id, (FolderInfo) item);
961 // Fall through
962 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
963 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
964 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
965 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
966 sBgWorkspaceItems.add(item);
967 } else {
968 if (!sBgFolders.containsKey(item.container)) {
969 // Adding an item to a folder that doesn't exist.
970 String msg = "adding item: " + item + " to a folder that " +
971 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700972 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700974 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 break;
976 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
977 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
978 break;
979 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700980 }
981 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700982 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700984 }
985
Sunny Goyal34942622014-08-29 17:20:55 -0700986 private static ArrayList<ItemInfo> getItemsByPackageName(
987 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700988 ItemInfoFilter filter = new ItemInfoFilter() {
989 @Override
990 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
991 return cn.getPackageName().equals(pn) && info.user.equals(user);
992 }
993 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700994 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700995 }
996
997 /**
998 * Removes all the items from the database corresponding to the specified package.
999 */
1000 static void deletePackageFromDatabase(Context context, final String pn,
1001 final UserHandleCompat user) {
1002 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001003 }
1004
1005 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001006 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001008 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001009 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1010 items.add(item);
1011 deleteItemsFromDatabase(context, items);
1012 }
1013
1014 /**
1015 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001016 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001017 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001019 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001021 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001022 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001023 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001025 // Lock on mBgLock *after* the db operation
1026 synchronized (sBgLock) {
1027 switch (item.itemType) {
1028 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1029 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001030 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001031 if (info.container == item.id) {
1032 // We are deleting a folder which still contains items that
1033 // think they are contained by that folder.
1034 String msg = "deleting a folder (" + item + ") which still " +
1035 "contains items (" + info + ")";
1036 Log.e(TAG, msg);
1037 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001039 sBgWorkspaceItems.remove(item);
1040 break;
1041 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1042 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1043 sBgWorkspaceItems.remove(item);
1044 break;
1045 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1046 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1047 break;
1048 }
1049 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051 }
1052 }
Michael Jurka83df1882011-08-31 20:59:26 -07001053 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 }
1056
1057 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 * Update the order of the workspace screens in the database. The array list contains
1059 * a list of screen ids in the order that they should appear.
1060 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001061 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001062 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 final ContentResolver cr = context.getContentResolver();
1064 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1065
1066 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001067 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 while (iter.hasNext()) {
1069 long id = iter.next();
1070 if (id < 0) {
1071 iter.remove();
1072 }
1073 }
1074
1075 Runnable r = new Runnable() {
1076 @Override
1077 public void run() {
Yura085c8532014-02-11 15:15:29 +00001078 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001080 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001081 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 for (int i = 0; i < count; i++) {
1083 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001084 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1086 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001087 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 }
Yura085c8532014-02-11 15:15:29 +00001089
1090 try {
1091 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1092 } catch (Exception ex) {
1093 throw new RuntimeException(ex);
1094 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001095
Winson Chungba9c37f2013-08-30 14:11:37 -07001096 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 sBgWorkspaceScreens.clear();
1098 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001099 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 }
1101 };
1102 runOnWorkerThread(r);
1103 }
1104
1105 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001106 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 */
Winsonc0b52fe2015-09-09 16:38:15 -07001108 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 final ContentResolver cr = context.getContentResolver();
1110
Michael Jurkac9d95c52011-08-29 14:03:34 -07001111 Runnable r = new Runnable() {
1112 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001113 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001115 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 sBgItemsIdMap.remove(info.id);
1117 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 sBgWorkspaceItems.remove(info);
1119 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001120
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001121 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001122 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001124 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 for (ItemInfo childInfo : info.contents) {
1126 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001128 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001129 }
1130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
1135 * Set this as the current Launcher activity object for the loader.
1136 */
1137 public void initialize(Callbacks callbacks) {
1138 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001139 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1140 // workspace to prevent leaking Launcher activities on orientation change.
1141 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 mCallbacks = new WeakReference<Callbacks>(callbacks);
1143 }
1144 }
1145
Kenny Guyed131872014-04-30 03:02:21 +01001146 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001147 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001148 int op = PackageUpdatedTask.OP_UPDATE;
1149 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1150 user));
1151 }
1152
1153 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001154 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001155 int op = PackageUpdatedTask.OP_REMOVE;
1156 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1157 user));
1158 }
1159
1160 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001161 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001162 int op = PackageUpdatedTask.OP_ADD;
1163 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1164 user));
1165 }
1166
1167 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001168 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001169 boolean replacing) {
1170 if (!replacing) {
1171 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1172 user));
1173 if (mAppsCanBeOnRemoveableStorage) {
1174 // Only rebind if we support removable storage. It catches the
1175 // case where
1176 // apps on the external sd card need to be reloaded
1177 startLoaderFromBackground();
1178 }
1179 } else {
1180 // If we are replacing then just update the packages in the list
1181 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1182 packageNames, user));
1183 }
1184 }
1185
1186 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001187 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001188 boolean replacing) {
1189 if (!replacing) {
1190 enqueuePackageUpdated(new PackageUpdatedTask(
1191 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1192 user));
1193 }
Kenny Guyed131872014-04-30 03:02:21 +01001194 }
1195
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001196 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1198 * ACTION_PACKAGE_CHANGED.
1199 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001200 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001201 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001202 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001203
Joe Onorato36115782010-06-17 13:28:48 -04001204 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001205 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001206 // If we have changed locale we need to clear out the labels in all apps/workspace.
1207 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001208 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001209 Callbacks callbacks = getCallback();
1210 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001211 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001212 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001213 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1214 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001215 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001216 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001217 }
1218 }
1219
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001220 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001221 resetLoadedState(true, true);
1222
Reena Lee93f824a2011-09-23 17:20:28 -07001223 // Do this here because if the launcher activity is running it will be restarted.
1224 // If it's not running startLoaderFromBackground will merely tell it that it needs
1225 // to reload.
1226 startLoaderFromBackground();
1227 }
1228
Winson Chungf0c6ae02012-03-21 16:10:31 -07001229 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1230 synchronized (mLock) {
1231 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1232 // mWorkspaceLoaded to true later
1233 stopLoaderLocked();
1234 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1235 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1236 }
1237 }
1238
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001239 /**
1240 * When the launcher is in the background, it's possible for it to miss paired
1241 * configuration changes. So whenever we trigger the loader from the background
1242 * tell the launcher that it needs to re-run the loader when it comes back instead
1243 * of doing it now.
1244 */
1245 public void startLoaderFromBackground() {
1246 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001247 Callbacks callbacks = getCallback();
1248 if (callbacks != null) {
1249 // Only actually run the loader if they're not paused.
1250 if (!callbacks.setLoadOnResume()) {
1251 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001252 }
1253 }
1254 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001255 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001256 }
Joe Onorato36115782010-06-17 13:28:48 -04001257 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001258
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001259 /**
1260 * If there is already a loader task running, tell it to stop.
1261 */
1262 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001263 LoaderTask oldTask = mLoaderTask;
1264 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001265 oldTask.stopLocked();
1266 }
Reena Lee93f824a2011-09-23 17:20:28 -07001267 }
1268
Adam Cohen1a85c582014-09-30 09:48:49 -07001269 public boolean isCurrentCallbacks(Callbacks callbacks) {
1270 return (mCallbacks != null && mCallbacks.get() == callbacks);
1271 }
1272
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001273 public void startLoader(int synchronousBindPage) {
1274 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001275 }
1276
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001277 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001278 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1279 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001280 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001281 // Don't bother to start the thread if we know it's not going to do anything
1282 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001283 final Callbacks oldCallbacks = mCallbacks.get();
1284 // Clear any pending bind-runnables from the synchronized load process.
1285 runOnMainThread(new Runnable() {
1286 public void run() {
1287 oldCallbacks.clearPendingBinds();
1288 }
1289 });
1290
Joe Onorato36115782010-06-17 13:28:48 -04001291 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001292 stopLoaderLocked();
1293 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001294 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001295 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001296 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1297 } else {
1298 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1299 sWorker.post(mLoaderTask);
1300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001302 }
1303 }
1304
Joe Onorato36115782010-06-17 13:28:48 -04001305 public void stopLoader() {
1306 synchronized (mLock) {
1307 if (mLoaderTask != null) {
1308 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
1310 }
Joe Onorato36115782010-06-17 13:28:48 -04001311 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001313 /**
1314 * Loads the workspace screen ids in an ordered list.
1315 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001316 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001317 final ContentResolver contentResolver = context.getContentResolver();
1318 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001319
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001320 // Get screens ordered by rank.
1321 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1322 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1323 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001324 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001325 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001326 while (sc.moveToNext()) {
1327 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001328 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001329 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001330 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001331 }
1332 }
1333 } finally {
1334 sc.close();
1335 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001336 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001337 }
1338
Joe Onorato36115782010-06-17 13:28:48 -04001339 /**
1340 * Runnable for the thread that loads the contents of the launcher:
1341 * - workspace icons
1342 * - widgets
1343 * - all apps icons
1344 */
1345 private class LoaderTask implements Runnable {
1346 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001347 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001348 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001349 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001350 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001351
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001352 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001353 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001354 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 }
1356
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001357 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001358 mIsLoadingAndBindingWorkspace = true;
1359
Joe Onorato36115782010-06-17 13:28:48 -04001360 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001361 if (DEBUG_LOADERS) {
1362 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001364
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001365 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001366 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001367 synchronized (LoaderTask.this) {
1368 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001369 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001370 }
1371 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373 }
1374
Joe Onorato36115782010-06-17 13:28:48 -04001375 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001376 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001377 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001378
Joe Onorato36115782010-06-17 13:28:48 -04001379 private void waitForIdle() {
1380 // Wait until the either we're stopped or the other threads are done.
1381 // This way we don't start loading all apps until the workspace has settled
1382 // down.
1383 synchronized (LoaderTask.this) {
1384 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001385
Joe Onorato36115782010-06-17 13:28:48 -04001386 mHandler.postIdle(new Runnable() {
1387 public void run() {
1388 synchronized (LoaderTask.this) {
1389 mLoadAndBindStepFinished = true;
1390 if (DEBUG_LOADERS) {
1391 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001392 }
Joe Onorato36115782010-06-17 13:28:48 -04001393 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001394 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001395 }
Joe Onorato36115782010-06-17 13:28:48 -04001396 });
1397
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001398 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001399 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001400 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1401 // wait no longer than 1sec at a time
1402 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001403 } catch (InterruptedException ex) {
1404 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 }
1406 }
Joe Onorato36115782010-06-17 13:28:48 -04001407 if (DEBUG_LOADERS) {
1408 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001409 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001410 + "ms for previous step to finish binding");
1411 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001414
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001415 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001416 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001417 // Ensure that we have a valid page index to load synchronously
1418 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1419 "valid page index");
1420 }
1421 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1422 // Ensure that we don't try and bind a specified page when the pages have not been
1423 // loaded already (we should load everything asynchronously in that case)
1424 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1425 }
1426 synchronized (mLock) {
1427 if (mIsLoaderTaskRunning) {
1428 // Ensure that we are never running the background loading at this point since
1429 // we also touch the background collections
1430 throw new RuntimeException("Error! Background loading is already running");
1431 }
1432 }
1433
1434 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1435 // data structures, we can't allow any other thread to touch that data, but because
1436 // this call is synchronous, we can get away with not locking).
1437
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001438 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001439 // operations from the previous activity. We need to ensure that all queued operations
1440 // are executed before any synchronous binding work is done.
1441 mHandler.flush();
1442
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001443 // Divide the set of loaded items into those that we are binding synchronously, and
1444 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001445 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1447 // arise from that.
1448 onlyBindAllApps();
1449 }
1450
Joe Onorato36115782010-06-17 13:28:48 -04001451 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001453 if (mStopped) {
1454 return;
1455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 mIsLoaderTaskRunning = true;
1457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 // Optimize for end-user experience: if the Launcher is up and // running with the
1459 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1460 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001461 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001462 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001464
Joe Onorato36115782010-06-17 13:28:48 -04001465 if (mStopped) {
1466 break keep_running;
1467 }
1468
Joe Onorato36115782010-06-17 13:28:48 -04001469 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001470
1471 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001472 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1473 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475
Joe Onorato36115782010-06-17 13:28:48 -04001476 // Clear out this reference, otherwise we end up holding it until all of the
1477 // callback runnables are done.
1478 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479
Joe Onorato36115782010-06-17 13:28:48 -04001480 synchronized (mLock) {
1481 // If we are still the last one to be scheduled, remove ourselves.
1482 if (mLoaderTask == this) {
1483 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001485 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001486 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001487 }
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
1489
1490 public void stopLocked() {
1491 synchronized (LoaderTask.this) {
1492 mStopped = true;
1493 this.notify();
1494 }
1495 }
1496
1497 /**
1498 * Gets the callbacks object. If we've been stopped, or if the launcher object
1499 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1500 * object that was around when the deferred message was scheduled, and if there's
1501 * a new Callbacks object around then also return null. This will save us from
1502 * calling onto it with data that will be ignored.
1503 */
1504 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1505 synchronized (mLock) {
1506 if (mStopped) {
1507 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509
1510 if (mCallbacks == null) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 final Callbacks callbacks = mCallbacks.get();
1515 if (callbacks != oldCallbacks) {
1516 return null;
1517 }
1518 if (callbacks == null) {
1519 Log.w(TAG, "no mCallbacks");
1520 return null;
1521 }
1522
1523 return callbacks;
1524 }
1525 }
1526
1527 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001528 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1529 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001530 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001531 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001532 final int countX = profile.numColumns;
1533 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001534
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001536 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001537 // Return early if we detect that an item is under the hotseat button
1538 if (mCallbacks == null ||
1539 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001540 Log.e(TAG, "Error loading shortcut into hotseat " + item
1541 + " into position (" + item.screenId + ":" + item.cellX + ","
1542 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001543 return false;
1544 }
1545
Dan Sandler295ae182013-12-10 16:05:47 -05001546 final ItemInfo[][] hotseatItems =
1547 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1548
Adam Cohen2e6da152015-05-06 11:42:25 -07001549 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001550 Log.e(TAG, "Error loading shortcut " + item
1551 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001552 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001553 + ")");
1554 return false;
1555 }
1556
Dan Sandler295ae182013-12-10 16:05:47 -05001557 if (hotseatItems != null) {
1558 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 Log.e(TAG, "Error loading shortcut into hotseat " + item
1560 + " into position (" + item.screenId + ":" + item.cellX + ","
1561 + item.cellY + ") occupied by "
1562 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1563 [(int) item.screenId][0]);
1564 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001565 } else {
1566 hotseatItems[(int) item.screenId][0] = item;
1567 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001569 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001570 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 items[(int) item.screenId][0] = item;
1572 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001573 return true;
1574 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001575 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1576 if (!workspaceScreens.contains((Long) item.screenId)) {
1577 // The item has an invalid screen id.
1578 return false;
1579 }
1580 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001586 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 occupied.put(item.screenId, items);
1588 }
1589
Dan Sandler295ae182013-12-10 16:05:47 -05001590 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001591 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1592 item.cellX < 0 || item.cellY < 0 ||
1593 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1594 Log.e(TAG, "Error loading shortcut " + item
1595 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1596 + item.cellX + "," + item.cellY
1597 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1598 return false;
1599 }
1600
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001601 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001602 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1603 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001604 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001605 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001607 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001608 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001610 return false;
1611 }
1612 }
1613 }
1614 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1615 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001617 }
1618 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001619
Joe Onorato36115782010-06-17 13:28:48 -04001620 return true;
1621 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622
Winson Chungba9c37f2013-08-30 14:11:37 -07001623 /** Clears all the sBg data structures */
1624 private void clearSBgDataStructures() {
1625 synchronized (sBgLock) {
1626 sBgWorkspaceItems.clear();
1627 sBgAppWidgets.clear();
1628 sBgFolders.clear();
1629 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001630 sBgWorkspaceScreens.clear();
1631 }
1632 }
1633
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001634 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001635 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636
Joe Onorato36115782010-06-17 13:28:48 -04001637 final Context context = mContext;
1638 final ContentResolver contentResolver = context.getContentResolver();
1639 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001640 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001641 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001642 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001643
Winson Chung892c74d2013-08-22 16:15:50 -07001644 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001645 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001646 int countX = profile.numColumns;
1647 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001648
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001649 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001650 long migrationStartTime = System.currentTimeMillis();
1651 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001652 try {
1653 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1654 // Clear the flags before starting the task, so that we do not run the task
1655 // again, in case there was an uncaught error.
1656 MigrateFromRestoreTask.clearFlags(mContext);
1657 task.execute();
1658 } catch (Exception e) {
1659 Log.e(TAG, "Error during grid migration", e);
1660
1661 // Clear workspace.
1662 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1663 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001664 Log.v(TAG, "Workspace migration completed in "
1665 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001666 }
1667
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001669 Log.d(TAG, "loadWorkspace: resetting launcher database");
Dan Sandlerd5024042014-01-09 15:01:33 -05001670 LauncherAppState.getLauncherProvider().deleteDatabase();
1671 }
1672
1673 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1674 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001675 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1677 } else {
1678 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001679 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001680 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001681 }
Adam Cohene25af792013-06-06 23:08:25 -07001682
Winson Chung2abf94d2012-07-18 18:16:38 -07001683 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001684 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001685 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001686 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001687 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001688
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001689 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001690 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001691 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001692 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001693 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 // +1 for the hotseat (it can be larger than the workspace)
1696 // Load workspace in reverse order to ensure that latest items are loaded first (and
1697 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001698 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 try {
1701 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1702 final int intentIndex = c.getColumnIndexOrThrow
1703 (LauncherSettings.Favorites.INTENT);
1704 final int titleIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 final int containerIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.CONTAINER);
1708 final int itemTypeIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.ITEM_TYPE);
1710 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001712 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1713 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 final int screenIndex = c.getColumnIndexOrThrow(
1715 LauncherSettings.Favorites.SCREEN);
1716 final int cellXIndex = c.getColumnIndexOrThrow
1717 (LauncherSettings.Favorites.CELLX);
1718 final int cellYIndex = c.getColumnIndexOrThrow
1719 (LauncherSettings.Favorites.CELLY);
1720 final int spanXIndex = c.getColumnIndexOrThrow
1721 (LauncherSettings.Favorites.SPANX);
1722 final int spanYIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001724 final int rankIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001726 final int restoredIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001728 final int profileIdIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001730 final int optionsIndex = c.getColumnIndexOrThrow(
1731 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001732 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733
Sunny Goyal7f834d22015-04-21 10:10:23 -07001734 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1735 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1736 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1737 }
1738
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 ShortcutInfo info;
1740 String intentDescription;
1741 LauncherAppWidgetInfo appWidgetInfo;
1742 int container;
1743 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001744 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001746 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 while (!mStopped && c.moveToNext()) {
1749 try {
1750 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001751 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001752 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001753 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 switch (itemType) {
1756 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1757 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001758 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001759 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001760 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001761 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001762 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001763 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001764 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001765 if (user == null) {
1766 // User has been deleted remove the item.
1767 itemsToRemove.add(id);
1768 continue;
1769 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001770 try {
1771 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001772 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001773 if (cn != null && cn.getPackageName() != null) {
1774 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1775 cn.getPackageName(), user);
1776 boolean validComponent = validPkg &&
1777 launcherApps.isActivityEnabledForProfile(cn, user);
1778
1779 if (validComponent) {
1780 if (restored) {
1781 // no special handling necessary for this item
1782 restoredRows.add(id);
1783 restored = false;
1784 }
1785 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001786 intent = null;
1787 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1788 // We allow auto install apps to have their intent
1789 // updated after an install.
1790 intent = manager.getLaunchIntentForPackage(
1791 cn.getPackageName());
1792 if (intent != null) {
1793 ContentValues values = new ContentValues();
1794 values.put(LauncherSettings.Favorites.INTENT,
1795 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001796 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001797 }
1798 }
1799
1800 if (intent == null) {
1801 // The app is installed but the component is no
1802 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001803 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001804 itemsToRemove.add(id);
1805 continue;
1806 } else {
1807 // no special handling necessary for this item
1808 restoredRows.add(id);
1809 restored = false;
1810 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001811 } else if (restored) {
1812 // Package is not yet available but might be
1813 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001814 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001815
1816 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1817 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001818 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001819 // App restore has started. Update the flag
1820 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1821 ContentValues values = new ContentValues();
1822 values.put(LauncherSettings.Favorites.RESTORED,
1823 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001824 updateItem(id, values);
1825 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1826 // This is a common app. Try to replace this.
1827 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1828 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1829 if (parser.findDefaultApp()) {
1830 // Default app found. Replace it.
1831 intent = parser.parsedIntent;
1832 cn = intent.getComponent();
1833 ContentValues values = parser.parsedValues;
1834 values.put(LauncherSettings.Favorites.RESTORED, 0);
1835 updateItem(id, values);
1836 restored = false;
1837 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001838
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001840 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001841 itemsToRemove.add(id);
1842 continue;
1843 }
Sunny Goyal94485362014-09-18 16:13:58 -07001844 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001845 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001846 itemsToRemove.add(id);
1847 continue;
1848 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001849 } else if (launcherApps.isAppEnabled(
1850 manager, cn.getPackageName(),
1851 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1852 // Package is present but not available.
1853 allowMissingTarget = true;
1854 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1855 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001856 // SdCard is not ready yet. Package might get available,
1857 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001858 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001859 HashSet<String> pkgs = sPendingPackages.get(user);
1860 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001861 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001862 sPendingPackages.put(user, pkgs);
1863 }
1864 pkgs.add(cn.getPackageName());
1865 allowMissingTarget = true;
1866 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001867
1868 } else {
1869 // Do not wait for external media load anymore.
1870 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001871 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001872 itemsToRemove.add(id);
1873 continue;
Winson Chungee055712013-07-30 14:46:24 -07001874 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001875 } else if (cn == null) {
1876 // For shortcuts with no component, keep them as they are
1877 restoredRows.add(id);
1878 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001879 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001881 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001882 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 continue;
1884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001885
Sunny Goyal34b65272015-03-11 16:56:52 -07001886 boolean useLowResIcon = container >= 0 &&
1887 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1888
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001889 if (itemReplaced) {
1890 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001891 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001892 cursorIconInfo.iconIndex, titleIndex,
1893 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001894 } else {
1895 // Don't replace items for other profiles.
1896 itemsToRemove.add(id);
1897 continue;
1898 }
1899 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001900 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001901 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001902 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001903 intent = getRestoredItemIntent(c, context, intent);
1904 } else {
1905 // Don't restore items for other profiles.
1906 itemsToRemove.add(id);
1907 continue;
1908 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001909 } else if (itemType ==
1910 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001911 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001912 cursorIconInfo.iconIndex, titleIndex,
1913 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001915 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001916
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 // App shortcuts that used to be automatically added to Launcher
1918 // didn't always have the correct intent flags set, so do that
1919 // here
1920 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001921 intent.getCategories() != null &&
1922 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001923 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 intent.addFlags(
1925 Intent.FLAG_ACTIVITY_NEW_TASK |
1926 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1927 }
Michael Jurka96879562012-03-22 05:54:33 -07001928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001930 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001931 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001935 info.cellX = c.getInt(cellXIndex);
1936 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001937 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001938 info.spanX = 1;
1939 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001940 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001941 if (info.promisedIntent != null) {
1942 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1943 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001944 info.isDisabled = disabledState;
1945 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1946 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1947 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001948
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001949 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001950 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001951 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 break;
1953 }
1954
Sunny Goyal756adbc2015-04-16 15:20:51 -07001955 if (restored) {
1956 ComponentName cn = info.getTargetComponent();
1957 if (cn != null) {
1958 Integer progress = installingPkgs.get(cn.getPackageName());
1959 if (progress != null) {
1960 info.setInstallProgress(progress);
1961 } else {
1962 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1963 }
1964 }
1965 }
1966
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 switch (container) {
1968 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1969 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1970 sBgWorkspaceItems.add(info);
1971 break;
1972 default:
1973 // Item is in a user folder
1974 FolderInfo folderInfo =
1975 findOrMakeFolder(sBgFolders, container);
1976 folderInfo.add(info);
1977 break;
1978 }
1979 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001980 } else {
1981 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 }
1983 break;
1984
1985 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1986 id = c.getLong(idIndex);
1987 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1988
Sunny Goyala508e4f2015-05-21 09:33:57 -07001989 // Do not trim the folder label, as is was set by the user.
1990 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 folderInfo.cellX = c.getInt(cellXIndex);
1995 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001996 folderInfo.spanX = 1;
1997 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001998 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999
Daniel Sandler8802e962010-05-26 16:28:16 -04002000 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002001 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002002 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002003 break;
2004 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002005
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2008 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2009 sBgWorkspaceItems.add(folderInfo);
2010 break;
Joe Onorato36115782010-06-17 13:28:48 -04002011 }
Joe Onorato17a89222011-02-08 17:26:11 -08002012
Chris Wrenf4d08112014-01-16 18:13:56 -05002013 if (restored) {
2014 // no special handling required for restored folders
2015 restoredRows.add(id);
2016 }
2017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2019 sBgFolders.put(folderInfo.id, folderInfo);
2020 break;
2021
2022 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002023 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002025 boolean customWidget = itemType ==
2026 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002029 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002030 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002031 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002032 user = allUsers.get(serialNumber);
2033 if (user == null) {
2034 itemsToRemove.add(id);
2035 continue;
2036 }
2037
Sunny Goyalff572272014-07-23 13:58:07 -07002038 final ComponentName component =
2039 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002040
Sunny Goyal651077b2014-06-30 14:15:31 -07002041 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002042 final boolean isIdValid = (restoreStatus &
2043 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002044 final boolean wasProviderReady = (restoreStatus &
2045 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002046
Adam Cohen59400422014-03-05 18:07:04 -08002047 final LauncherAppWidgetProviderInfo provider =
2048 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002049 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002050 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002051
2052 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002053 if (!isSafeMode && !customWidget &&
2054 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002055 addDumpLog("Deleting widget that isn't installed anymore: "
2056 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 itemsToRemove.add(id);
2058 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002059 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002060 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2061 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002062
Sunny Goyal53f96722015-07-13 19:54:53 -07002063 // The provider is available. So the widget is either
2064 // available or not available. We do not need to track
2065 // any future restore updates.
2066 int status = restoreStatus &
2067 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002068 if (!wasProviderReady) {
2069 // If provider was not previously ready, update the
2070 // status and UI flag.
2071
2072 // Id would be valid only if the widget restore broadcast was received.
2073 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002074 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002075 } else {
2076 status &= ~LauncherAppWidgetInfo
2077 .FLAG_PROVIDER_NOT_READY;
2078 }
2079 }
2080 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002081 } else {
2082 Log.v(TAG, "Widget restore pending id=" + id
2083 + " appWidgetId=" + appWidgetId
2084 + " status =" + restoreStatus);
2085 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002086 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002087 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002088 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002089
2090 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2091 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002092 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002093 // App restore has started. Update the flag
2094 appWidgetInfo.restoreStatus |=
2095 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002096 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002097 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002098 itemsToRemove.add(id);
2099 continue;
2100 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002101
2102 appWidgetInfo.installProgress =
2103 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002104 }
Sunny Goyalff572272014-07-23 13:58:07 -07002105
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002107 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 appWidgetInfo.cellX = c.getInt(cellXIndex);
2109 appWidgetInfo.cellY = c.getInt(cellYIndex);
2110 appWidgetInfo.spanX = c.getInt(spanXIndex);
2111 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002112 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002113
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2115 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2116 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002117 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2118 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 continue;
2120 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002122 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002124 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002125 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 break;
2127 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002128
Adam Cohen59400422014-03-05 18:07:04 -08002129 if (!customWidget) {
2130 String providerName =
2131 appWidgetInfo.providerName.flattenToString();
2132 if (!providerName.equals(savedProvider) ||
2133 (appWidgetInfo.restoreStatus != restoreStatus)) {
2134 ContentValues values = new ContentValues();
2135 values.put(
2136 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2137 providerName);
2138 values.put(LauncherSettings.Favorites.RESTORED,
2139 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002140 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002141 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002142 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2144 sBgAppWidgets.add(appWidgetInfo);
2145 }
Joe Onorato36115782010-06-17 13:28:48 -04002146 break;
2147 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002149 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002150 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 }
2152 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002153 if (c != null) {
2154 c.close();
2155 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 }
2157
Winson Chungba9c37f2013-08-30 14:11:37 -07002158 // Break early if we've stopped loading
2159 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002160 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002161 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002162 }
2163
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002165 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002166 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2167 Utilities.createDbSelectionQuery(
2168 LauncherSettings.Favorites._ID, itemsToRemove), null);
2169 if (DEBUG_LOADERS) {
2170 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2171 LauncherSettings.Favorites._ID, itemsToRemove));
2172 }
2173
2174 // Remove any empty folder
2175 for (long folderId : LauncherAppState.getLauncherProvider()
2176 .deleteEmptyFolders()) {
2177 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2178 sBgFolders.remove(folderId);
2179 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002180 }
2181 }
2182
Sunny Goyal317698b2015-07-29 11:45:41 -07002183 // Sort all the folder items and make sure the first 3 items are high resolution.
2184 for (FolderInfo folder : sBgFolders) {
2185 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2186 int pos = 0;
2187 for (ShortcutInfo info : folder.contents) {
2188 if (info.usingLowResIcon) {
2189 info.updateIcon(mIconCache, false);
2190 }
2191 pos ++;
2192 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2193 break;
2194 }
2195 }
2196 }
2197
Chris Wrenf4d08112014-01-16 18:13:56 -05002198 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002199 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002200 ContentValues values = new ContentValues();
2201 values.put(LauncherSettings.Favorites.RESTORED, 0);
2202 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2203 Utilities.createDbSelectionQuery(
2204 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002205 }
2206
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002207 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2208 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002209 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002210 null, sWorker);
2211 }
2212
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002213 // Remove any empty screens
2214 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002215 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002216 long screenId = item.screenId;
2217 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2218 unusedScreens.contains(screenId)) {
2219 unusedScreens.remove(screenId);
2220 }
2221 }
2222
2223 // If there are any empty screens remove them, and update.
2224 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002225 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 }
2228
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 if (DEBUG_LOADERS) {
2230 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2231 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002233 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002235
Sunny Goyale2df0622015-04-24 11:27:00 -07002236 for (int i = 0; i < nScreens; i++) {
2237 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002238 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 line += " | ";
2240 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002241 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002242 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002243 if (x < screen.length && y < screen[x].length) {
2244 line += (screen[x][y] != null) ? "#" : ".";
2245 } else {
2246 line += "!";
2247 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 }
Joe Onorato36115782010-06-17 13:28:48 -04002249 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 }
Joe Onorato36115782010-06-17 13:28:48 -04002253 }
Adam Cohene25af792013-06-06 23:08:25 -07002254 }
2255
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002256 /**
2257 * Partially updates the item without any notification. Must be called on the worker thread.
2258 */
2259 private void updateItem(long itemId, ContentValues update) {
2260 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002261 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002262 update,
2263 BaseColumns._ID + "= ?",
2264 new String[]{Long.toString(itemId)});
2265 }
2266
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 /** Filters the set of items who are directly or indirectly (via another container) on the
2268 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002269 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 ArrayList<ItemInfo> allWorkspaceItems,
2271 ArrayList<ItemInfo> currentScreenItems,
2272 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002273 // Purge any null ItemInfos
2274 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2275 while (iter.hasNext()) {
2276 ItemInfo i = iter.next();
2277 if (i == null) {
2278 iter.remove();
2279 }
2280 }
2281
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 // Order the set of items by their containers first, this allows use to walk through the
2283 // list sequentially, build up a list of containers that are in the specified screen,
2284 // as well as all items in those containers.
2285 Set<Long> itemsOnScreen = new HashSet<Long>();
2286 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2287 @Override
2288 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002289 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 }
2291 });
2292 for (ItemInfo info : allWorkspaceItems) {
2293 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002294 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 currentScreenItems.add(info);
2296 itemsOnScreen.add(info.id);
2297 } else {
2298 otherScreenItems.add(info);
2299 }
2300 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2301 currentScreenItems.add(info);
2302 itemsOnScreen.add(info.id);
2303 } else {
2304 if (itemsOnScreen.contains(info.container)) {
2305 currentScreenItems.add(info);
2306 itemsOnScreen.add(info.id);
2307 } else {
2308 otherScreenItems.add(info);
2309 }
2310 }
2311 }
2312 }
2313
2314 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002315 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 ArrayList<LauncherAppWidgetInfo> appWidgets,
2317 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2318 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319
2320 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002321 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002323 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 currentScreenWidgets.add(widget);
2325 } else {
2326 otherScreenWidgets.add(widget);
2327 }
2328 }
2329 }
2330
2331 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002332 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002333 LongArrayMap<ItemInfo> itemsIdMap,
2334 LongArrayMap<FolderInfo> folders,
2335 LongArrayMap<FolderInfo> currentScreenFolders,
2336 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337
Sunny Goyale2df0622015-04-24 11:27:00 -07002338 int total = folders.size();
2339 for (int i = 0; i < total; i++) {
2340 long id = folders.keyAt(i);
2341 FolderInfo folder = folders.valueAt(i);
2342
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002344 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002346 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 currentScreenFolders.put(id, folder);
2348 } else {
2349 otherScreenFolders.put(id, folder);
2350 }
2351 }
2352 }
2353
2354 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2355 * right) */
2356 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002357 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002358 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002359 final int screenCols = profile.numColumns;
2360 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002362 @Override
2363 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002364 if (lhs.container == rhs.container) {
2365 // Within containers, order by their spatial position in that container
2366 switch ((int) lhs.container) {
2367 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2368 long lr = (lhs.screenId * screenCellCount +
2369 lhs.cellY * screenCols + lhs.cellX);
2370 long rr = (rhs.screenId * screenCellCount +
2371 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002372 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002373 }
2374 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2375 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002376 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002377 }
2378 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002379 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002380 throw new RuntimeException("Unexpected container type when " +
2381 "sorting workspace items.");
2382 }
2383 return 0;
2384 }
2385 } else {
2386 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002387 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002388 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002389 }
2390 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002392
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2394 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 final Runnable r = new Runnable() {
2396 @Override
2397 public void run() {
2398 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2399 if (callbacks != null) {
2400 callbacks.bindScreens(orderedScreens);
2401 }
2402 }
2403 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002404 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002405 }
2406
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2408 final ArrayList<ItemInfo> workspaceItems,
2409 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002410 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002411 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412
2413 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002414 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002416 final int start = i;
2417 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 final Runnable r = new Runnable() {
2419 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002421 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002423 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2424 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002425 }
2426 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002428 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002429 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430
2431 // Bind the folders
2432 if (!folders.isEmpty()) {
2433 final Runnable r = new Runnable() {
2434 public void run() {
2435 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2436 if (callbacks != null) {
2437 callbacks.bindFolders(folders);
2438 }
2439 }
2440 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002441 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 }
2443
2444 // Bind the widgets, one at a time
2445 N = appWidgets.size();
2446 for (int i = 0; i < N; i++) {
2447 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2448 final Runnable r = new Runnable() {
2449 public void run() {
2450 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2451 if (callbacks != null) {
2452 callbacks.bindAppWidget(widget);
2453 }
2454 }
2455 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002456 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 }
2458 }
2459
2460 /**
2461 * Binds all loaded data to actual views on the main thread.
2462 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002463 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 final long t = SystemClock.uptimeMillis();
2465 Runnable r;
2466
2467 // Don't use these two variables in any of the callback runnables.
2468 // Otherwise we hold a reference to them.
2469 final Callbacks oldCallbacks = mCallbacks.get();
2470 if (oldCallbacks == null) {
2471 // This launcher has exited and nobody bothered to tell us. Just bail.
2472 Log.w(TAG, "LoaderTask running with no launcher");
2473 return;
2474 }
2475
Winson Chung9b9fb962013-11-15 15:39:34 -08002476 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2478 ArrayList<LauncherAppWidgetInfo> appWidgets =
2479 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002480 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002481
2482 final LongArrayMap<FolderInfo> folders;
2483 final LongArrayMap<ItemInfo> itemsIdMap;
2484
Winson Chung2abf94d2012-07-18 18:16:38 -07002485 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 workspaceItems.addAll(sBgWorkspaceItems);
2487 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002488 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002489
2490 folders = sBgFolders.clone();
2491 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 }
2493
Derek Prothro7aff3992013-12-10 14:00:37 -05002494 final boolean isLoadingSynchronously =
2495 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002496 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002497 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002498 if (currScreen >= orderedScreenIds.size()) {
2499 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002500 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002501 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002502 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002503 final long currentScreenId = currentScreen < 0
2504 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002505
2506 // Load all the items that are on the current page first (and in the process, unbind
2507 // all the existing workspace items before we call startBinding() below.
2508 unbindWorkspaceItemsOnMainThread();
2509
2510 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2512 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2513 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2514 new ArrayList<LauncherAppWidgetInfo>();
2515 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2516 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002517 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2518 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519
Winson Chung9b9fb962013-11-15 15:39:34 -08002520 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002522 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002524 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 otherFolders);
2526 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2527 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2528
2529 // Tell the workspace that we're about to start binding items
2530 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002531 public void run() {
2532 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2533 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002534 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002536 }
2537 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002539 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540
Adam Cohendcd297f2013-06-18 13:13:40 -07002541 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2542
Sunny Goyal527c7d32015-08-28 15:19:36 -07002543 Executor mainExecutor = new DeferredMainThreadExecutor();
2544 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002546 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547
Sunny Goyal527c7d32015-08-28 15:19:36 -07002548 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2549 // remaining screens after first onDraw is called. This ensures that the first screen
2550 // is immediately visible (eg. during rotation)
2551 // In case of !isLoadingSynchronously, bind all pages one after other.
2552 final Executor deferredExecutor = isLoadingSynchronously ?
2553 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2554
2555 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2556 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557
2558 // Tell the workspace that we're done binding items
2559 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002560 public void run() {
2561 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2562 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002563 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002564 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565
Sunny Goyal639e9062015-08-19 19:17:06 -07002566 mIsLoadingAndBindingWorkspace = false;
2567
2568 // Run all the bind complete runnables after workspace is bound.
2569 if (!mBindCompleteRunnables.isEmpty()) {
2570 synchronized (mBindCompleteRunnables) {
2571 for (final Runnable r : mBindCompleteRunnables) {
2572 runOnWorkerThread(r);
2573 }
2574 mBindCompleteRunnables.clear();
2575 }
2576 }
2577
Winson Chung98e030b2012-05-07 16:01:11 -07002578 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002579 if (DEBUG_LOADERS) {
2580 Log.d(TAG, "bound workspace in "
2581 + (SystemClock.uptimeMillis()-t) + "ms");
2582 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002583
Joe Onorato36115782010-06-17 13:28:48 -04002584 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002586 deferredExecutor.execute(r);
2587
Winson Chung4a2afa32012-07-19 14:53:05 -07002588 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002589 r = new Runnable() {
2590 public void run() {
2591 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2592 if (callbacks != null) {
2593 // We are loading synchronously, which means, some of the pages will be
2594 // bound after first draw. Inform the callbacks that page binding is
2595 // not complete, and schedule the remaining pages.
2596 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2597 callbacks.onPageBoundSynchronously(currentScreen);
2598 }
2599 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2600 }
2601 }
2602 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002603 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002604 }
Joe Onorato36115782010-06-17 13:28:48 -04002605 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002606
Joe Onorato36115782010-06-17 13:28:48 -04002607 private void loadAndBindAllApps() {
2608 if (DEBUG_LOADERS) {
2609 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2610 }
2611 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002612 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002613 synchronized (LoaderTask.this) {
2614 if (mStopped) {
2615 return;
2616 }
2617 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002618 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002619 synchronized (LoaderTask.this) {
2620 if (mStopped) {
2621 return;
2622 }
2623 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002624 }
Joe Onorato36115782010-06-17 13:28:48 -04002625 } else {
2626 onlyBindAllApps();
2627 }
2628 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002629
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002630 private void updateIconCache() {
2631 // Ignore packages which have a promise icon.
2632 HashSet<String> packagesToIgnore = new HashSet<>();
2633 synchronized (sBgLock) {
2634 for (ItemInfo info : sBgItemsIdMap) {
2635 if (info instanceof ShortcutInfo) {
2636 ShortcutInfo si = (ShortcutInfo) info;
2637 if (si.isPromise() && si.getTargetComponent() != null) {
2638 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2639 }
2640 } else if (info instanceof LauncherAppWidgetInfo) {
2641 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2642 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2643 packagesToIgnore.add(lawi.providerName.getPackageName());
2644 }
2645 }
2646 }
2647 }
2648 mIconCache.updateDbIcons(packagesToIgnore);
2649 }
2650
Joe Onorato36115782010-06-17 13:28:48 -04002651 private void onlyBindAllApps() {
2652 final Callbacks oldCallbacks = mCallbacks.get();
2653 if (oldCallbacks == null) {
2654 // This launcher has exited and nobody bothered to tell us. Just bail.
2655 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2656 return;
2657 }
2658
2659 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002660 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002661 final ArrayList<AppInfo> list
2662 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002663 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002664 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002665 public void run() {
2666 final long t = SystemClock.uptimeMillis();
2667 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2668 if (callbacks != null) {
2669 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002670 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
2672 if (DEBUG_LOADERS) {
2673 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2674 + (SystemClock.uptimeMillis()-t) + "ms");
2675 }
2676 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002677 };
2678 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002679 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002680 r.run();
2681 } else {
2682 mHandler.post(r);
2683 }
Joe Onorato36115782010-06-17 13:28:48 -04002684 }
2685
Winson Chung64359a52013-07-08 17:17:08 -07002686 private void loadAllApps() {
2687 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002688
Joe Onorato36115782010-06-17 13:28:48 -04002689 final Callbacks oldCallbacks = mCallbacks.get();
2690 if (oldCallbacks == null) {
2691 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002692 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002693 return;
2694 }
2695
Kenny Guyed131872014-04-30 03:02:21 +01002696 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2697
Winson Chung64359a52013-07-08 17:17:08 -07002698 // Clear the list of apps
2699 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002700 for (UserHandleCompat user : profiles) {
2701 // Query for the set of apps
2702 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002703 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002704 if (DEBUG_LOADERS) {
2705 Log.d(TAG, "getActivityList took "
2706 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2707 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2708 }
2709 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002710 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002711 if (apps == null || apps.isEmpty()) {
2712 return;
2713 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002714
Kenny Guyed131872014-04-30 03:02:21 +01002715 // Create the ApplicationInfos
2716 for (int i = 0; i < apps.size(); i++) {
2717 LauncherActivityInfoCompat app = apps.get(i);
2718 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002719 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002720 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002721
Sunny Goyal756a28a2015-04-23 17:07:55 -07002722 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2723 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002724 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002725
2726 @Override
2727 public void run() {
2728 heuristic.processUserApps(apps);
2729 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002730 };
2731 runOnMainThread(new Runnable() {
2732
2733 @Override
2734 public void run() {
2735 // Check isLoadingWorkspace on the UI thread, as it is updated on
2736 // the UI thread.
2737 if (mIsLoadingAndBindingWorkspace) {
2738 synchronized (mBindCompleteRunnables) {
2739 mBindCompleteRunnables.add(r);
2740 }
2741 } else {
2742 runOnWorkerThread(r);
2743 }
2744 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002745 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002746 }
Winson Chung64359a52013-07-08 17:17:08 -07002747 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002748 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002749 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2750 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002751
2752 // Post callback on main thread
2753 mHandler.post(new Runnable() {
2754 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002755
Winson Chung64359a52013-07-08 17:17:08 -07002756 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002757 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002758 if (callbacks != null) {
2759 callbacks.bindAllApplications(added);
2760 if (DEBUG_LOADERS) {
2761 Log.d(TAG, "bound " + added.size() + " apps in "
2762 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2763 }
2764 } else {
2765 Log.i(TAG, "not binding apps: no Launcher activity");
2766 }
2767 }
2768 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002769 // Cleanup any data stored for a deleted user.
2770 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002771
Sunny Goyal2d648b02015-08-11 13:56:28 -07002772 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002773 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002774 Log.d(TAG, "Icons processed in "
2775 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002776 }
2777 }
2778
2779 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002780 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002781 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002782 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2783 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2784 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2785 }
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
2787 }
2788
Sunny Goyal75b0f552015-05-20 21:57:06 -07002789 /**
2790 * Called when the icons for packages have been updated in the icon cache.
2791 */
2792 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2793 final Callbacks callbacks = getCallback();
2794 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2795 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2796
2797 // If any package icon has changed (app was updated while launcher was dead),
2798 // update the corresponding shortcuts.
2799 synchronized (sBgLock) {
2800 for (ItemInfo info : sBgItemsIdMap) {
2801 if (info instanceof ShortcutInfo && user.equals(info.user)
2802 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2803 ShortcutInfo si = (ShortcutInfo) info;
2804 ComponentName cn = si.getTargetComponent();
2805 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2806 si.updateIcon(mIconCache);
2807 updatedShortcuts.add(si);
2808 }
2809 }
2810 }
2811 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2812 }
2813
2814 if (!updatedShortcuts.isEmpty()) {
2815 final UserHandleCompat userFinal = user;
2816 mHandler.post(new Runnable() {
2817
2818 public void run() {
2819 Callbacks cb = getCallback();
2820 if (cb != null && callbacks == cb) {
2821 cb.bindShortcutsChanged(updatedShortcuts,
2822 new ArrayList<ShortcutInfo>(), userFinal);
2823 }
2824 }
2825 });
2826 }
2827
2828 if (!updatedApps.isEmpty()) {
2829 mHandler.post(new Runnable() {
2830
2831 public void run() {
2832 Callbacks cb = getCallback();
2833 if (cb != null && callbacks == cb) {
2834 cb.bindAppsUpdated(updatedApps);
2835 }
2836 }
2837 });
2838 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002839
2840 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002841 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002842 }
2843
Joe Onorato36115782010-06-17 13:28:48 -04002844 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002845 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002846 }
2847
Adam Cohen091440a2015-03-18 14:16:05 -07002848 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002849
2850 @Override
2851 public void onReceive(Context context, Intent intent) {
2852 synchronized (sBgLock) {
2853 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2854 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002855 final PackageManager manager = context.getPackageManager();
2856 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2857 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002858 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2859 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002860 packagesRemoved.clear();
2861 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002862 for (String pkg : entry.getValue()) {
2863 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002864 boolean packageOnSdcard = launcherApps.isAppEnabled(
2865 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2866 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002867 packagesUnavailable.add(pkg);
2868 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002869 packagesRemoved.add(pkg);
2870 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002871 }
2872 }
2873 if (!packagesRemoved.isEmpty()) {
2874 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2875 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2876 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002877 if (!packagesUnavailable.isEmpty()) {
2878 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2879 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2880 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002881 }
Sunny Goyal34942622014-08-29 17:20:55 -07002882 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002883 }
2884 }
2885 }
2886
Joe Onorato36115782010-06-17 13:28:48 -04002887 private class PackageUpdatedTask implements Runnable {
2888 int mOp;
2889 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002890 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002891
2892 public static final int OP_NONE = 0;
2893 public static final int OP_ADD = 1;
2894 public static final int OP_UPDATE = 2;
2895 public static final int OP_REMOVE = 3; // uninstlled
2896 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2897
2898
Kenny Guyed131872014-04-30 03:02:21 +01002899 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002900 mOp = op;
2901 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002902 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002903 }
2904
2905 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002906 if (!mHasLoaderCompletedOnce) {
2907 // Loader has not yet run.
2908 return;
2909 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002910 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002911
2912 final String[] packages = mPackages;
2913 final int N = packages.length;
2914 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002915 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002916 for (int i=0; i<N; i++) {
2917 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002918 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002919 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002920 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002921
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002922 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2923 if (heuristic != null) {
2924 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002925 }
Joe Onorato36115782010-06-17 13:28:48 -04002926 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002927 }
Joe Onorato36115782010-06-17 13:28:48 -04002928 case OP_UPDATE:
2929 for (int i=0; i<N; i++) {
2930 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002931 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002932 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002933 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002934 }
2935 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002936 case OP_REMOVE: {
2937 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2938 if (heuristic != null) {
2939 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002940 }
Joe Onorato36115782010-06-17 13:28:48 -04002941 for (int i=0; i<N; i++) {
2942 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002943 mIconCache.removeIconsForPkg(packages[i], mUser);
2944 }
2945 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002946 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002947 case OP_UNAVAILABLE:
2948 for (int i=0; i<N; i++) {
2949 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2950 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002951 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002952 }
2953 break;
2954 }
2955
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002956 ArrayList<AppInfo> added = null;
2957 ArrayList<AppInfo> modified = null;
2958 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002959
Adam Cohen487f7dd2012-06-28 18:12:10 -07002960 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002961 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002962 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002963 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002964 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002965 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002966 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002967 }
Winson Chung5d55f332012-07-16 20:45:03 -07002968 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002969 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002970 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002971 }
2972
Sunny Goyale0f58d72014-11-10 18:05:31 -08002973 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04002974 if (callbacks == null) {
2975 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2976 return;
2977 }
2978
Sunny Goyal4390ace2014-10-13 11:33:11 -07002979 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
2980 new HashMap<ComponentName, AppInfo>();
2981
Joe Onorato36115782010-06-17 13:28:48 -04002982 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002983 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002984 for (AppInfo ai : added) {
2985 addedOrUpdatedApps.put(ai.componentName, ai);
2986 }
Joe Onorato36115782010-06-17 13:28:48 -04002987 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002988
Joe Onorato36115782010-06-17 13:28:48 -04002989 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002990 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002991 for (AppInfo ai : modified) {
2992 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002993 }
2994
Joe Onorato36115782010-06-17 13:28:48 -04002995 mHandler.post(new Runnable() {
2996 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002997 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002998 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002999 callbacks.bindAppsUpdated(modifiedFinal);
3000 }
3001 }
3002 });
3003 }
Winson Chung83892cc2013-05-01 16:53:33 -07003004
Sunny Goyal4390ace2014-10-13 11:33:11 -07003005 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003006 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003007 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3008 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3009 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3010
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003011 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003012 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003013 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003014 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3015 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003016 boolean infoUpdated = false;
3017 boolean shortcutUpdated = false;
3018
3019 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003020 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003021 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003022 Bitmap icon = Utilities.createIconBitmap(
3023 si.iconResource.packageName,
3024 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003025 if (icon != null) {
3026 si.setIcon(icon);
3027 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003028 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003029 }
3030 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003031
3032 ComponentName cn = si.getTargetComponent();
3033 if (cn != null && packageSet.contains(cn.getPackageName())) {
3034 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3035
3036 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003037 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3038 // Auto install icon
3039 PackageManager pm = context.getPackageManager();
3040 ResolveInfo matched = pm.resolveActivity(
3041 new Intent(Intent.ACTION_MAIN)
3042 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3043 PackageManager.MATCH_DEFAULT_ONLY);
3044 if (matched == null) {
3045 // Try to find the best match activity.
3046 Intent intent = pm.getLaunchIntentForPackage(
3047 cn.getPackageName());
3048 if (intent != null) {
3049 cn = intent.getComponent();
3050 appInfo = addedOrUpdatedApps.get(cn);
3051 }
3052
3053 if ((intent == null) || (appInfo == null)) {
3054 removedShortcuts.add(si);
3055 continue;
3056 }
3057 si.promisedIntent = intent;
3058 }
3059 }
3060
3061 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003062 if (appInfo != null) {
3063 si.flags = appInfo.flags;
3064 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003065
Sunny Goyal756adbc2015-04-16 15:20:51 -07003066 si.intent = si.promisedIntent;
3067 si.promisedIntent = null;
3068 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003069 infoUpdated = true;
3070 si.updateIcon(mIconCache);
3071 }
3072
3073 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3074 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3075 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003076 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 si.contentDescription = appInfo.contentDescription;
3078 infoUpdated = true;
3079 }
3080
3081 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3082 // Since package was just updated, the target must be available now.
3083 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3084 shortcutUpdated = true;
3085 }
3086 }
3087
3088 if (infoUpdated || shortcutUpdated) {
3089 updatedShortcuts.add(si);
3090 }
3091 if (infoUpdated) {
3092 updateItemInDatabase(context, si);
3093 }
3094 } else if (info instanceof LauncherAppWidgetInfo) {
3095 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3096 if (mUser.equals(widgetInfo.user)
3097 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3098 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003099 widgetInfo.restoreStatus &=
3100 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3101 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003102
3103 // adding this flag ensures that launcher shows 'click to setup'
3104 // if the widget has a config activity. In case there is no config
3105 // activity, it will be marked as 'restored' during bind.
3106 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3107
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 widgets.add(widgetInfo);
3109 updateItemInDatabase(context, widgetInfo);
3110 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003111 }
3112 }
3113 }
3114
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3116 mHandler.post(new Runnable() {
3117
3118 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003119 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 if (callbacks == cb && cb != null) {
3121 callbacks.bindShortcutsChanged(
3122 updatedShortcuts, removedShortcuts, mUser);
3123 }
3124 }
3125 });
3126 if (!removedShortcuts.isEmpty()) {
3127 deleteItemsFromDatabase(context, removedShortcuts);
3128 }
3129 }
3130 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003131 mHandler.post(new Runnable() {
3132 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003133 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003134 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003136 }
3137 }
3138 });
3139 }
3140 }
3141
Winson Chungdf95eb12013-10-16 14:57:07 -07003142 final ArrayList<String> removedPackageNames =
3143 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003144 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003145 // Mark all packages in the broadcast to be removed
3146 removedPackageNames.addAll(Arrays.asList(packages));
3147 } else if (mOp == OP_UPDATE) {
3148 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003149 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003150 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003151 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003152 }
3153 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003154 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003155
Winson Chungdf95eb12013-10-16 14:57:07 -07003156 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003157 final int removeReason;
3158 if (mOp == OP_UNAVAILABLE) {
3159 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3160 } else {
3161 // Remove all the components associated with this package
3162 for (String pn : removedPackageNames) {
3163 deletePackageFromDatabase(context, pn, mUser);
3164 }
3165 // Remove all the specific components
3166 for (AppInfo a : removedApps) {
3167 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3168 deleteItemsFromDatabase(context, infos);
3169 }
3170 removeReason = 0;
3171 }
3172
Winson Chungdf95eb12013-10-16 14:57:07 -07003173 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003174 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003175 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003176 mHandler.post(new Runnable() {
3177 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003178 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003179 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003180 callbacks.bindComponentsRemoved(
3181 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003182 }
3183 }
3184 });
Joe Onoratobe386092009-11-17 17:32:16 -08003185 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003186
Sunny Goyal2d648b02015-08-11 13:56:28 -07003187 // Update widgets
3188 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3189 // Always refresh for a package event on secondary user
3190 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3191
3192 // Refresh widget list, if the package already had a widget.
3193 synchronized (sBgLock) {
3194 if (sBgWidgetProviders != null) {
3195 HashSet<String> pkgSet = new HashSet<>();
3196 Collections.addAll(pkgSet, mPackages);
3197
3198 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3199 needToRefresh |= key.user.equals(mUser) &&
3200 pkgSet.contains(key.componentName.getPackageName());
3201 }
3202 }
3203 }
3204
3205 if (!needToRefresh && mOp != OP_REMOVE) {
3206 // Refresh widget list, if there is any newly added widget
3207 PackageManager pm = context.getPackageManager();
3208 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003209 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003210 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003211 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003212 .setPackage(pkg), 0);
3213 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003214 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003215 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003216 throw e;
3217 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003218 // Ignore the crash. We can live with a state widget list.
3219 Log.e(TAG, "PM call failed for " + pkg, e);
3220 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003221 }
3222 }
3223
3224 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 }
3227 }
3228
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003229 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3230 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003231 ArrayList<LauncherAppWidgetProviderInfo> results =
3232 new ArrayList<LauncherAppWidgetProviderInfo>();
3233 try {
3234 synchronized (sBgLock) {
3235 if (sBgWidgetProviders == null || refresh) {
3236 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3237 = new HashMap<>();
3238 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3239 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003240
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003241 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3242 for (AppWidgetProviderInfo pInfo : widgets) {
3243 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3244 UserHandleCompat user = wm.getUser(info);
3245 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3246 }
Robin Lee26ace122015-03-16 19:41:43 +00003247
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003248 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3249 for (CustomAppWidget widget : customWidgets) {
3250 info = new LauncherAppWidgetProviderInfo(context, widget);
3251 UserHandleCompat user = wm.getUser(info);
3252 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3253 }
3254 // Replace the global list at the very end, so that if there is an exception,
3255 // previously loaded provider list is used.
3256 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003257 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003258 results.addAll(sBgWidgetProviders.values());
3259 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003260 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003261 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003262 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003263 (e.getCause() instanceof TransactionTooLargeException ||
3264 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003265 // the returned value may be incomplete and will not be refreshed until the next
3266 // time Launcher starts.
3267 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3268 // onResume is called to refresh the widget provider list.
3269 synchronized (sBgLock) {
3270 if (sBgWidgetProviders != null) {
3271 results.addAll(sBgWidgetProviders.values());
3272 }
3273 return results;
3274 }
3275 } else {
3276 throw e;
3277 }
Adam Cohen59400422014-03-05 18:07:04 -08003278 }
3279 }
3280
Robin Lee26ace122015-03-16 19:41:43 +00003281 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3282 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003283 synchronized (sBgLock) {
3284 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003285 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003286 }
Robin Lee26ace122015-03-16 19:41:43 +00003287 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003288 }
3289 }
3290
Sunny Goyal2d648b02015-08-11 13:56:28 -07003291 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003292
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003293 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003294 @Override
3295 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003296 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003297 final WidgetsModel model = mBgWidgetsModel.clone();
3298
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003299 mHandler.post(new Runnable() {
3300 @Override
3301 public void run() {
3302 Callbacks cb = getCallback();
3303 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003304 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003305 }
3306 }
3307 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003308 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003309 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3310 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003311 }
3312 });
3313 }
3314
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003315 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003316 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003317 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003318 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003319 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003320 @Thunk void updateWidgetsModel(boolean refresh) {
3321 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003322 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003323 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003324
3325 // Update shortcut providers
3326 synchronized (sBgLock) {
3327 try {
3328 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3329 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3330 sBgShortcutProviders = providers;
3331 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003332 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003333 (e.getCause() instanceof TransactionTooLargeException ||
3334 e.getCause() instanceof DeadObjectException)) {
3335 /**
3336 * Ignore exception and use the cached list if available.
3337 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3338 */
3339 } else {
3340 throw e;
3341 }
3342 }
3343 if (sBgShortcutProviders != null) {
3344 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3345 }
3346 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003347 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003348 }
3349
Adam Cohen091440a2015-03-18 14:16:05 -07003350 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003351 UserHandleCompat user) {
3352 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3353 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003354 }
Adam Cohen556f6132014-01-15 15:18:08 -08003355
Kenny Guyed131872014-04-30 03:02:21 +01003356 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3357 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003358 if (cn == null) {
3359 return false;
3360 }
Kenny Guyed131872014-04-30 03:02:21 +01003361 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3362 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003363 return false;
3364 }
Kenny Guyed131872014-04-30 03:02:21 +01003365 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003366 }
3367
Adam Cohena28b78e2014-05-20 17:03:04 -07003368 public static boolean isValidPackage(Context context, String packageName,
3369 UserHandleCompat user) {
3370 if (packageName == null) {
3371 return false;
3372 }
3373 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3374 return launcherApps.isPackageEnabledForProfile(packageName, user);
3375 }
3376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003378 * Make an ShortcutInfo object for a restored application or shortcut item that points
3379 * to a package that is not yet installed on the system.
3380 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003381 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003382 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003383 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003384 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003385
3386 Bitmap icon = iconInfo.loadIcon(c, info, context);
3387 // the fallback icon
3388 if (icon == null) {
3389 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3390 } else {
3391 info.setIcon(icon);
3392 }
Sunny Goyal34942622014-08-29 17:20:55 -07003393
3394 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003395 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003396 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003397 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003398 }
Sunny Goyal34942622014-08-29 17:20:55 -07003399 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3400 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003401 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003402 }
Sunny Goyal34942622014-08-29 17:20:55 -07003403 } else {
3404 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3405 }
3406
Winson Chung82b016c2015-05-08 17:00:10 -07003407 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003408 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003409 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003410 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003411 return info;
3412 }
3413
3414 /**
3415 * Make an Intent object for a restored application or shortcut item that points
3416 * to the market page for the item.
3417 */
Adam Cohen091440a2015-03-18 14:16:05 -07003418 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003419 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003420 return getMarketIntent(componentName.getPackageName());
3421 }
3422
3423 static Intent getMarketIntent(String packageName) {
3424 return new Intent(Intent.ACTION_VIEW)
3425 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003426 .scheme("market")
3427 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003428 .appendQueryParameter("id", packageName)
3429 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003430 }
3431
3432 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003433 * Make an ShortcutInfo object for a shortcut that is an application.
3434 *
3435 * If c is not null, then it will be used to fill in missing data like the title and icon.
3436 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003437 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003438 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003439 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003440 if (user == null) {
3441 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003442 return null;
3443 }
3444
Kenny Guyed131872014-04-30 03:02:21 +01003445 ComponentName componentName = intent.getComponent();
3446 if (componentName == null) {
3447 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3448 return null;
3449 }
3450
3451 Intent newIntent = new Intent(intent.getAction(), null);
3452 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3453 newIntent.setComponent(componentName);
3454 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003455 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003456 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3457 return null;
3458 }
3459
3460 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003461 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003462 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3463 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3464 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003465 }
3466
Joe Onorato56d82912010-03-07 14:32:10 -05003467 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003468 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003469 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003470 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003471
Joe Onorato56d82912010-03-07 14:32:10 -05003472 // fall back to the class name of the activity
3473 if (info.title == null) {
3474 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003475 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003478 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003479 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003480 if (lai != null) {
3481 info.flags = AppInfo.initFlags(lai);
3482 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003484 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003485
Sunny Goyale2df0622015-04-24 11:27:00 -07003486 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003487 ItemInfoFilter f) {
3488 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3489 for (ItemInfo i : infos) {
3490 if (i instanceof ShortcutInfo) {
3491 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003492 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003493 if (cn != null && f.filterItem(null, info, cn)) {
3494 filtered.add(info);
3495 }
3496 } else if (i instanceof FolderInfo) {
3497 FolderInfo info = (FolderInfo) i;
3498 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003499 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003500 if (cn != null && f.filterItem(info, s, cn)) {
3501 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003502 }
3503 }
Winson Chung64359a52013-07-08 17:17:08 -07003504 } else if (i instanceof LauncherAppWidgetInfo) {
3505 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3506 ComponentName cn = info.providerName;
3507 if (cn != null && f.filterItem(null, info, cn)) {
3508 filtered.add(info);
3509 }
Winson Chung8a435102012-08-30 17:16:53 -07003510 }
3511 }
Winson Chung64359a52013-07-08 17:17:08 -07003512 return new ArrayList<ItemInfo>(filtered);
3513 }
3514
Adam Cohen091440a2015-03-18 14:16:05 -07003515 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003516 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003517 ItemInfoFilter filter = new ItemInfoFilter() {
3518 @Override
3519 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003520 if (info.user == null) {
3521 return cn.equals(cname);
3522 } else {
3523 return cn.equals(cname) && info.user.equals(user);
3524 }
Winson Chung64359a52013-07-08 17:17:08 -07003525 }
3526 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003527 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003528 }
3529
Sunny Goyal1a745e82014-10-02 15:58:31 -07003530 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003531 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003532 */
Adam Cohen091440a2015-03-18 14:16:05 -07003533 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003534 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003535 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003536 // Non-app shortcuts are only supported for current user.
3537 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003539
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003540 // TODO: If there's an explicit component and we can't install that, delete it.
3541
Winson Chung82b016c2015-05-08 17:00:10 -07003542 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003543
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003544 Bitmap icon = iconInfo.loadIcon(c, info, context);
3545 // the fallback icon
3546 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003547 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003548 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003549 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003550 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 return info;
3552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003553
Sunny Goyal2350bc92014-10-14 16:42:54 -07003554 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003555 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3556 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3557 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3558
Adam Cohend9198822011-11-22 16:42:47 -08003559 if (intent == null) {
3560 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3561 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3562 return null;
3563 }
3564
Joe Onorato0589f0f2010-02-08 13:44:00 -08003565 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003566 boolean customIcon = false;
3567 ShortcutIconResource iconResource = null;
3568
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003569 if (bitmap instanceof Bitmap) {
3570 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003571 customIcon = true;
3572 } else {
3573 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003574 if (extra instanceof ShortcutIconResource) {
3575 iconResource = (ShortcutIconResource) extra;
3576 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003577 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003578 }
3579 }
3580
Michael Jurkac9d95c52011-08-29 14:03:34 -07003581 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003582
Kenny Guyed131872014-04-30 03:02:21 +01003583 // Only support intents for current user for now. Intents sent from other
3584 // users wouldn't get here without intent forwarding anyway.
3585 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003586 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003587 icon = mIconCache.getDefaultIcon(info.user);
3588 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003589 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003590 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003591
Winson Chung82b016c2015-05-08 17:00:10 -07003592 info.title = Utilities.trim(name);
3593 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003594 info.intent = intent;
3595 info.customIcon = customIcon;
3596 info.iconResource = iconResource;
3597
3598 return info;
3599 }
3600
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003602 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 * or make a new one.
3604 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003605 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 // See if a placeholder was created for us already
3607 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003608 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003610 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 folders.put(id, folderInfo);
3612 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003613 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003614 }
3615
Joe Onoratobe386092009-11-17 17:32:16 -08003616
Sunny Goyal651077b2014-06-30 14:15:31 -07003617 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3618 return (provider != null) && (provider.provider != null)
3619 && (provider.provider.getPackageName() != null);
3620 }
3621
Joe Onoratobe386092009-11-17 17:32:16 -08003622 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003623 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003624 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3625 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3626 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3627 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003628 if (mLoaderTask != null) {
3629 mLoaderTask.dumpState();
3630 } else {
3631 Log.d(TAG, "mLoaderTask=null");
3632 }
Joe Onoratobe386092009-11-17 17:32:16 -08003633 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003634
3635 public Callbacks getCallback() {
3636 return mCallbacks != null ? mCallbacks.get() : null;
3637 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003638
3639 /**
3640 * @return {@link FolderInfo} if its already loaded.
3641 */
3642 public FolderInfo findFolderById(Long folderId) {
3643 synchronized (sBgLock) {
3644 return sBgFolders.get(folderId);
3645 }
3646 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003647
Sunny Goyal527c7d32015-08-28 15:19:36 -07003648 @Thunk class DeferredMainThreadExecutor implements Executor {
3649
3650 @Override
3651 public void execute(Runnable command) {
3652 runOnMainThread(command);
3653 }
3654 }
3655
Sunny Goyal756adbc2015-04-16 15:20:51 -07003656 /**
3657 * @return the looper for the worker thread which can be used to start background tasks.
3658 */
3659 public static Looper getWorkerLooper() {
3660 return sWorkerThread.getLooper();
3661 }
Sunny Goyala1365452015-10-01 15:46:24 -07003662
3663 @Thunk static final void addDumpLog(String log) {
3664 Launcher.addDumpLog(TAG, log);
3665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666}