blob: 92ef3eae76eb90e5a46e572b16f557c8e63bc543 [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 Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
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 Goyalc3642d42015-10-30 10:27:08 -0700434 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 ArrayList<Long> workspaceScreens,
436 ArrayList<Long> addedWorkspaceScreensFinal,
437 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700438 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439
Sunny Goyala9116722015-04-29 13:55:58 -0700440 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700441 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700442 synchronized (sBgLock) {
443 for (ItemInfo info : sBgItemsIdMap) {
444 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
445 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
446 if (items == null) {
447 items = new ArrayList<>();
448 screenItems.put(info.screenId, items);
449 }
450 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 }
454
455 // Find appropriate space for the item.
456 long screenId = 0;
457 int[] cordinates = new int[2];
458 boolean found = false;
459
460 int screenCount = workspaceScreens.size();
461 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700462 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 if (preferredScreenIndex < screenCount) {
464 screenId = workspaceScreens.get(preferredScreenIndex);
465 found = findNextAvailableIconSpaceInScreen(
466 screenItems.get(screenId), cordinates, spanX, spanY);
467 }
468
469 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700470 // Search on any of the screens starting from the first screen.
471 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 screenId = workspaceScreens.get(screen);
473 if (findNextAvailableIconSpaceInScreen(
474 screenItems.get(screenId), cordinates, spanX, spanY)) {
475 // We found a space for it
476 found = true;
477 break;
478 }
479 }
480 }
481
482 if (!found) {
483 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700484 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
485 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
486 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487
488 // Save the screen id for binding in the workspace
489 workspaceScreens.add(screenId);
490 addedWorkspaceScreensFinal.add(screenId);
491
492 // If we still can't find an empty space, then God help us all!!!
493 if (!findNextAvailableIconSpaceInScreen(
494 screenItems.get(screenId), cordinates, spanX, spanY)) {
495 throw new RuntimeException("Can't find space to add the item");
496 }
497 }
498 return Pair.create(screenId, cordinates);
499 }
500
501 /**
502 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700505 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800507 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700508 return;
Winson Chung997a9232013-07-24 15:33:46 -0700509 }
Winson Chung64359a52013-07-08 17:17:08 -0700510 // Process the newly added applications and add them to the database first
511 Runnable r = new Runnable() {
512 public void run() {
513 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
514 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
515
Winson Chung76828c82013-08-19 15:43:29 -0700516 // Get the list of workspace screens. We need to append to this list and
517 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
518 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800519 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700520 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700522 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700524 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700526 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 }
Winson Chung76828c82013-08-19 15:43:29 -0700528
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700530 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700531 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 long screenId = coords.first;
533 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700534
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 ItemInfo itemInfo;
536 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
537 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700539 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700540 } else {
541 throw new RuntimeException("Unexpected info type");
542 }
Winson Chung94d67682013-09-25 16:29:40 -0700543
Winson Chung64359a52013-07-08 17:17:08 -0700544 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700546 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700547 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700548 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700549 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700550 }
551 }
552
Winson Chung76828c82013-08-19 15:43:29 -0700553 // Update the workspace screens
554 updateWorkspaceScreenOrder(context, workspaceScreens);
555
Adam Cohen76a47a12014-02-05 11:47:43 -0800556 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700557 runOnMainThread(new Runnable() {
558 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800559 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700560 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700561 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
562 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700563 if (!addedShortcutsFinal.isEmpty()) {
564 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
565 long lastScreenId = info.screenId;
566 for (ItemInfo i : addedShortcutsFinal) {
567 if (i.screenId == lastScreenId) {
568 addAnimated.add(i);
569 } else {
570 addNotAnimated.add(i);
571 }
Winson Chung997a9232013-07-24 15:33:46 -0700572 }
573 }
Winson Chungd64d1762013-08-20 14:37:16 -0700574 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800575 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700576 }
Winson Chung64359a52013-07-08 17:17:08 -0700577 }
Winson Chung997a9232013-07-24 15:33:46 -0700578 });
579 }
Winson Chung64359a52013-07-08 17:17:08 -0700580 }
581 };
582 runOnWorkerThread(r);
583 }
584
Sunny Goyald33860f2015-04-23 16:02:20 -0700585 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700586 if (sWorkerThread.getThreadId() == Process.myTid()) {
587 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
588 "main thread");
589 }
590
Sunny Goyald33860f2015-04-23 16:02:20 -0700591 // Remove any queued UI runnables
592 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700593 // Unbind all the workspace items
594 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700595 }
596
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700597 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700598 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700599 // Ensure that we don't use the same workspace items data structure on the main thread
600 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700601 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700602 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700603 tmpItems.addAll(sBgWorkspaceItems);
604 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700605 }
606 Runnable r = new Runnable() {
607 @Override
608 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700609 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610 item.unbind();
611 }
612 }
613 };
614 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700615 }
616
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 /**
618 * Adds an item to the DB if it was not created previously, or move it to a new
619 * <container, screen, cellX, cellY>
620 */
621 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700622 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 if (item.container == ItemInfo.NO_ID) {
624 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700625 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 } else {
627 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700628 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
630 }
631
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700632 static void checkItemInfoLocked(
633 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
634 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
635 if (modelItem != null && item != modelItem) {
636 // check all the data is consistent
637 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
638 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
639 ShortcutInfo shortcut = (ShortcutInfo) item;
640 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
641 modelShortcut.intent.filterEquals(shortcut.intent) &&
642 modelShortcut.id == shortcut.id &&
643 modelShortcut.itemType == shortcut.itemType &&
644 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 modelShortcut.cellX == shortcut.cellX &&
647 modelShortcut.cellY == shortcut.cellY &&
648 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700649 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700650 // For all intents and purposes, this is the same object
651 return;
652 }
653 }
654
655 // the modelItem needs to match up perfectly with item if our model is
656 // to be consistent with the database-- for now, just require
657 // modelItem == item or the equality check above
658 String msg = "item: " + ((item != null) ? item.toString() : "null") +
659 "modelItem: " +
660 ((modelItem != null) ? modelItem.toString() : "null") +
661 "Error: ItemInfo passed to checkItemInfo doesn't match original";
662 RuntimeException e = new RuntimeException(msg);
663 if (stackTrace != null) {
664 e.setStackTrace(stackTrace);
665 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800666 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700667 }
668 }
669
Michael Jurka816474f2012-06-25 14:49:02 -0700670 static void checkItemInfo(final ItemInfo item) {
671 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
672 final long itemId = item.id;
673 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 public void run() {
675 synchronized (sBgLock) {
676 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700677 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700678 }
679 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700680 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700681 }
682
Michael Jurkac9d95c52011-08-29 14:03:34 -0700683 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
684 final ItemInfo item, final String callingFunction) {
685 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700686 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700687 final ContentResolver cr = context.getContentResolver();
688
Adam Cohen487f7dd2012-06-28 18:12:10 -0700689 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 Runnable r = new Runnable() {
691 public void run() {
692 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700693 updateItemArrays(item, itemId, stackTrace);
694 }
695 };
696 runOnWorkerThread(r);
697 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
700 final ArrayList<ItemInfo> items, final String callingFunction) {
701 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700702
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
704 Runnable r = new Runnable() {
705 public void run() {
706 ArrayList<ContentProviderOperation> ops =
707 new ArrayList<ContentProviderOperation>();
708 int count = items.size();
709 for (int i = 0; i < count; i++) {
710 ItemInfo item = items.get(i);
711 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700712 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700714
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700715 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
716 updateItemArrays(item, itemId, stackTrace);
717
718 }
719 try {
720 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
721 } catch (Exception e) {
722 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723 }
724 }
725 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700726 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700728
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
730 // Lock on mBgLock *after* the db operation
731 synchronized (sBgLock) {
732 checkItemInfoLocked(itemId, item, stackTrace);
733
734 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
735 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
736 // Item is in a folder, make sure this folder exists
737 if (!sBgFolders.containsKey(item.container)) {
738 // An items container is being set to a that of an item which is not in
739 // the list of Folders.
740 String msg = "item: " + item + " container being set to: " +
741 item.container + ", not in the list of folders";
742 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 }
744 }
745
746 // Items are added/removed from the corresponding FolderInfo elsewhere, such
747 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
748 // that are on the desktop, as appropriate
749 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800750 if (modelItem != null &&
751 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
752 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 switch (modelItem.itemType) {
754 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
755 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
756 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
757 if (!sBgWorkspaceItems.contains(modelItem)) {
758 sBgWorkspaceItems.add(modelItem);
759 }
760 break;
761 default:
762 break;
763 }
764 } else {
765 sBgWorkspaceItems.remove(modelItem);
766 }
767 }
768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700772 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700773 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 item.cellX = cellX;
777 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700778
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 // We store hotseat items in canonical form which is this orientation invariant position
780 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787
788 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
791 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800792 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
Michael Jurkac9d95c52011-08-29 14:03:34 -0700795 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700796 }
797
798 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700799 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
800 * cellX, cellY have already been updated on the ItemInfos.
801 */
802 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
803 final long container, final int screen) {
804
805 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
806 int count = items.size();
807
808 for (int i = 0; i < count; i++) {
809 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 item.container = container;
811
812 // We store hotseat items in canonical form which is this orientation invariant position
813 // in the hotseat
814 if (context instanceof Launcher && screen < 0 &&
815 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 item.cellY);
818 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 }
821
822 final ContentValues values = new ContentValues();
823 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
824 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
825 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800826 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828
829 contentValues.add(values);
830 }
831 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
832 }
833
834 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700835 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800836 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700837 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700839 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800840 item.cellX = cellX;
841 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700842 item.spanX = spanX;
843 item.spanY = spanY;
844
845 // We store hotseat items in canonical form which is this orientation invariant position
846 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700848 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700850 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 }
Adam Cohend4844c32011-02-18 19:25:06 -0800853
Adam Cohend4844c32011-02-18 19:25:06 -0800854 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800855 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
857 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800858 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
860 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800862
Michael Jurka816474f2012-06-25 14:49:02 -0700863 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865
866 /**
867 * Update an item to the database in a specified container.
868 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700869 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700870 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100871 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800873 }
874
Sunny Goyal756a28a2015-04-23 17:07:55 -0700875 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700876 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700877 synchronized (mLock) {
878 if (!mHasLoaderCompletedOnce ||
879 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
880 throw new RuntimeException("Trying to add shortcut while loader is running");
881 }
882 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700883 }
884 }
885
Adam Cohend4844c32011-02-18 19:25:06 -0800886 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700887 * Returns true if the shortcuts already exists on the workspace. This must be called after
888 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700890 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
891 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700892 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 if (intent.getComponent() != null) {
894 // If component is not null, an intent with null package will produce
895 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700896 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700897 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = intent.toUri(0);
899 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
902 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 }
904 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700905 intentWithPkg = intent.toUri(0);
906 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700908
909 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700910 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911 if (item instanceof ShortcutInfo) {
912 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700913 Intent targetIntent = info.promisedIntent == null
914 ? info.intent : info.promisedIntent;
915 if (targetIntent != null && info.user.equals(user)) {
916 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700917 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
918 return true;
919 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 }
921 }
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700924 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700925 }
926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 * Add an item to the database in a specified container. Sets the container, screen, cellX and
929 * cellY fields of the item. Also assigns an ID to the item.
930 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700931 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700932 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 item.cellX = cellX;
935 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 // We store hotseat items in canonical form which is this orientation invariant position
937 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944
945 final ContentValues values = new ContentValues();
946 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100947 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948
Sunny Goyald2497482015-09-22 18:24:19 -0700949 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
950 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
951
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700953
Jason Monk8e19cf22014-03-20 15:06:57 -0400954 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700955 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700956 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700957 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700959 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700960 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400961 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700962 sBgItemsIdMap.put(item.id, item);
963 switch (item.itemType) {
964 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
965 sBgFolders.put(item.id, (FolderInfo) item);
966 // Fall through
967 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
968 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
969 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
970 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
971 sBgWorkspaceItems.add(item);
972 } else {
973 if (!sBgFolders.containsKey(item.container)) {
974 // Adding an item to a folder that doesn't exist.
975 String msg = "adding item: " + item + " to a folder that " +
976 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700977 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700978 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700979 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 break;
981 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
982 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
983 break;
984 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700985 }
986 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700987 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700988 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700989 }
990
Sunny Goyal34942622014-08-29 17:20:55 -0700991 private static ArrayList<ItemInfo> getItemsByPackageName(
992 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700993 ItemInfoFilter filter = new ItemInfoFilter() {
994 @Override
995 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
996 return cn.getPackageName().equals(pn) && info.user.equals(user);
997 }
998 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700999 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001000 }
1001
1002 /**
1003 * Removes all the items from the database corresponding to the specified package.
1004 */
1005 static void deletePackageFromDatabase(Context context, final String pn,
1006 final UserHandleCompat user) {
1007 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001008 }
1009
1010 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001013 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001014 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1015 items.add(item);
1016 deleteItemsFromDatabase(context, items);
1017 }
1018
1019 /**
1020 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001021 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001022 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001024 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001025 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001026 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001027 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001028 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001029
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001030 // Lock on mBgLock *after* the db operation
1031 synchronized (sBgLock) {
1032 switch (item.itemType) {
1033 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1034 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001035 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001036 if (info.container == item.id) {
1037 // We are deleting a folder which still contains items that
1038 // think they are contained by that folder.
1039 String msg = "deleting a folder (" + item + ") which still " +
1040 "contains items (" + info + ")";
1041 Log.e(TAG, msg);
1042 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001043 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001044 sBgWorkspaceItems.remove(item);
1045 break;
1046 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1047 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1048 sBgWorkspaceItems.remove(item);
1049 break;
1050 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1051 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1052 break;
1053 }
1054 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001056 }
1057 }
Michael Jurka83df1882011-08-31 20:59:26 -07001058 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 }
1061
1062 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 * Update the order of the workspace screens in the database. The array list contains
1064 * a list of screen ids in the order that they should appear.
1065 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001066 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001067 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 final ContentResolver cr = context.getContentResolver();
1069 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1070
1071 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001072 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 while (iter.hasNext()) {
1074 long id = iter.next();
1075 if (id < 0) {
1076 iter.remove();
1077 }
1078 }
1079
1080 Runnable r = new Runnable() {
1081 @Override
1082 public void run() {
Yura085c8532014-02-11 15:15:29 +00001083 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001085 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001086 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 for (int i = 0; i < count; i++) {
1088 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001089 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1091 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001092 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 }
Yura085c8532014-02-11 15:15:29 +00001094
1095 try {
1096 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1097 } catch (Exception ex) {
1098 throw new RuntimeException(ex);
1099 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001100
Winson Chungba9c37f2013-08-30 14:11:37 -07001101 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001102 sBgWorkspaceScreens.clear();
1103 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001104 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001105 }
1106 };
1107 runOnWorkerThread(r);
1108 }
1109
1110 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001111 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 */
Winsonc0b52fe2015-09-09 16:38:15 -07001113 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 final ContentResolver cr = context.getContentResolver();
1115
Michael Jurkac9d95c52011-08-29 14:03:34 -07001116 Runnable r = new Runnable() {
1117 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001118 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001120 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 sBgItemsIdMap.remove(info.id);
1122 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 sBgWorkspaceItems.remove(info);
1124 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001125
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001126 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001127 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001129 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 for (ItemInfo childInfo : info.contents) {
1131 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001133 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001134 }
1135 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001136 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 }
1138
1139 /**
1140 * Set this as the current Launcher activity object for the loader.
1141 */
1142 public void initialize(Callbacks callbacks) {
1143 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001144 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1145 // workspace to prevent leaking Launcher activities on orientation change.
1146 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 mCallbacks = new WeakReference<Callbacks>(callbacks);
1148 }
1149 }
1150
Kenny Guyed131872014-04-30 03:02:21 +01001151 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001152 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001153 int op = PackageUpdatedTask.OP_UPDATE;
1154 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1155 user));
1156 }
1157
1158 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001159 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001160 int op = PackageUpdatedTask.OP_REMOVE;
1161 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1162 user));
1163 }
1164
1165 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001166 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001167 int op = PackageUpdatedTask.OP_ADD;
1168 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1169 user));
1170 }
1171
1172 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001173 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001174 boolean replacing) {
1175 if (!replacing) {
1176 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1177 user));
1178 if (mAppsCanBeOnRemoveableStorage) {
1179 // Only rebind if we support removable storage. It catches the
1180 // case where
1181 // apps on the external sd card need to be reloaded
1182 startLoaderFromBackground();
1183 }
1184 } else {
1185 // If we are replacing then just update the packages in the list
1186 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1187 packageNames, user));
1188 }
1189 }
1190
1191 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001192 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001193 boolean replacing) {
1194 if (!replacing) {
1195 enqueuePackageUpdated(new PackageUpdatedTask(
1196 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1197 user));
1198 }
Kenny Guyed131872014-04-30 03:02:21 +01001199 }
1200
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001201 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1203 * ACTION_PACKAGE_CHANGED.
1204 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001205 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001206 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001207 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001208
Joe Onorato36115782010-06-17 13:28:48 -04001209 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001210 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001211 // If we have changed locale we need to clear out the labels in all apps/workspace.
1212 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001213 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001214 Callbacks callbacks = getCallback();
1215 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001216 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001217 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001218 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1219 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001220 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001221 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001222 }
1223 }
1224
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001225 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001226 resetLoadedState(true, true);
1227
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // Do this here because if the launcher activity is running it will be restarted.
1229 // If it's not running startLoaderFromBackground will merely tell it that it needs
1230 // to reload.
1231 startLoaderFromBackground();
1232 }
1233
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1235 synchronized (mLock) {
1236 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1237 // mWorkspaceLoaded to true later
1238 stopLoaderLocked();
1239 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1240 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1241 }
1242 }
1243
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001244 /**
1245 * When the launcher is in the background, it's possible for it to miss paired
1246 * configuration changes. So whenever we trigger the loader from the background
1247 * tell the launcher that it needs to re-run the loader when it comes back instead
1248 * of doing it now.
1249 */
1250 public void startLoaderFromBackground() {
1251 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001252 Callbacks callbacks = getCallback();
1253 if (callbacks != null) {
1254 // Only actually run the loader if they're not paused.
1255 if (!callbacks.setLoadOnResume()) {
1256 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001257 }
1258 }
1259 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001260 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001261 }
Joe Onorato36115782010-06-17 13:28:48 -04001262 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001263
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001264 /**
1265 * If there is already a loader task running, tell it to stop.
1266 */
1267 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001268 LoaderTask oldTask = mLoaderTask;
1269 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001270 oldTask.stopLocked();
1271 }
Reena Lee93f824a2011-09-23 17:20:28 -07001272 }
1273
Adam Cohen1a85c582014-09-30 09:48:49 -07001274 public boolean isCurrentCallbacks(Callbacks callbacks) {
1275 return (mCallbacks != null && mCallbacks.get() == callbacks);
1276 }
1277
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001278 public void startLoader(int synchronousBindPage) {
1279 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001280 }
1281
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001282 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001283 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1284 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001285 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001286 // Don't bother to start the thread if we know it's not going to do anything
1287 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001288 final Callbacks oldCallbacks = mCallbacks.get();
1289 // Clear any pending bind-runnables from the synchronized load process.
1290 runOnMainThread(new Runnable() {
1291 public void run() {
1292 oldCallbacks.clearPendingBinds();
1293 }
1294 });
1295
Joe Onorato36115782010-06-17 13:28:48 -04001296 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001297 stopLoaderLocked();
1298 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001299 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001300 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1302 } else {
1303 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1304 sWorker.post(mLoaderTask);
1305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001307 }
1308 }
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 public void stopLoader() {
1311 synchronized (mLock) {
1312 if (mLoaderTask != null) {
1313 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
1315 }
Joe Onorato36115782010-06-17 13:28:48 -04001316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001318 /**
1319 * Loads the workspace screen ids in an ordered list.
1320 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001321 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001322 final ContentResolver contentResolver = context.getContentResolver();
1323 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001324
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001325 // Get screens ordered by rank.
1326 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1327 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1328 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001329 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001330 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001331 while (sc.moveToNext()) {
1332 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001333 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001334 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001335 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001336 }
1337 }
1338 } finally {
1339 sc.close();
1340 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001341 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001342 }
1343
Joe Onorato36115782010-06-17 13:28:48 -04001344 /**
1345 * Runnable for the thread that loads the contents of the launcher:
1346 * - workspace icons
1347 * - widgets
1348 * - all apps icons
1349 */
1350 private class LoaderTask implements Runnable {
1351 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001352 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001353 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001354 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001355 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001357 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001358 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001359 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 }
1361
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001363 mIsLoadingAndBindingWorkspace = true;
1364
Joe Onorato36115782010-06-17 13:28:48 -04001365 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001366 if (DEBUG_LOADERS) {
1367 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001369
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001370 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001371 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001372 synchronized (LoaderTask.this) {
1373 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001374 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001375 }
1376 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378 }
1379
Joe Onorato36115782010-06-17 13:28:48 -04001380 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001381 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001382 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001383
Joe Onorato36115782010-06-17 13:28:48 -04001384 private void waitForIdle() {
1385 // Wait until the either we're stopped or the other threads are done.
1386 // This way we don't start loading all apps until the workspace has settled
1387 // down.
1388 synchronized (LoaderTask.this) {
1389 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001390
Joe Onorato36115782010-06-17 13:28:48 -04001391 mHandler.postIdle(new Runnable() {
1392 public void run() {
1393 synchronized (LoaderTask.this) {
1394 mLoadAndBindStepFinished = true;
1395 if (DEBUG_LOADERS) {
1396 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001397 }
Joe Onorato36115782010-06-17 13:28:48 -04001398 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001399 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001400 }
Joe Onorato36115782010-06-17 13:28:48 -04001401 });
1402
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001403 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001404 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001405 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1406 // wait no longer than 1sec at a time
1407 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001408 } catch (InterruptedException ex) {
1409 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
1411 }
Joe Onorato36115782010-06-17 13:28:48 -04001412 if (DEBUG_LOADERS) {
1413 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001414 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001415 + "ms for previous step to finish binding");
1416 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001417 }
Joe Onorato36115782010-06-17 13:28:48 -04001418 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001419
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001421 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 // Ensure that we have a valid page index to load synchronously
1423 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1424 "valid page index");
1425 }
1426 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1427 // Ensure that we don't try and bind a specified page when the pages have not been
1428 // loaded already (we should load everything asynchronously in that case)
1429 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1430 }
1431 synchronized (mLock) {
1432 if (mIsLoaderTaskRunning) {
1433 // Ensure that we are never running the background loading at this point since
1434 // we also touch the background collections
1435 throw new RuntimeException("Error! Background loading is already running");
1436 }
1437 }
1438
1439 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1440 // data structures, we can't allow any other thread to touch that data, but because
1441 // this call is synchronous, we can get away with not locking).
1442
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001443 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001444 // operations from the previous activity. We need to ensure that all queued operations
1445 // are executed before any synchronous binding work is done.
1446 mHandler.flush();
1447
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 // Divide the set of loaded items into those that we are binding synchronously, and
1449 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001450 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1452 // arise from that.
1453 onlyBindAllApps();
1454 }
1455
Joe Onorato36115782010-06-17 13:28:48 -04001456 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001457 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001458 if (mStopped) {
1459 return;
1460 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 mIsLoaderTaskRunning = true;
1462 }
Joe Onorato36115782010-06-17 13:28:48 -04001463 // Optimize for end-user experience: if the Launcher is up and // running with the
1464 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1465 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001466 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001467 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001468 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001469
Joe Onorato36115782010-06-17 13:28:48 -04001470 if (mStopped) {
1471 break keep_running;
1472 }
1473
Joe Onorato36115782010-06-17 13:28:48 -04001474 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001475
1476 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001477 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1478 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480
Joe Onorato36115782010-06-17 13:28:48 -04001481 // Clear out this reference, otherwise we end up holding it until all of the
1482 // callback runnables are done.
1483 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 synchronized (mLock) {
1486 // If we are still the last one to be scheduled, remove ourselves.
1487 if (mLoaderTask == this) {
1488 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001490 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001491 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
Joe Onorato36115782010-06-17 13:28:48 -04001493 }
1494
1495 public void stopLocked() {
1496 synchronized (LoaderTask.this) {
1497 mStopped = true;
1498 this.notify();
1499 }
1500 }
1501
1502 /**
1503 * Gets the callbacks object. If we've been stopped, or if the launcher object
1504 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1505 * object that was around when the deferred message was scheduled, and if there's
1506 * a new Callbacks object around then also return null. This will save us from
1507 * calling onto it with data that will be ignored.
1508 */
1509 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1510 synchronized (mLock) {
1511 if (mStopped) {
1512 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514
1515 if (mCallbacks == null) {
1516 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001517 }
Joe Onorato36115782010-06-17 13:28:48 -04001518
1519 final Callbacks callbacks = mCallbacks.get();
1520 if (callbacks != oldCallbacks) {
1521 return null;
1522 }
1523 if (callbacks == null) {
1524 Log.w(TAG, "no mCallbacks");
1525 return null;
1526 }
1527
1528 return callbacks;
1529 }
1530 }
1531
1532 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001533 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1534 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001535 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001536 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001537 final int countX = profile.numColumns;
1538 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001539
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001541 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001542 // Return early if we detect that an item is under the hotseat button
1543 if (mCallbacks == null ||
1544 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001545 Log.e(TAG, "Error loading shortcut into hotseat " + item
1546 + " into position (" + item.screenId + ":" + item.cellX + ","
1547 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001548 return false;
1549 }
1550
Dan Sandler295ae182013-12-10 16:05:47 -05001551 final ItemInfo[][] hotseatItems =
1552 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1553
Adam Cohen2e6da152015-05-06 11:42:25 -07001554 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001555 Log.e(TAG, "Error loading shortcut " + item
1556 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001557 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001558 + ")");
1559 return false;
1560 }
1561
Dan Sandler295ae182013-12-10 16:05:47 -05001562 if (hotseatItems != null) {
1563 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 Log.e(TAG, "Error loading shortcut into hotseat " + item
1565 + " into position (" + item.screenId + ":" + item.cellX + ","
1566 + item.cellY + ") occupied by "
1567 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1568 [(int) item.screenId][0]);
1569 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001570 } else {
1571 hotseatItems[(int) item.screenId][0] = item;
1572 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001573 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001574 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001575 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 items[(int) item.screenId][0] = item;
1577 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001578 return true;
1579 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001580 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1581 if (!workspaceScreens.contains((Long) item.screenId)) {
1582 // The item has an invalid screen id.
1583 return false;
1584 }
1585 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001586 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001587 return true;
1588 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001589
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001591 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 occupied.put(item.screenId, items);
1593 }
1594
Dan Sandler295ae182013-12-10 16:05:47 -05001595 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001596 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1597 item.cellX < 0 || item.cellY < 0 ||
1598 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1599 Log.e(TAG, "Error loading shortcut " + item
1600 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1601 + item.cellX + "," + item.cellY
1602 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1603 return false;
1604 }
1605
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001606 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001607 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1608 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001610 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001611 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001612 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001613 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001614 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001615 return false;
1616 }
1617 }
1618 }
1619 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1620 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
1623 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001624
Joe Onorato36115782010-06-17 13:28:48 -04001625 return true;
1626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627
Winson Chungba9c37f2013-08-30 14:11:37 -07001628 /** Clears all the sBg data structures */
1629 private void clearSBgDataStructures() {
1630 synchronized (sBgLock) {
1631 sBgWorkspaceItems.clear();
1632 sBgAppWidgets.clear();
1633 sBgFolders.clear();
1634 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001635 sBgWorkspaceScreens.clear();
1636 }
1637 }
1638
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001639 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001640 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641
Joe Onorato36115782010-06-17 13:28:48 -04001642 final Context context = mContext;
1643 final ContentResolver contentResolver = context.getContentResolver();
1644 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001645 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001646 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001647 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001648
Winson Chung892c74d2013-08-22 16:15:50 -07001649 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001650 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001651 int countX = profile.numColumns;
1652 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001653
Sunny Goyalf076eae2016-01-11 12:25:10 -08001654 if (GridSizeMigrationTask.ENABLED &&
1655 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1656 // Migration failed. Clear workspace.
1657 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001658 }
1659
Dan Sandlerd5024042014-01-09 15:01:33 -05001660 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001661 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001662 LauncherSettings.Settings.call(contentResolver,
1663 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001664 }
1665
1666 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1667 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001668 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001669 LauncherSettings.Settings.call(contentResolver,
1670 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 } else {
1672 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001673 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001674 LauncherSettings.Settings.call(contentResolver,
1675 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 }
Adam Cohene25af792013-06-06 23:08:25 -07001677
Winson Chung2abf94d2012-07-18 18:16:38 -07001678 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001679 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001680 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001681 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001682 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001683
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001685 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001686 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001687 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001688 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001689
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 // +1 for the hotseat (it can be larger than the workspace)
1691 // Load workspace in reverse order to ensure that latest items are loaded first (and
1692 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001693 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 try {
1696 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1697 final int intentIndex = c.getColumnIndexOrThrow
1698 (LauncherSettings.Favorites.INTENT);
1699 final int titleIndex = c.getColumnIndexOrThrow
1700 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 final int containerIndex = c.getColumnIndexOrThrow(
1702 LauncherSettings.Favorites.CONTAINER);
1703 final int itemTypeIndex = c.getColumnIndexOrThrow(
1704 LauncherSettings.Favorites.ITEM_TYPE);
1705 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1706 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001707 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1708 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 final int screenIndex = c.getColumnIndexOrThrow(
1710 LauncherSettings.Favorites.SCREEN);
1711 final int cellXIndex = c.getColumnIndexOrThrow
1712 (LauncherSettings.Favorites.CELLX);
1713 final int cellYIndex = c.getColumnIndexOrThrow
1714 (LauncherSettings.Favorites.CELLY);
1715 final int spanXIndex = c.getColumnIndexOrThrow
1716 (LauncherSettings.Favorites.SPANX);
1717 final int spanYIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001719 final int rankIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001721 final int restoredIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001723 final int profileIdIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001725 final int optionsIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001727 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728
Sunny Goyal7f834d22015-04-21 10:10:23 -07001729 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1730 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1731 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1732 }
1733
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734 ShortcutInfo info;
1735 String intentDescription;
1736 LauncherAppWidgetInfo appWidgetInfo;
1737 int container;
1738 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001739 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001741 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 while (!mStopped && c.moveToNext()) {
1744 try {
1745 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001746 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001747 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001748 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 switch (itemType) {
1751 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1752 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001753 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001755 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001756 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001757 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001758 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001759 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001760 if (user == null) {
1761 // User has been deleted remove the item.
1762 itemsToRemove.add(id);
1763 continue;
1764 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 try {
1766 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001767 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001768 if (cn != null && cn.getPackageName() != null) {
1769 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1770 cn.getPackageName(), user);
1771 boolean validComponent = validPkg &&
1772 launcherApps.isActivityEnabledForProfile(cn, user);
1773
1774 if (validComponent) {
1775 if (restored) {
1776 // no special handling necessary for this item
1777 restoredRows.add(id);
1778 restored = false;
1779 }
1780 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001781 intent = null;
1782 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1783 // We allow auto install apps to have their intent
1784 // updated after an install.
1785 intent = manager.getLaunchIntentForPackage(
1786 cn.getPackageName());
1787 if (intent != null) {
1788 ContentValues values = new ContentValues();
1789 values.put(LauncherSettings.Favorites.INTENT,
1790 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001791 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001792 }
1793 }
1794
1795 if (intent == null) {
1796 // The app is installed but the component is no
1797 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001798 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001799 itemsToRemove.add(id);
1800 continue;
1801 } else {
1802 // no special handling necessary for this item
1803 restoredRows.add(id);
1804 restored = false;
1805 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001806 } else if (restored) {
1807 // Package is not yet available but might be
1808 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001809 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001810
1811 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1812 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001813 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001814 // App restore has started. Update the flag
1815 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1816 ContentValues values = new ContentValues();
1817 values.put(LauncherSettings.Favorites.RESTORED,
1818 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001819 updateItem(id, values);
1820 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1821 // This is a common app. Try to replace this.
1822 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1823 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1824 if (parser.findDefaultApp()) {
1825 // Default app found. Replace it.
1826 intent = parser.parsedIntent;
1827 cn = intent.getComponent();
1828 ContentValues values = parser.parsedValues;
1829 values.put(LauncherSettings.Favorites.RESTORED, 0);
1830 updateItem(id, values);
1831 restored = false;
1832 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001833
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001834 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001835 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001836 itemsToRemove.add(id);
1837 continue;
1838 }
Sunny Goyal94485362014-09-18 16:13:58 -07001839 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001840 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001841 itemsToRemove.add(id);
1842 continue;
1843 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001844 } else if (launcherApps.isAppEnabled(
1845 manager, cn.getPackageName(),
1846 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1847 // Package is present but not available.
1848 allowMissingTarget = true;
1849 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1850 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001851 // SdCard is not ready yet. Package might get available,
1852 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001853 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001854 HashSet<String> pkgs = sPendingPackages.get(user);
1855 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001856 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001857 sPendingPackages.put(user, pkgs);
1858 }
1859 pkgs.add(cn.getPackageName());
1860 allowMissingTarget = true;
1861 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001862
1863 } else {
1864 // Do not wait for external media load anymore.
1865 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001866 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001867 itemsToRemove.add(id);
1868 continue;
Winson Chungee055712013-07-30 14:46:24 -07001869 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001870 } else if (cn == null) {
1871 // For shortcuts with no component, keep them as they are
1872 restoredRows.add(id);
1873 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001874 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001876 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001877 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 continue;
1879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001880
Sunny Goyal34b65272015-03-11 16:56:52 -07001881 boolean useLowResIcon = container >= 0 &&
1882 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1883
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001884 if (itemReplaced) {
1885 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001886 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001887 cursorIconInfo.iconIndex, titleIndex,
1888 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001889 } else {
1890 // Don't replace items for other profiles.
1891 itemsToRemove.add(id);
1892 continue;
1893 }
1894 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001895 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001896 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001897 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001898 intent = getRestoredItemIntent(c, context, intent);
1899 } else {
1900 // Don't restore items for other profiles.
1901 itemsToRemove.add(id);
1902 continue;
1903 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001904 } else if (itemType ==
1905 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001906 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001907 cursorIconInfo.iconIndex, titleIndex,
1908 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001910 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001911
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001912 // App shortcuts that used to be automatically added to Launcher
1913 // didn't always have the correct intent flags set, so do that
1914 // here
1915 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001916 intent.getCategories() != null &&
1917 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001918 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 intent.addFlags(
1920 Intent.FLAG_ACTIVITY_NEW_TASK |
1921 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1922 }
Michael Jurka96879562012-03-22 05:54:33 -07001923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001926 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001930 info.cellX = c.getInt(cellXIndex);
1931 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001932 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001933 info.spanX = 1;
1934 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001935 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001936 if (info.promisedIntent != null) {
1937 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1938 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001939 info.isDisabled = disabledState;
1940 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1941 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1942 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001943
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001945 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001946 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 break;
1948 }
1949
Sunny Goyal756adbc2015-04-16 15:20:51 -07001950 if (restored) {
1951 ComponentName cn = info.getTargetComponent();
1952 if (cn != null) {
1953 Integer progress = installingPkgs.get(cn.getPackageName());
1954 if (progress != null) {
1955 info.setInstallProgress(progress);
1956 } else {
1957 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1958 }
1959 }
1960 }
1961
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 switch (container) {
1963 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1964 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1965 sBgWorkspaceItems.add(info);
1966 break;
1967 default:
1968 // Item is in a user folder
1969 FolderInfo folderInfo =
1970 findOrMakeFolder(sBgFolders, container);
1971 folderInfo.add(info);
1972 break;
1973 }
1974 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001975 } else {
1976 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 }
1978 break;
1979
1980 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1981 id = c.getLong(idIndex);
1982 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1983
Sunny Goyala508e4f2015-05-21 09:33:57 -07001984 // Do not trim the folder label, as is was set by the user.
1985 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 folderInfo.cellX = c.getInt(cellXIndex);
1990 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001991 folderInfo.spanX = 1;
1992 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001993 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994
Daniel Sandler8802e962010-05-26 16:28:16 -04001995 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001996 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001997 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001998 break;
1999 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002000
Joe Onorato9c1289c2009-08-17 11:03:03 -04002001 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2003 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2004 sBgWorkspaceItems.add(folderInfo);
2005 break;
Joe Onorato36115782010-06-17 13:28:48 -04002006 }
Joe Onorato17a89222011-02-08 17:26:11 -08002007
Chris Wrenf4d08112014-01-16 18:13:56 -05002008 if (restored) {
2009 // no special handling required for restored folders
2010 restoredRows.add(id);
2011 }
2012
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2014 sBgFolders.put(folderInfo.id, folderInfo);
2015 break;
2016
2017 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002018 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002020 boolean customWidget = itemType ==
2021 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002024 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002025 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002026 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002027 user = allUsers.get(serialNumber);
2028 if (user == null) {
2029 itemsToRemove.add(id);
2030 continue;
2031 }
2032
Sunny Goyalff572272014-07-23 13:58:07 -07002033 final ComponentName component =
2034 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002035
Sunny Goyal651077b2014-06-30 14:15:31 -07002036 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002037 final boolean isIdValid = (restoreStatus &
2038 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002039 final boolean wasProviderReady = (restoreStatus &
2040 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002041
Adam Cohen59400422014-03-05 18:07:04 -08002042 final LauncherAppWidgetProviderInfo provider =
2043 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002044 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002045 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002046
2047 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002048 if (!isSafeMode && !customWidget &&
2049 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002050 addDumpLog("Deleting widget that isn't installed anymore: "
2051 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 itemsToRemove.add(id);
2053 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002054 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002055 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2056 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002057
Sunny Goyal53f96722015-07-13 19:54:53 -07002058 // The provider is available. So the widget is either
2059 // available or not available. We do not need to track
2060 // any future restore updates.
2061 int status = restoreStatus &
2062 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002063 if (!wasProviderReady) {
2064 // If provider was not previously ready, update the
2065 // status and UI flag.
2066
2067 // Id would be valid only if the widget restore broadcast was received.
2068 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002069 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002070 } else {
2071 status &= ~LauncherAppWidgetInfo
2072 .FLAG_PROVIDER_NOT_READY;
2073 }
2074 }
2075 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002076 } else {
2077 Log.v(TAG, "Widget restore pending id=" + id
2078 + " appWidgetId=" + appWidgetId
2079 + " status =" + restoreStatus);
2080 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002081 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002082 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002083 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002084
2085 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2086 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002087 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002088 // App restore has started. Update the flag
2089 appWidgetInfo.restoreStatus |=
2090 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002091 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002092 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002093 itemsToRemove.add(id);
2094 continue;
2095 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002096
2097 appWidgetInfo.installProgress =
2098 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002099 }
Sunny Goyalff572272014-07-23 13:58:07 -07002100
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002102 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 appWidgetInfo.cellX = c.getInt(cellXIndex);
2104 appWidgetInfo.cellY = c.getInt(cellYIndex);
2105 appWidgetInfo.spanX = c.getInt(spanXIndex);
2106 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002107 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002108
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2110 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2111 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002112 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2113 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 continue;
2115 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002117 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002119 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002120 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 break;
2122 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002123
Adam Cohen59400422014-03-05 18:07:04 -08002124 if (!customWidget) {
2125 String providerName =
2126 appWidgetInfo.providerName.flattenToString();
2127 if (!providerName.equals(savedProvider) ||
2128 (appWidgetInfo.restoreStatus != restoreStatus)) {
2129 ContentValues values = new ContentValues();
2130 values.put(
2131 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2132 providerName);
2133 values.put(LauncherSettings.Favorites.RESTORED,
2134 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002135 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002136 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002137 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2139 sBgAppWidgets.add(appWidgetInfo);
2140 }
Joe Onorato36115782010-06-17 13:28:48 -04002141 break;
2142 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002144 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002145 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002146 }
2147 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002148 if (c != null) {
2149 c.close();
2150 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 }
2152
Winson Chungba9c37f2013-08-30 14:11:37 -07002153 // Break early if we've stopped loading
2154 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002155 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002156 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002157 }
2158
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002161 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2162 Utilities.createDbSelectionQuery(
2163 LauncherSettings.Favorites._ID, itemsToRemove), null);
2164 if (DEBUG_LOADERS) {
2165 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2166 LauncherSettings.Favorites._ID, itemsToRemove));
2167 }
2168
2169 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002170 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2171 .call(contentResolver,
2172 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2173 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2174 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002175 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2176 sBgFolders.remove(folderId);
2177 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002178 }
2179 }
2180
Sunny Goyal317698b2015-07-29 11:45:41 -07002181 // Sort all the folder items and make sure the first 3 items are high resolution.
2182 for (FolderInfo folder : sBgFolders) {
2183 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2184 int pos = 0;
2185 for (ShortcutInfo info : folder.contents) {
2186 if (info.usingLowResIcon) {
2187 info.updateIcon(mIconCache, false);
2188 }
2189 pos ++;
2190 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2191 break;
2192 }
2193 }
2194 }
2195
Chris Wrenf4d08112014-01-16 18:13:56 -05002196 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002197 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002198 ContentValues values = new ContentValues();
2199 values.put(LauncherSettings.Favorites.RESTORED, 0);
2200 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2201 Utilities.createDbSelectionQuery(
2202 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002203 }
2204
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002205 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2206 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002207 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002208 null, sWorker);
2209 }
2210
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002211 // Remove any empty screens
2212 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002213 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002214 long screenId = item.screenId;
2215 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2216 unusedScreens.contains(screenId)) {
2217 unusedScreens.remove(screenId);
2218 }
2219 }
2220
2221 // If there are any empty screens remove them, and update.
2222 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002223 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 }
2226
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 if (DEBUG_LOADERS) {
2228 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2229 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002231 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002233
Sunny Goyale2df0622015-04-24 11:27:00 -07002234 for (int i = 0; i < nScreens; i++) {
2235 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002236 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 line += " | ";
2238 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002239 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002240 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002241 if (x < screen.length && y < screen[x].length) {
2242 line += (screen[x][y] != null) ? "#" : ".";
2243 } else {
2244 line += "!";
2245 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 }
Joe Onorato36115782010-06-17 13:28:48 -04002247 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002249 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 }
Joe Onorato36115782010-06-17 13:28:48 -04002251 }
Adam Cohene25af792013-06-06 23:08:25 -07002252 }
2253
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002254 /**
2255 * Partially updates the item without any notification. Must be called on the worker thread.
2256 */
2257 private void updateItem(long itemId, ContentValues update) {
2258 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002259 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002260 update,
2261 BaseColumns._ID + "= ?",
2262 new String[]{Long.toString(itemId)});
2263 }
2264
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 /** Filters the set of items who are directly or indirectly (via another container) on the
2266 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002267 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 ArrayList<ItemInfo> allWorkspaceItems,
2269 ArrayList<ItemInfo> currentScreenItems,
2270 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002271 // Purge any null ItemInfos
2272 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2273 while (iter.hasNext()) {
2274 ItemInfo i = iter.next();
2275 if (i == null) {
2276 iter.remove();
2277 }
2278 }
2279
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 // Order the set of items by their containers first, this allows use to walk through the
2281 // list sequentially, build up a list of containers that are in the specified screen,
2282 // as well as all items in those containers.
2283 Set<Long> itemsOnScreen = new HashSet<Long>();
2284 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2285 @Override
2286 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002287 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 }
2289 });
2290 for (ItemInfo info : allWorkspaceItems) {
2291 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002292 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293 currentScreenItems.add(info);
2294 itemsOnScreen.add(info.id);
2295 } else {
2296 otherScreenItems.add(info);
2297 }
2298 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2299 currentScreenItems.add(info);
2300 itemsOnScreen.add(info.id);
2301 } else {
2302 if (itemsOnScreen.contains(info.container)) {
2303 currentScreenItems.add(info);
2304 itemsOnScreen.add(info.id);
2305 } else {
2306 otherScreenItems.add(info);
2307 }
2308 }
2309 }
2310 }
2311
2312 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002313 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314 ArrayList<LauncherAppWidgetInfo> appWidgets,
2315 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2316 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317
2318 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002319 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002321 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 currentScreenWidgets.add(widget);
2323 } else {
2324 otherScreenWidgets.add(widget);
2325 }
2326 }
2327 }
2328
2329 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002330 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002331 LongArrayMap<ItemInfo> itemsIdMap,
2332 LongArrayMap<FolderInfo> folders,
2333 LongArrayMap<FolderInfo> currentScreenFolders,
2334 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335
Sunny Goyale2df0622015-04-24 11:27:00 -07002336 int total = folders.size();
2337 for (int i = 0; i < total; i++) {
2338 long id = folders.keyAt(i);
2339 FolderInfo folder = folders.valueAt(i);
2340
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002342 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002344 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 currentScreenFolders.put(id, folder);
2346 } else {
2347 otherScreenFolders.put(id, folder);
2348 }
2349 }
2350 }
2351
2352 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2353 * right) */
2354 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002355 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002356 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002357 final int screenCols = profile.numColumns;
2358 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002360 @Override
2361 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002362 if (lhs.container == rhs.container) {
2363 // Within containers, order by their spatial position in that container
2364 switch ((int) lhs.container) {
2365 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2366 long lr = (lhs.screenId * screenCellCount +
2367 lhs.cellY * screenCols + lhs.cellX);
2368 long rr = (rhs.screenId * screenCellCount +
2369 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002370 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002371 }
2372 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2373 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002374 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002375 }
2376 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002377 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002378 throw new RuntimeException("Unexpected container type when " +
2379 "sorting workspace items.");
2380 }
2381 return 0;
2382 }
2383 } else {
2384 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002385 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002386 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002387 }
2388 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002389 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002390
Adam Cohendcd297f2013-06-18 13:13:40 -07002391 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2392 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 final Runnable r = new Runnable() {
2394 @Override
2395 public void run() {
2396 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2397 if (callbacks != null) {
2398 callbacks.bindScreens(orderedScreens);
2399 }
2400 }
2401 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002402 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002403 }
2404
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2406 final ArrayList<ItemInfo> workspaceItems,
2407 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002408 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002409 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410
2411 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002412 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002414 final int start = i;
2415 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 final Runnable r = new Runnable() {
2417 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002419 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002421 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2422 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002423 }
2424 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002426 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002427 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428
2429 // Bind the folders
2430 if (!folders.isEmpty()) {
2431 final Runnable r = new Runnable() {
2432 public void run() {
2433 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2434 if (callbacks != null) {
2435 callbacks.bindFolders(folders);
2436 }
2437 }
2438 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002439 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440 }
2441
2442 // Bind the widgets, one at a time
2443 N = appWidgets.size();
2444 for (int i = 0; i < N; i++) {
2445 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2446 final Runnable r = new Runnable() {
2447 public void run() {
2448 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2449 if (callbacks != null) {
2450 callbacks.bindAppWidget(widget);
2451 }
2452 }
2453 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002454 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 }
2456 }
2457
2458 /**
2459 * Binds all loaded data to actual views on the main thread.
2460 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002461 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 final long t = SystemClock.uptimeMillis();
2463 Runnable r;
2464
2465 // Don't use these two variables in any of the callback runnables.
2466 // Otherwise we hold a reference to them.
2467 final Callbacks oldCallbacks = mCallbacks.get();
2468 if (oldCallbacks == null) {
2469 // This launcher has exited and nobody bothered to tell us. Just bail.
2470 Log.w(TAG, "LoaderTask running with no launcher");
2471 return;
2472 }
2473
Winson Chung9b9fb962013-11-15 15:39:34 -08002474 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2476 ArrayList<LauncherAppWidgetInfo> appWidgets =
2477 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002478 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002479
2480 final LongArrayMap<FolderInfo> folders;
2481 final LongArrayMap<ItemInfo> itemsIdMap;
2482
Winson Chung2abf94d2012-07-18 18:16:38 -07002483 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 workspaceItems.addAll(sBgWorkspaceItems);
2485 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002487
2488 folders = sBgFolders.clone();
2489 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490 }
2491
Derek Prothro7aff3992013-12-10 14:00:37 -05002492 final boolean isLoadingSynchronously =
2493 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002494 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002495 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002496 if (currScreen >= orderedScreenIds.size()) {
2497 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002498 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002499 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002500 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002501 final long currentScreenId = currentScreen < 0
2502 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002503
2504 // Load all the items that are on the current page first (and in the process, unbind
2505 // all the existing workspace items before we call startBinding() below.
2506 unbindWorkspaceItemsOnMainThread();
2507
2508 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2510 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2511 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2512 new ArrayList<LauncherAppWidgetInfo>();
2513 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2514 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002515 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2516 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517
Winson Chung9b9fb962013-11-15 15:39:34 -08002518 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002520 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002522 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 otherFolders);
2524 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2525 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2526
2527 // Tell the workspace that we're about to start binding items
2528 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002529 public void run() {
2530 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2531 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002532 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002534 }
2535 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002537 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538
Adam Cohendcd297f2013-06-18 13:13:40 -07002539 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2540
Sunny Goyal527c7d32015-08-28 15:19:36 -07002541 Executor mainExecutor = new DeferredMainThreadExecutor();
2542 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002544 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545
Sunny Goyal527c7d32015-08-28 15:19:36 -07002546 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2547 // remaining screens after first onDraw is called. This ensures that the first screen
2548 // is immediately visible (eg. during rotation)
2549 // In case of !isLoadingSynchronously, bind all pages one after other.
2550 final Executor deferredExecutor = isLoadingSynchronously ?
2551 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2552
2553 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2554 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002555
2556 // Tell the workspace that we're done binding items
2557 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002558 public void run() {
2559 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2560 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002561 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002562 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563
Sunny Goyal639e9062015-08-19 19:17:06 -07002564 mIsLoadingAndBindingWorkspace = false;
2565
2566 // Run all the bind complete runnables after workspace is bound.
2567 if (!mBindCompleteRunnables.isEmpty()) {
2568 synchronized (mBindCompleteRunnables) {
2569 for (final Runnable r : mBindCompleteRunnables) {
2570 runOnWorkerThread(r);
2571 }
2572 mBindCompleteRunnables.clear();
2573 }
2574 }
2575
Winson Chung98e030b2012-05-07 16:01:11 -07002576 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002577 if (DEBUG_LOADERS) {
2578 Log.d(TAG, "bound workspace in "
2579 + (SystemClock.uptimeMillis()-t) + "ms");
2580 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002581
Joe Onorato36115782010-06-17 13:28:48 -04002582 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002584 deferredExecutor.execute(r);
2585
Winson Chung4a2afa32012-07-19 14:53:05 -07002586 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002587 r = new Runnable() {
2588 public void run() {
2589 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2590 if (callbacks != null) {
2591 // We are loading synchronously, which means, some of the pages will be
2592 // bound after first draw. Inform the callbacks that page binding is
2593 // not complete, and schedule the remaining pages.
2594 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2595 callbacks.onPageBoundSynchronously(currentScreen);
2596 }
2597 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2598 }
2599 }
2600 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002601 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002602 }
Joe Onorato36115782010-06-17 13:28:48 -04002603 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002604
Joe Onorato36115782010-06-17 13:28:48 -04002605 private void loadAndBindAllApps() {
2606 if (DEBUG_LOADERS) {
2607 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2608 }
2609 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002610 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002611 synchronized (LoaderTask.this) {
2612 if (mStopped) {
2613 return;
2614 }
2615 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002616 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002617 synchronized (LoaderTask.this) {
2618 if (mStopped) {
2619 return;
2620 }
2621 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002622 }
Joe Onorato36115782010-06-17 13:28:48 -04002623 } else {
2624 onlyBindAllApps();
2625 }
2626 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002627
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002628 private void updateIconCache() {
2629 // Ignore packages which have a promise icon.
2630 HashSet<String> packagesToIgnore = new HashSet<>();
2631 synchronized (sBgLock) {
2632 for (ItemInfo info : sBgItemsIdMap) {
2633 if (info instanceof ShortcutInfo) {
2634 ShortcutInfo si = (ShortcutInfo) info;
2635 if (si.isPromise() && si.getTargetComponent() != null) {
2636 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2637 }
2638 } else if (info instanceof LauncherAppWidgetInfo) {
2639 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2640 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2641 packagesToIgnore.add(lawi.providerName.getPackageName());
2642 }
2643 }
2644 }
2645 }
2646 mIconCache.updateDbIcons(packagesToIgnore);
2647 }
2648
Joe Onorato36115782010-06-17 13:28:48 -04002649 private void onlyBindAllApps() {
2650 final Callbacks oldCallbacks = mCallbacks.get();
2651 if (oldCallbacks == null) {
2652 // This launcher has exited and nobody bothered to tell us. Just bail.
2653 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2654 return;
2655 }
2656
2657 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002658 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002659 final ArrayList<AppInfo> list
2660 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002661 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002662 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002663 public void run() {
2664 final long t = SystemClock.uptimeMillis();
2665 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2666 if (callbacks != null) {
2667 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002668 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002669 }
2670 if (DEBUG_LOADERS) {
2671 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2672 + (SystemClock.uptimeMillis()-t) + "ms");
2673 }
2674 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002675 };
2676 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002677 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002678 r.run();
2679 } else {
2680 mHandler.post(r);
2681 }
Joe Onorato36115782010-06-17 13:28:48 -04002682 }
2683
Winson Chung64359a52013-07-08 17:17:08 -07002684 private void loadAllApps() {
2685 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002686
Joe Onorato36115782010-06-17 13:28:48 -04002687 final Callbacks oldCallbacks = mCallbacks.get();
2688 if (oldCallbacks == null) {
2689 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002690 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002691 return;
2692 }
2693
Kenny Guyed131872014-04-30 03:02:21 +01002694 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2695
Winson Chung64359a52013-07-08 17:17:08 -07002696 // Clear the list of apps
2697 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002698 for (UserHandleCompat user : profiles) {
2699 // Query for the set of apps
2700 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002701 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002702 if (DEBUG_LOADERS) {
2703 Log.d(TAG, "getActivityList took "
2704 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2705 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2706 }
2707 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002708 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002709 if (apps == null || apps.isEmpty()) {
2710 return;
2711 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002712
Kenny Guyed131872014-04-30 03:02:21 +01002713 // Create the ApplicationInfos
2714 for (int i = 0; i < apps.size(); i++) {
2715 LauncherActivityInfoCompat app = apps.get(i);
2716 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002717 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002718 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002719
Sunny Goyal756a28a2015-04-23 17:07:55 -07002720 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2721 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002722 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002723
2724 @Override
2725 public void run() {
2726 heuristic.processUserApps(apps);
2727 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002728 };
2729 runOnMainThread(new Runnable() {
2730
2731 @Override
2732 public void run() {
2733 // Check isLoadingWorkspace on the UI thread, as it is updated on
2734 // the UI thread.
2735 if (mIsLoadingAndBindingWorkspace) {
2736 synchronized (mBindCompleteRunnables) {
2737 mBindCompleteRunnables.add(r);
2738 }
2739 } else {
2740 runOnWorkerThread(r);
2741 }
2742 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002743 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002744 }
Winson Chung64359a52013-07-08 17:17:08 -07002745 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002746 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002747 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2748 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002749
2750 // Post callback on main thread
2751 mHandler.post(new Runnable() {
2752 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002753
Winson Chung64359a52013-07-08 17:17:08 -07002754 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002755 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002756 if (callbacks != null) {
2757 callbacks.bindAllApplications(added);
2758 if (DEBUG_LOADERS) {
2759 Log.d(TAG, "bound " + added.size() + " apps in "
2760 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2761 }
2762 } else {
2763 Log.i(TAG, "not binding apps: no Launcher activity");
2764 }
2765 }
2766 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002767 // Cleanup any data stored for a deleted user.
2768 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002769
Sunny Goyal2d648b02015-08-11 13:56:28 -07002770 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002771 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002772 Log.d(TAG, "Icons processed in "
2773 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002774 }
2775 }
2776
2777 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002778 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002779 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002780 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2781 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2782 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2783 }
Joe Onorato36115782010-06-17 13:28:48 -04002784 }
2785 }
2786
Sunny Goyal75b0f552015-05-20 21:57:06 -07002787 /**
2788 * Called when the icons for packages have been updated in the icon cache.
2789 */
2790 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2791 final Callbacks callbacks = getCallback();
2792 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2793 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2794
2795 // If any package icon has changed (app was updated while launcher was dead),
2796 // update the corresponding shortcuts.
2797 synchronized (sBgLock) {
2798 for (ItemInfo info : sBgItemsIdMap) {
2799 if (info instanceof ShortcutInfo && user.equals(info.user)
2800 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2801 ShortcutInfo si = (ShortcutInfo) info;
2802 ComponentName cn = si.getTargetComponent();
2803 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2804 si.updateIcon(mIconCache);
2805 updatedShortcuts.add(si);
2806 }
2807 }
2808 }
2809 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2810 }
2811
2812 if (!updatedShortcuts.isEmpty()) {
2813 final UserHandleCompat userFinal = user;
2814 mHandler.post(new Runnable() {
2815
2816 public void run() {
2817 Callbacks cb = getCallback();
2818 if (cb != null && callbacks == cb) {
2819 cb.bindShortcutsChanged(updatedShortcuts,
2820 new ArrayList<ShortcutInfo>(), userFinal);
2821 }
2822 }
2823 });
2824 }
2825
2826 if (!updatedApps.isEmpty()) {
2827 mHandler.post(new Runnable() {
2828
2829 public void run() {
2830 Callbacks cb = getCallback();
2831 if (cb != null && callbacks == cb) {
2832 cb.bindAppsUpdated(updatedApps);
2833 }
2834 }
2835 });
2836 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002837
2838 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002839 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002840 }
2841
Joe Onorato36115782010-06-17 13:28:48 -04002842 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002843 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002844 }
2845
Adam Cohen091440a2015-03-18 14:16:05 -07002846 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002847
2848 @Override
2849 public void onReceive(Context context, Intent intent) {
2850 synchronized (sBgLock) {
2851 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2852 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002853 final PackageManager manager = context.getPackageManager();
2854 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2855 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002856 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2857 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002858 packagesRemoved.clear();
2859 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002860 for (String pkg : entry.getValue()) {
2861 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002862 boolean packageOnSdcard = launcherApps.isAppEnabled(
2863 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2864 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002865 packagesUnavailable.add(pkg);
2866 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002867 packagesRemoved.add(pkg);
2868 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002869 }
2870 }
2871 if (!packagesRemoved.isEmpty()) {
2872 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2873 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2874 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002875 if (!packagesUnavailable.isEmpty()) {
2876 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2877 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2878 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002879 }
Sunny Goyal34942622014-08-29 17:20:55 -07002880 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002881 }
2882 }
2883 }
2884
Joe Onorato36115782010-06-17 13:28:48 -04002885 private class PackageUpdatedTask implements Runnable {
2886 int mOp;
2887 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002888 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002889
2890 public static final int OP_NONE = 0;
2891 public static final int OP_ADD = 1;
2892 public static final int OP_UPDATE = 2;
2893 public static final int OP_REMOVE = 3; // uninstlled
2894 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2895
2896
Kenny Guyed131872014-04-30 03:02:21 +01002897 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002898 mOp = op;
2899 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002900 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002901 }
2902
2903 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002904 if (!mHasLoaderCompletedOnce) {
2905 // Loader has not yet run.
2906 return;
2907 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002908 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002909
2910 final String[] packages = mPackages;
2911 final int N = packages.length;
2912 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002913 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002914 for (int i=0; i<N; i++) {
2915 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002916 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002917 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002918 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002919
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002920 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2921 if (heuristic != null) {
2922 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002923 }
Joe Onorato36115782010-06-17 13:28:48 -04002924 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002925 }
Joe Onorato36115782010-06-17 13:28:48 -04002926 case OP_UPDATE:
2927 for (int i=0; i<N; i++) {
2928 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002929 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002930 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002931 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002932 }
2933 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002934 case OP_REMOVE: {
2935 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2936 if (heuristic != null) {
2937 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002938 }
Joe Onorato36115782010-06-17 13:28:48 -04002939 for (int i=0; i<N; i++) {
2940 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002941 mIconCache.removeIconsForPkg(packages[i], mUser);
2942 }
2943 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002944 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002945 case OP_UNAVAILABLE:
2946 for (int i=0; i<N; i++) {
2947 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2948 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002949 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002950 }
2951 break;
2952 }
2953
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002954 ArrayList<AppInfo> added = null;
2955 ArrayList<AppInfo> modified = null;
2956 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002957
Adam Cohen487f7dd2012-06-28 18:12:10 -07002958 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002959 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002960 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002961 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002962 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002963 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002964 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002965 }
Winson Chung5d55f332012-07-16 20:45:03 -07002966 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002967 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002968 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002969 }
2970
Sunny Goyale0f58d72014-11-10 18:05:31 -08002971 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04002972 if (callbacks == null) {
2973 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2974 return;
2975 }
2976
Sunny Goyal4390ace2014-10-13 11:33:11 -07002977 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
2978 new HashMap<ComponentName, AppInfo>();
2979
Joe Onorato36115782010-06-17 13:28:48 -04002980 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002981 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002982 for (AppInfo ai : added) {
2983 addedOrUpdatedApps.put(ai.componentName, ai);
2984 }
Joe Onorato36115782010-06-17 13:28:48 -04002985 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002986
Joe Onorato36115782010-06-17 13:28:48 -04002987 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002988 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002989 for (AppInfo ai : modified) {
2990 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002991 }
2992
Joe Onorato36115782010-06-17 13:28:48 -04002993 mHandler.post(new Runnable() {
2994 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002995 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002996 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002997 callbacks.bindAppsUpdated(modifiedFinal);
2998 }
2999 }
3000 });
3001 }
Winson Chung83892cc2013-05-01 16:53:33 -07003002
Sunny Goyal4390ace2014-10-13 11:33:11 -07003003 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003004 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003005 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3006 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3007 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3008
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003009 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003010 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003011 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003012 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3013 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003014 boolean infoUpdated = false;
3015 boolean shortcutUpdated = false;
3016
3017 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003018 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003019 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003020 Bitmap icon = Utilities.createIconBitmap(
3021 si.iconResource.packageName,
3022 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003023 if (icon != null) {
3024 si.setIcon(icon);
3025 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003026 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003027 }
3028 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003029
3030 ComponentName cn = si.getTargetComponent();
3031 if (cn != null && packageSet.contains(cn.getPackageName())) {
3032 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3033
3034 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003035 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3036 // Auto install icon
3037 PackageManager pm = context.getPackageManager();
3038 ResolveInfo matched = pm.resolveActivity(
3039 new Intent(Intent.ACTION_MAIN)
3040 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3041 PackageManager.MATCH_DEFAULT_ONLY);
3042 if (matched == null) {
3043 // Try to find the best match activity.
3044 Intent intent = pm.getLaunchIntentForPackage(
3045 cn.getPackageName());
3046 if (intent != null) {
3047 cn = intent.getComponent();
3048 appInfo = addedOrUpdatedApps.get(cn);
3049 }
3050
3051 if ((intent == null) || (appInfo == null)) {
3052 removedShortcuts.add(si);
3053 continue;
3054 }
3055 si.promisedIntent = intent;
3056 }
3057 }
3058
3059 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003060 if (appInfo != null) {
3061 si.flags = appInfo.flags;
3062 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063
Sunny Goyal756adbc2015-04-16 15:20:51 -07003064 si.intent = si.promisedIntent;
3065 si.promisedIntent = null;
3066 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003067 infoUpdated = true;
3068 si.updateIcon(mIconCache);
3069 }
3070
3071 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3072 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3073 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003074 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075 si.contentDescription = appInfo.contentDescription;
3076 infoUpdated = true;
3077 }
3078
3079 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3080 // Since package was just updated, the target must be available now.
3081 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3082 shortcutUpdated = true;
3083 }
3084 }
3085
3086 if (infoUpdated || shortcutUpdated) {
3087 updatedShortcuts.add(si);
3088 }
3089 if (infoUpdated) {
3090 updateItemInDatabase(context, si);
3091 }
3092 } else if (info instanceof LauncherAppWidgetInfo) {
3093 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3094 if (mUser.equals(widgetInfo.user)
3095 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3096 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003097 widgetInfo.restoreStatus &=
3098 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3099 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003100
3101 // adding this flag ensures that launcher shows 'click to setup'
3102 // if the widget has a config activity. In case there is no config
3103 // activity, it will be marked as 'restored' during bind.
3104 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3105
Sunny Goyal4390ace2014-10-13 11:33:11 -07003106 widgets.add(widgetInfo);
3107 updateItemInDatabase(context, widgetInfo);
3108 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 }
3110 }
3111 }
3112
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3114 mHandler.post(new Runnable() {
3115
3116 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003117 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118 if (callbacks == cb && cb != null) {
3119 callbacks.bindShortcutsChanged(
3120 updatedShortcuts, removedShortcuts, mUser);
3121 }
3122 }
3123 });
3124 if (!removedShortcuts.isEmpty()) {
3125 deleteItemsFromDatabase(context, removedShortcuts);
3126 }
3127 }
3128 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 mHandler.post(new Runnable() {
3130 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003131 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003132 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003134 }
3135 }
3136 });
3137 }
3138 }
3139
Winson Chungdf95eb12013-10-16 14:57:07 -07003140 final ArrayList<String> removedPackageNames =
3141 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003142 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003143 // Mark all packages in the broadcast to be removed
3144 removedPackageNames.addAll(Arrays.asList(packages));
3145 } else if (mOp == OP_UPDATE) {
3146 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003147 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003148 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003149 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003150 }
3151 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003152 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003153
Winson Chungdf95eb12013-10-16 14:57:07 -07003154 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003155 final int removeReason;
3156 if (mOp == OP_UNAVAILABLE) {
3157 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3158 } else {
3159 // Remove all the components associated with this package
3160 for (String pn : removedPackageNames) {
3161 deletePackageFromDatabase(context, pn, mUser);
3162 }
3163 // Remove all the specific components
3164 for (AppInfo a : removedApps) {
3165 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3166 deleteItemsFromDatabase(context, infos);
3167 }
3168 removeReason = 0;
3169 }
3170
Winson Chungdf95eb12013-10-16 14:57:07 -07003171 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003172 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003173 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003174 mHandler.post(new Runnable() {
3175 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003176 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003177 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003178 callbacks.bindComponentsRemoved(
3179 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003180 }
3181 }
3182 });
Joe Onoratobe386092009-11-17 17:32:16 -08003183 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003184
Sunny Goyal2d648b02015-08-11 13:56:28 -07003185 // Update widgets
3186 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3187 // Always refresh for a package event on secondary user
3188 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3189
3190 // Refresh widget list, if the package already had a widget.
3191 synchronized (sBgLock) {
3192 if (sBgWidgetProviders != null) {
3193 HashSet<String> pkgSet = new HashSet<>();
3194 Collections.addAll(pkgSet, mPackages);
3195
3196 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3197 needToRefresh |= key.user.equals(mUser) &&
3198 pkgSet.contains(key.componentName.getPackageName());
3199 }
3200 }
3201 }
3202
3203 if (!needToRefresh && mOp != OP_REMOVE) {
3204 // Refresh widget list, if there is any newly added widget
3205 PackageManager pm = context.getPackageManager();
3206 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003207 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003208 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003209 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003210 .setPackage(pkg), 0);
3211 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003212 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003213 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003214 throw e;
3215 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003216 // Ignore the crash. We can live with a state widget list.
3217 Log.e(TAG, "PM call failed for " + pkg, e);
3218 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003219 }
3220 }
3221
3222 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3223 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 }
3225 }
3226
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003227 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3228 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003229 ArrayList<LauncherAppWidgetProviderInfo> results =
3230 new ArrayList<LauncherAppWidgetProviderInfo>();
3231 try {
3232 synchronized (sBgLock) {
3233 if (sBgWidgetProviders == null || refresh) {
3234 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3235 = new HashMap<>();
3236 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3237 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003238
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003239 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3240 for (AppWidgetProviderInfo pInfo : widgets) {
3241 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3242 UserHandleCompat user = wm.getUser(info);
3243 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3244 }
Robin Lee26ace122015-03-16 19:41:43 +00003245
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003246 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3247 for (CustomAppWidget widget : customWidgets) {
3248 info = new LauncherAppWidgetProviderInfo(context, widget);
3249 UserHandleCompat user = wm.getUser(info);
3250 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3251 }
3252 // Replace the global list at the very end, so that if there is an exception,
3253 // previously loaded provider list is used.
3254 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003255 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003256 results.addAll(sBgWidgetProviders.values());
3257 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003258 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003259 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003260 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003261 (e.getCause() instanceof TransactionTooLargeException ||
3262 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003263 // the returned value may be incomplete and will not be refreshed until the next
3264 // time Launcher starts.
3265 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3266 // onResume is called to refresh the widget provider list.
3267 synchronized (sBgLock) {
3268 if (sBgWidgetProviders != null) {
3269 results.addAll(sBgWidgetProviders.values());
3270 }
3271 return results;
3272 }
3273 } else {
3274 throw e;
3275 }
Adam Cohen59400422014-03-05 18:07:04 -08003276 }
3277 }
3278
Robin Lee26ace122015-03-16 19:41:43 +00003279 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3280 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003281 synchronized (sBgLock) {
3282 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003283 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003284 }
Robin Lee26ace122015-03-16 19:41:43 +00003285 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003286 }
3287 }
3288
Sunny Goyal2d648b02015-08-11 13:56:28 -07003289 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003290
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003291 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003292 @Override
3293 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003294 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003295 final WidgetsModel model = mBgWidgetsModel.clone();
3296
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003297 mHandler.post(new Runnable() {
3298 @Override
3299 public void run() {
3300 Callbacks cb = getCallback();
3301 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003302 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003303 }
3304 }
3305 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003306 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003307 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3308 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003309 }
3310 });
3311 }
3312
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003313 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003314 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003315 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003316 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003317 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003318 @Thunk void updateWidgetsModel(boolean refresh) {
3319 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003320 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003321 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003322
3323 // Update shortcut providers
3324 synchronized (sBgLock) {
3325 try {
3326 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3327 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3328 sBgShortcutProviders = providers;
3329 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003330 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003331 (e.getCause() instanceof TransactionTooLargeException ||
3332 e.getCause() instanceof DeadObjectException)) {
3333 /**
3334 * Ignore exception and use the cached list if available.
3335 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3336 */
3337 } else {
3338 throw e;
3339 }
3340 }
3341 if (sBgShortcutProviders != null) {
3342 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3343 }
3344 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003345 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003346 }
3347
Adam Cohen091440a2015-03-18 14:16:05 -07003348 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003349 UserHandleCompat user) {
3350 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3351 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003352 }
Adam Cohen556f6132014-01-15 15:18:08 -08003353
Kenny Guyed131872014-04-30 03:02:21 +01003354 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3355 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003356 if (cn == null) {
3357 return false;
3358 }
Kenny Guyed131872014-04-30 03:02:21 +01003359 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3360 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003361 return false;
3362 }
Kenny Guyed131872014-04-30 03:02:21 +01003363 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003364 }
3365
Adam Cohena28b78e2014-05-20 17:03:04 -07003366 public static boolean isValidPackage(Context context, String packageName,
3367 UserHandleCompat user) {
3368 if (packageName == null) {
3369 return false;
3370 }
3371 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3372 return launcherApps.isPackageEnabledForProfile(packageName, user);
3373 }
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003376 * Make an ShortcutInfo object for a restored application or shortcut item that points
3377 * to a package that is not yet installed on the system.
3378 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003379 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003380 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003381 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003382 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003383
3384 Bitmap icon = iconInfo.loadIcon(c, info, context);
3385 // the fallback icon
3386 if (icon == null) {
3387 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3388 } else {
3389 info.setIcon(icon);
3390 }
Sunny Goyal34942622014-08-29 17:20:55 -07003391
3392 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003393 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003394 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003395 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003396 }
Sunny Goyal34942622014-08-29 17:20:55 -07003397 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3398 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003399 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003400 }
Sunny Goyal34942622014-08-29 17:20:55 -07003401 } else {
3402 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3403 }
3404
Winson Chung82b016c2015-05-08 17:00:10 -07003405 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003406 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003407 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003408 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003409 return info;
3410 }
3411
3412 /**
3413 * Make an Intent object for a restored application or shortcut item that points
3414 * to the market page for the item.
3415 */
Adam Cohen091440a2015-03-18 14:16:05 -07003416 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003417 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003418 return getMarketIntent(componentName.getPackageName());
3419 }
3420
3421 static Intent getMarketIntent(String packageName) {
3422 return new Intent(Intent.ACTION_VIEW)
3423 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 .scheme("market")
3425 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003426 .appendQueryParameter("id", packageName)
3427 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003428 }
3429
3430 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003431 * Make an ShortcutInfo object for a shortcut that is an application.
3432 *
3433 * If c is not null, then it will be used to fill in missing data like the title and icon.
3434 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003435 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003436 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003437 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003438 if (user == null) {
3439 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003440 return null;
3441 }
3442
Kenny Guyed131872014-04-30 03:02:21 +01003443 ComponentName componentName = intent.getComponent();
3444 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003445 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003446 return null;
3447 }
3448
3449 Intent newIntent = new Intent(intent.getAction(), null);
3450 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3451 newIntent.setComponent(componentName);
3452 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003453 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003454 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3455 return null;
3456 }
3457
3458 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003459 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003460 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3461 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3462 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003463 }
3464
Joe Onorato56d82912010-03-07 14:32:10 -05003465 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003466 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003467 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003468 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003469
Joe Onorato56d82912010-03-07 14:32:10 -05003470 // fall back to the class name of the activity
3471 if (info.title == null) {
3472 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003473 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003474
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003476 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003477 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003478 if (lai != null) {
3479 info.flags = AppInfo.initFlags(lai);
3480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003482 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003483
Sunny Goyale2df0622015-04-24 11:27:00 -07003484 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003485 ItemInfoFilter f) {
3486 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3487 for (ItemInfo i : infos) {
3488 if (i instanceof ShortcutInfo) {
3489 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003490 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003491 if (cn != null && f.filterItem(null, info, cn)) {
3492 filtered.add(info);
3493 }
3494 } else if (i instanceof FolderInfo) {
3495 FolderInfo info = (FolderInfo) i;
3496 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003497 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003498 if (cn != null && f.filterItem(info, s, cn)) {
3499 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003500 }
3501 }
Winson Chung64359a52013-07-08 17:17:08 -07003502 } else if (i instanceof LauncherAppWidgetInfo) {
3503 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3504 ComponentName cn = info.providerName;
3505 if (cn != null && f.filterItem(null, info, cn)) {
3506 filtered.add(info);
3507 }
Winson Chung8a435102012-08-30 17:16:53 -07003508 }
3509 }
Winson Chung64359a52013-07-08 17:17:08 -07003510 return new ArrayList<ItemInfo>(filtered);
3511 }
3512
Adam Cohen091440a2015-03-18 14:16:05 -07003513 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003514 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003515 ItemInfoFilter filter = new ItemInfoFilter() {
3516 @Override
3517 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003518 if (info.user == null) {
3519 return cn.equals(cname);
3520 } else {
3521 return cn.equals(cname) && info.user.equals(user);
3522 }
Winson Chung64359a52013-07-08 17:17:08 -07003523 }
3524 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003525 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003526 }
3527
Sunny Goyal1a745e82014-10-02 15:58:31 -07003528 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003529 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003530 */
Adam Cohen091440a2015-03-18 14:16:05 -07003531 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003532 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003533 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003534 // Non-app shortcuts are only supported for current user.
3535 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003538 // TODO: If there's an explicit component and we can't install that, delete it.
3539
Winson Chung82b016c2015-05-08 17:00:10 -07003540 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003541
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003542 Bitmap icon = iconInfo.loadIcon(c, info, context);
3543 // the fallback icon
3544 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003545 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003546 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003548 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549 return info;
3550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003551
Sunny Goyal2350bc92014-10-14 16:42:54 -07003552 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003553 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3554 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3555 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3556
Adam Cohend9198822011-11-22 16:42:47 -08003557 if (intent == null) {
3558 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3559 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3560 return null;
3561 }
3562
Joe Onorato0589f0f2010-02-08 13:44:00 -08003563 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003564 boolean customIcon = false;
3565 ShortcutIconResource iconResource = null;
3566
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003567 if (bitmap instanceof Bitmap) {
3568 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003569 customIcon = true;
3570 } else {
3571 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003572 if (extra instanceof ShortcutIconResource) {
3573 iconResource = (ShortcutIconResource) extra;
3574 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003575 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003576 }
3577 }
3578
Michael Jurkac9d95c52011-08-29 14:03:34 -07003579 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003580
Kenny Guyed131872014-04-30 03:02:21 +01003581 // Only support intents for current user for now. Intents sent from other
3582 // users wouldn't get here without intent forwarding anyway.
3583 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003584 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003585 icon = mIconCache.getDefaultIcon(info.user);
3586 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003587 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003588 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003589
Winson Chung82b016c2015-05-08 17:00:10 -07003590 info.title = Utilities.trim(name);
3591 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003592 info.intent = intent;
3593 info.customIcon = customIcon;
3594 info.iconResource = iconResource;
3595
3596 return info;
3597 }
3598
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003600 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 * or make a new one.
3602 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003603 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 // See if a placeholder was created for us already
3605 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003606 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003608 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 folders.put(id, folderInfo);
3610 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003611 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003612 }
3613
Joe Onoratobe386092009-11-17 17:32:16 -08003614
Sunny Goyal651077b2014-06-30 14:15:31 -07003615 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3616 return (provider != null) && (provider.provider != null)
3617 && (provider.provider.getPackageName() != null);
3618 }
3619
Joe Onoratobe386092009-11-17 17:32:16 -08003620 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003621 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003622 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3623 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3624 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3625 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003626 if (mLoaderTask != null) {
3627 mLoaderTask.dumpState();
3628 } else {
3629 Log.d(TAG, "mLoaderTask=null");
3630 }
Joe Onoratobe386092009-11-17 17:32:16 -08003631 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003632
3633 public Callbacks getCallback() {
3634 return mCallbacks != null ? mCallbacks.get() : null;
3635 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003636
3637 /**
3638 * @return {@link FolderInfo} if its already loaded.
3639 */
3640 public FolderInfo findFolderById(Long folderId) {
3641 synchronized (sBgLock) {
3642 return sBgFolders.get(folderId);
3643 }
3644 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003645
Sunny Goyal527c7d32015-08-28 15:19:36 -07003646 @Thunk class DeferredMainThreadExecutor implements Executor {
3647
3648 @Override
3649 public void execute(Runnable command) {
3650 runOnMainThread(command);
3651 }
3652 }
3653
Sunny Goyal756adbc2015-04-16 15:20:51 -07003654 /**
3655 * @return the looper for the worker thread which can be used to start background tasks.
3656 */
3657 public static Looper getWorkerLooper() {
3658 return sWorkerThread.getLooper();
3659 }
Sunny Goyala1365452015-10-01 15:46:24 -07003660
3661 @Thunk static final void addDumpLog(String log) {
3662 Launcher.addDumpLog(TAG, log);
3663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003664}