blob: 9208d329ede6ecd12df0ca7e605c6c617d83fe18 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080036import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040037import android.os.Handler;
38import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070039import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040043import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080044import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080046import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070047import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010048
Sunny Goyalffe83f12014-08-14 17:39:34 -070049import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010050import com.android.launcher3.compat.LauncherActivityInfoCompat;
51import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070052import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070053import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010054import com.android.launcher3.compat.UserHandleCompat;
55import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000057import com.android.launcher3.folder.Folder;
58import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf862a262015-12-14 14:27:38 -080059import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070063import com.android.launcher3.util.FlagOp;
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;
Sunny Goyalda891c12016-03-18 18:29:24 -070066import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070067import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070068import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080069
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.lang.ref.WeakReference;
71import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070072import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070074import java.util.Arrays;
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 >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800138 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700139 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800140 private final 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
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700167 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700168 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
169 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700170
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // </ only access in worker thread >
172
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Adam Cohen091440a2015-03-18 14:16:05 -0700175 @Thunk final LauncherAppsCompat mLauncherApps;
176 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700179 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700181 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700183 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
184 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700187 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800188 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
197 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
202 UserHandleCompat user);
203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800204 public void notifyWidgetProvidersChanged();
205 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700206 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700207 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700209 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Winson Chungee055712013-07-30 14:46:24 -0700219 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700220 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400221 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
222 // resource string.
223 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
224 ProviderInfo providerInfo =
225 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
226 ProviderInfo redirectProvider =
227 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700228
229 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400230 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 if (mOldContentProviderExists) {
233 Log.d(TAG, "Old launcher provider exists.");
234 } else {
235 Log.d(TAG, "Old launcher provider does not exist.");
236 }
237
Daniel Sandlere4f98912013-06-25 15:13:26 -0400238 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100239 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700240 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 mIconCache = iconCache;
242
Kenny Guyed131872014-04-30 03:02:21 +0100243 mLauncherApps = LauncherAppsCompat.getInstance(context);
244 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700247 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
248 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 // If we are on the worker thread, post onto the main handler
252 mHandler.post(r);
253 } else {
254 r.run();
255 }
256 }
257
258 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
259 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700260 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700261 if (sWorkerThread.getThreadId() == Process.myTid()) {
262 r.run();
263 } else {
264 // If we are not on the worker thread, then post to the worker handler
265 sWorker.post(r);
266 }
267 }
268
Winson Chunge43a1e72014-01-15 10:33:02 -0800269 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
270 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800271 }
272
Sunny Goyal756adbc2015-04-16 15:20:51 -0700273 public void setPackageState(final PackageInstallInfo installInfo) {
274 Runnable updateRunnable = new Runnable() {
275
276 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500277 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700278 synchronized (sBgLock) {
279 final HashSet<ItemInfo> updates = new HashSet<>();
280
281 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
282 // Ignore install success events as they are handled by Package add events.
283 return;
284 }
285
Sunny Goyale2df0622015-04-24 11:27:00 -0700286 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700287 if (info instanceof ShortcutInfo) {
288 ShortcutInfo si = (ShortcutInfo) info;
289 ComponentName cn = si.getTargetComponent();
290 if (si.isPromise() && (cn != null)
291 && installInfo.packageName.equals(cn.getPackageName())) {
292 si.setInstallProgress(installInfo.progress);
293
294 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
295 // Mark this info as broken.
296 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
297 }
298 updates.add(si);
299 }
300 }
301 }
302
303 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
304 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
305 widget.installProgress = installInfo.progress;
306 updates.add(widget);
307 }
308 }
309
310 if (!updates.isEmpty()) {
311 // Push changes to the callback.
312 Runnable r = new Runnable() {
313 public void run() {
314 Callbacks callbacks = getCallback();
315 if (callbacks != null) {
316 callbacks.bindRestoreItemsChange(updates);
317 }
318 }
319 };
320 mHandler.post(r);
321 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500322 }
323 }
324 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700325 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500326 }
327
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 /**
329 * Updates the icons and label of all pending icons for the provided package name.
330 */
331 public void updateSessionDisplayInfo(final String packageName) {
332 Runnable updateRunnable = new Runnable() {
333
334 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700335 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700336 synchronized (sBgLock) {
337 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
338 final UserHandleCompat user = UserHandleCompat.myUserHandle();
339
Sunny Goyale2df0622015-04-24 11:27:00 -0700340 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 if (info instanceof ShortcutInfo) {
342 ShortcutInfo si = (ShortcutInfo) info;
343 ComponentName cn = si.getTargetComponent();
344 if (si.isPromise() && (cn != null)
345 && packageName.equals(cn.getPackageName())) {
346 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
347 // For auto install apps update the icon as well as label.
348 mIconCache.getTitleAndIcon(si,
349 si.promisedIntent, user,
350 si.shouldUseLowResIcon());
351 } else {
352 // Only update the icon for restored apps.
353 si.updateIcon(mIconCache);
354 }
355 updates.add(si);
356 }
357 }
358 }
359
360 if (!updates.isEmpty()) {
361 // Push changes to the callback.
362 Runnable r = new Runnable() {
363 public void run() {
364 Callbacks callbacks = getCallback();
365 if (callbacks != null) {
366 callbacks.bindShortcutsChanged(updates,
367 new ArrayList<ShortcutInfo>(), user);
368 }
369 }
370 };
371 mHandler.post(r);
372 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700373 }
374 }
375 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700376 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700377 }
378
Adam Cohen76a47a12014-02-05 11:47:43 -0800379 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800380 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800381
382 if (allAppsApps == null) {
383 throw new RuntimeException("allAppsApps must not be null");
384 }
385 if (allAppsApps.isEmpty()) {
386 return;
387 }
388
389 // Process the newly added applications and add them to the database first
390 Runnable r = new Runnable() {
391 public void run() {
392 runOnMainThread(new Runnable() {
393 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800394 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800395 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500396 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800397 }
398 }
399 });
400 }
401 };
402 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700403 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800404
Sunny Goyala9116722015-04-29 13:55:58 -0700405 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800406 int[] xy, int spanX, int spanY) {
407 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700408 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
409 final int xCount = (int) profile.numColumns;
410 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 boolean[][] occupied = new boolean[xCount][yCount];
412 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700413 for (ItemInfo r : occupiedPos) {
414 int right = r.cellX + r.spanX;
415 int bottom = r.cellY + r.spanY;
416 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
417 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418 occupied[x][y] = true;
419 }
420 }
421 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800422 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700423 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 }
425
426 /**
427 * Find a position on the screen for the given size or adds a new screen.
428 * @return screenId and the coordinates for the item.
429 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700430 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700431 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 ArrayList<Long> workspaceScreens,
433 ArrayList<Long> addedWorkspaceScreensFinal,
434 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700435 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436
Sunny Goyala9116722015-04-29 13:55:58 -0700437 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700438 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700439 synchronized (sBgLock) {
440 for (ItemInfo info : sBgItemsIdMap) {
441 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
442 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
443 if (items == null) {
444 items = new ArrayList<>();
445 screenItems.put(info.screenId, items);
446 }
447 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
451
452 // Find appropriate space for the item.
453 long screenId = 0;
454 int[] cordinates = new int[2];
455 boolean found = false;
456
457 int screenCount = workspaceScreens.size();
458 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700459 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800460 if (preferredScreenIndex < screenCount) {
461 screenId = workspaceScreens.get(preferredScreenIndex);
462 found = findNextAvailableIconSpaceInScreen(
463 screenItems.get(screenId), cordinates, spanX, spanY);
464 }
465
466 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700467 // Search on any of the screens starting from the first screen.
468 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 screenId = workspaceScreens.get(screen);
470 if (findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY)) {
472 // We found a space for it
473 found = true;
474 break;
475 }
476 }
477 }
478
479 if (!found) {
480 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700481 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
482 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
483 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484
485 // Save the screen id for binding in the workspace
486 workspaceScreens.add(screenId);
487 addedWorkspaceScreensFinal.add(screenId);
488
489 // If we still can't find an empty space, then God help us all!!!
490 if (!findNextAvailableIconSpaceInScreen(
491 screenItems.get(screenId), cordinates, spanX, spanY)) {
492 throw new RuntimeException("Can't find space to add the item");
493 }
494 }
495 return Pair.create(screenId, cordinates);
496 }
497
498 /**
499 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700501 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700502 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800504 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700505 return;
Winson Chung997a9232013-07-24 15:33:46 -0700506 }
Winson Chung64359a52013-07-08 17:17:08 -0700507 // Process the newly added applications and add them to the database first
508 Runnable r = new Runnable() {
509 public void run() {
510 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
511 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
512
Winson Chung76828c82013-08-19 15:43:29 -0700513 // Get the list of workspace screens. We need to append to this list and
514 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
515 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800516 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700517 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700519 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700521 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700523 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 }
Winson Chung76828c82013-08-19 15:43:29 -0700525
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700527 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700528 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 long screenId = coords.first;
530 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700531
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 ItemInfo itemInfo;
533 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
534 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700536 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700537 } else {
538 throw new RuntimeException("Unexpected info type");
539 }
Winson Chung94d67682013-09-25 16:29:40 -0700540
Winson Chung64359a52013-07-08 17:17:08 -0700541 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700543 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700544 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700545 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700547 }
548 }
549
Winson Chung76828c82013-08-19 15:43:29 -0700550 // Update the workspace screens
551 updateWorkspaceScreenOrder(context, workspaceScreens);
552
Adam Cohen76a47a12014-02-05 11:47:43 -0800553 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700554 runOnMainThread(new Runnable() {
555 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800556 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700557 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700558 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
559 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700560 if (!addedShortcutsFinal.isEmpty()) {
561 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
562 long lastScreenId = info.screenId;
563 for (ItemInfo i : addedShortcutsFinal) {
564 if (i.screenId == lastScreenId) {
565 addAnimated.add(i);
566 } else {
567 addNotAnimated.add(i);
568 }
Winson Chung997a9232013-07-24 15:33:46 -0700569 }
570 }
Winson Chungd64d1762013-08-20 14:37:16 -0700571 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800572 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700573 }
Winson Chung64359a52013-07-08 17:17:08 -0700574 }
Winson Chung997a9232013-07-24 15:33:46 -0700575 });
576 }
Winson Chung64359a52013-07-08 17:17:08 -0700577 }
578 };
579 runOnWorkerThread(r);
580 }
581
Sunny Goyald33860f2015-04-23 16:02:20 -0700582 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700583 if (sWorkerThread.getThreadId() == Process.myTid()) {
584 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
585 "main thread");
586 }
587
Sunny Goyald33860f2015-04-23 16:02:20 -0700588 // Remove any queued UI runnables
589 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700590 // Unbind all the workspace items
591 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700592 }
593
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700594 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700595 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700596 // Ensure that we don't use the same workspace items data structure on the main thread
597 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700598 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700599 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700600 tmpItems.addAll(sBgWorkspaceItems);
601 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602 }
603 Runnable r = new Runnable() {
604 @Override
605 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700606 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 item.unbind();
608 }
609 }
610 };
611 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700612 }
613
Joe Onorato9c1289c2009-08-17 11:03:03 -0400614 /**
615 * Adds an item to the DB if it was not created previously, or move it to a new
616 * <container, screen, cellX, cellY>
617 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800618 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700619 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620 if (item.container == ItemInfo.NO_ID) {
621 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700622 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 } else {
624 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 }
627 }
628
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700629 static void checkItemInfoLocked(
630 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
631 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
632 if (modelItem != null && item != modelItem) {
633 // check all the data is consistent
634 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
635 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
636 ShortcutInfo shortcut = (ShortcutInfo) item;
637 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
638 modelShortcut.intent.filterEquals(shortcut.intent) &&
639 modelShortcut.id == shortcut.id &&
640 modelShortcut.itemType == shortcut.itemType &&
641 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700643 modelShortcut.cellX == shortcut.cellX &&
644 modelShortcut.cellY == shortcut.cellY &&
645 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700646 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700647 // For all intents and purposes, this is the same object
648 return;
649 }
650 }
651
652 // the modelItem needs to match up perfectly with item if our model is
653 // to be consistent with the database-- for now, just require
654 // modelItem == item or the equality check above
655 String msg = "item: " + ((item != null) ? item.toString() : "null") +
656 "modelItem: " +
657 ((modelItem != null) ? modelItem.toString() : "null") +
658 "Error: ItemInfo passed to checkItemInfo doesn't match original";
659 RuntimeException e = new RuntimeException(msg);
660 if (stackTrace != null) {
661 e.setStackTrace(stackTrace);
662 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800663 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700664 }
665 }
666
Michael Jurka816474f2012-06-25 14:49:02 -0700667 static void checkItemInfo(final ItemInfo item) {
668 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
669 final long itemId = item.id;
670 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700671 public void run() {
672 synchronized (sBgLock) {
673 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700674 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700675 }
676 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700677 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700678 }
679
Michael Jurkac9d95c52011-08-29 14:03:34 -0700680 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
681 final ItemInfo item, final String callingFunction) {
682 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700683 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700684 final ContentResolver cr = context.getContentResolver();
685
Adam Cohen487f7dd2012-06-28 18:12:10 -0700686 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700687 Runnable r = new Runnable() {
688 public void run() {
689 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 updateItemArrays(item, itemId, stackTrace);
691 }
692 };
693 runOnWorkerThread(r);
694 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700695
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
697 final ArrayList<ItemInfo> items, final String callingFunction) {
698 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700699
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
701 Runnable r = new Runnable() {
702 public void run() {
703 ArrayList<ContentProviderOperation> ops =
704 new ArrayList<ContentProviderOperation>();
705 int count = items.size();
706 for (int i = 0; i < count; i++) {
707 ItemInfo item = items.get(i);
708 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700709 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700711
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700712 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
713 updateItemArrays(item, itemId, stackTrace);
714
715 }
716 try {
717 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
718 } catch (Exception e) {
719 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720 }
721 }
722 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700723 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700725
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
727 // Lock on mBgLock *after* the db operation
728 synchronized (sBgLock) {
729 checkItemInfoLocked(itemId, item, stackTrace);
730
731 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
732 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
733 // Item is in a folder, make sure this folder exists
734 if (!sBgFolders.containsKey(item.container)) {
735 // An items container is being set to a that of an item which is not in
736 // the list of Folders.
737 String msg = "item: " + item + " container being set to: " +
738 item.container + ", not in the list of folders";
739 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 }
741 }
742
743 // Items are added/removed from the corresponding FolderInfo elsewhere, such
744 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
745 // that are on the desktop, as appropriate
746 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800747 if (modelItem != null &&
748 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
749 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 switch (modelItem.itemType) {
751 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
752 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
753 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
754 if (!sBgWorkspaceItems.contains(modelItem)) {
755 sBgWorkspaceItems.add(modelItem);
756 }
757 break;
758 default:
759 break;
760 }
761 } else {
762 sBgWorkspaceItems.remove(modelItem);
763 }
764 }
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700769 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700770 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 item.cellX = cellX;
774 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700775
Winson Chung3d503fb2011-07-13 17:25:49 -0700776 // We store hotseat items in canonical form which is this orientation invariant position
777 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700781 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784
785 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
788 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800789 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700793 }
794
795 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700796 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
797 * cellX, cellY have already been updated on the ItemInfos.
798 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800799 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700800 final long container, final int screen) {
801
802 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
803 int count = items.size();
804
805 for (int i = 0; i < count; i++) {
806 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700807 item.container = container;
808
809 // We store hotseat items in canonical form which is this orientation invariant position
810 // in the hotseat
811 if (context instanceof Launcher && screen < 0 &&
812 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700813 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 item.cellY);
815 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 }
818
819 final ContentValues values = new ContentValues();
820 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
821 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
822 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800823 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825
826 contentValues.add(values);
827 }
828 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
829 }
830
831 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700832 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800833 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700834 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700836 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800837 item.cellX = cellX;
838 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700839 item.spanX = spanX;
840 item.spanY = spanY;
841
842 // We store hotseat items in canonical form which is this orientation invariant position
843 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700847 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 }
Adam Cohend4844c32011-02-18 19:25:06 -0800850
Adam Cohend4844c32011-02-18 19:25:06 -0800851 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800852 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
854 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800855 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
857 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800859
Michael Jurka816474f2012-06-25 14:49:02 -0700860 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862
863 /**
864 * Update an item to the database in a specified container.
865 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700866 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700867 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100868 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800870 }
871
Sunny Goyal756a28a2015-04-23 17:07:55 -0700872 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700873 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700874 synchronized (mLock) {
875 if (!mHasLoaderCompletedOnce ||
876 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
877 throw new RuntimeException("Trying to add shortcut while loader is running");
878 }
879 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700880 }
881 }
882
Adam Cohend4844c32011-02-18 19:25:06 -0800883 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700884 * Returns true if the shortcuts already exists on the workspace. This must be called after
885 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700887 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
888 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700889 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700890 if (intent.getComponent() != null) {
891 // If component is not null, an intent with null package will produce
892 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700893 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700894 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700895 intentWithPkg = intent.toUri(0);
896 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700897 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
899 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 }
901 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700902 intentWithPkg = intent.toUri(0);
903 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700905
906 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700907 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700908 if (item instanceof ShortcutInfo) {
909 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700910 Intent targetIntent = info.promisedIntent == null
911 ? info.intent : info.promisedIntent;
912 if (targetIntent != null && info.user.equals(user)) {
913 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700914 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
915 return true;
916 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917 }
918 }
919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700921 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700922 }
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 * Add an item to the database in a specified container. Sets the container, screen, cellX and
926 * cellY fields of the item. Also assigns an ID to the item.
927 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700928 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700929 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 item.cellX = cellX;
932 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 // We store hotseat items in canonical form which is this orientation invariant position
934 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 final ContentValues values = new ContentValues();
943 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100944 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945
Sunny Goyald2497482015-09-22 18:24:19 -0700946 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
947 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
948
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700949 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700950
Jason Monk8e19cf22014-03-20 15:06:57 -0400951 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700952 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700953 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700954 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700957 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400958 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700959 sBgItemsIdMap.put(item.id, item);
960 switch (item.itemType) {
961 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
962 sBgFolders.put(item.id, (FolderInfo) item);
963 // Fall through
964 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
965 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
966 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
967 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
968 sBgWorkspaceItems.add(item);
969 } else {
970 if (!sBgFolders.containsKey(item.container)) {
971 // Adding an item to a folder that doesn't exist.
972 String msg = "adding item: " + item + " to a folder that " +
973 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700974 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700976 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700977 break;
978 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
979 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
980 break;
981 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700982 }
983 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700984 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700986 }
987
Sunny Goyal34942622014-08-29 17:20:55 -0700988 private static ArrayList<ItemInfo> getItemsByPackageName(
989 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700990 ItemInfoFilter filter = new ItemInfoFilter() {
991 @Override
992 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
993 return cn.getPackageName().equals(pn) && info.user.equals(user);
994 }
995 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700996 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700997 }
998
999 /**
1000 * Removes all the items from the database corresponding to the specified package.
1001 */
1002 static void deletePackageFromDatabase(Context context, final String pn,
1003 final UserHandleCompat user) {
1004 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001005 }
1006
1007 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001008 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001010 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001011 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1012 items.add(item);
1013 deleteItemsFromDatabase(context, items);
1014 }
1015
1016 /**
1017 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001018 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001019 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001021 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001022 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001023 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001024 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001025 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001027 // Lock on mBgLock *after* the db operation
1028 synchronized (sBgLock) {
1029 switch (item.itemType) {
1030 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1031 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001032 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001033 if (info.container == item.id) {
1034 // We are deleting a folder which still contains items that
1035 // think they are contained by that folder.
1036 String msg = "deleting a folder (" + item + ") which still " +
1037 "contains items (" + info + ")";
1038 Log.e(TAG, msg);
1039 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001040 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001041 sBgWorkspaceItems.remove(item);
1042 break;
1043 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1044 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1045 sBgWorkspaceItems.remove(item);
1046 break;
1047 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1048 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1049 break;
1050 }
1051 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001053 }
1054 }
Michael Jurka83df1882011-08-31 20:59:26 -07001055 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 }
1058
1059 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 * Update the order of the workspace screens in the database. The array list contains
1061 * a list of screen ids in the order that they should appear.
1062 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001063 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001064 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 final ContentResolver cr = context.getContentResolver();
1066 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1067
1068 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001069 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 while (iter.hasNext()) {
1071 long id = iter.next();
1072 if (id < 0) {
1073 iter.remove();
1074 }
1075 }
1076
1077 Runnable r = new Runnable() {
1078 @Override
1079 public void run() {
Yura085c8532014-02-11 15:15:29 +00001080 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001082 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001083 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 for (int i = 0; i < count; i++) {
1085 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001086 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1088 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001089 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 }
Yura085c8532014-02-11 15:15:29 +00001091
1092 try {
1093 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1094 } catch (Exception ex) {
1095 throw new RuntimeException(ex);
1096 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001097
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001099 sBgWorkspaceScreens.clear();
1100 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001101 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001102 }
1103 };
1104 runOnWorkerThread(r);
1105 }
1106
1107 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001108 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 */
Winsonc0b52fe2015-09-09 16:38:15 -07001110 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 final ContentResolver cr = context.getContentResolver();
1112
Michael Jurkac9d95c52011-08-29 14:03:34 -07001113 Runnable r = new Runnable() {
1114 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001115 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001117 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 sBgItemsIdMap.remove(info.id);
1119 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 sBgWorkspaceItems.remove(info);
1121 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001123 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001124 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001126 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 for (ItemInfo childInfo : info.contents) {
1128 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001130 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001131 }
1132 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
1135
1136 /**
1137 * Set this as the current Launcher activity object for the loader.
1138 */
1139 public void initialize(Callbacks callbacks) {
1140 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001141 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1142 // workspace to prevent leaking Launcher activities on orientation change.
1143 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 mCallbacks = new WeakReference<Callbacks>(callbacks);
1145 }
1146 }
1147
Kenny Guyed131872014-04-30 03:02:21 +01001148 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001149 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001150 int op = PackageUpdatedTask.OP_UPDATE;
1151 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1152 user));
1153 }
1154
1155 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001156 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001157 int op = PackageUpdatedTask.OP_REMOVE;
1158 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1159 user));
1160 }
1161
1162 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001163 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001164 int op = PackageUpdatedTask.OP_ADD;
1165 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1166 user));
1167 }
1168
1169 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001170 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001171 boolean replacing) {
1172 if (!replacing) {
1173 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1174 user));
1175 if (mAppsCanBeOnRemoveableStorage) {
1176 // Only rebind if we support removable storage. It catches the
1177 // case where
1178 // apps on the external sd card need to be reloaded
1179 startLoaderFromBackground();
1180 }
1181 } else {
1182 // If we are replacing then just update the packages in the list
1183 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1184 packageNames, user));
1185 }
1186 }
1187
1188 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001189 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001190 boolean replacing) {
1191 if (!replacing) {
1192 enqueuePackageUpdated(new PackageUpdatedTask(
1193 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1194 user));
1195 }
Kenny Guyed131872014-04-30 03:02:21 +01001196 }
1197
Kenny Guy44cba692016-01-21 19:50:02 +00001198 @Override
1199 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1200 enqueuePackageUpdated(new PackageUpdatedTask(
1201 PackageUpdatedTask.OP_SUSPEND, packageNames,
1202 user));
1203 }
1204
1205 @Override
1206 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1207 enqueuePackageUpdated(new PackageUpdatedTask(
1208 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1209 user));
1210 }
1211
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001212 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1214 * ACTION_PACKAGE_CHANGED.
1215 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001216 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001217 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001218 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001219
Joe Onorato36115782010-06-17 13:28:48 -04001220 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001221 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001222 // If we have changed locale we need to clear out the labels in all apps/workspace.
1223 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001224 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001225 Callbacks callbacks = getCallback();
1226 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001227 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001228 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001229 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001230 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001231 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001232 forceReload();
Sunny Goyalda891c12016-03-18 18:29:24 -07001233 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) {
1234 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1235 if (user != null) {
1236 enqueuePackageUpdated(new PackageUpdatedTask(
1237 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1238 new String[0], user));
1239 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001240 }
1241 }
1242
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001243 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001244 resetLoadedState(true, true);
1245
Reena Lee93f824a2011-09-23 17:20:28 -07001246 // Do this here because if the launcher activity is running it will be restarted.
1247 // If it's not running startLoaderFromBackground will merely tell it that it needs
1248 // to reload.
1249 startLoaderFromBackground();
1250 }
1251
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1253 synchronized (mLock) {
1254 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1255 // mWorkspaceLoaded to true later
1256 stopLoaderLocked();
1257 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1258 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1259 }
1260 }
1261
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001262 /**
1263 * When the launcher is in the background, it's possible for it to miss paired
1264 * configuration changes. So whenever we trigger the loader from the background
1265 * tell the launcher that it needs to re-run the loader when it comes back instead
1266 * of doing it now.
1267 */
1268 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001269 Callbacks callbacks = getCallback();
1270 if (callbacks != null) {
1271 // Only actually run the loader if they're not paused.
1272 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001273 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001274 }
1275 }
Joe Onorato36115782010-06-17 13:28:48 -04001276 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001277
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001278 /**
1279 * If there is already a loader task running, tell it to stop.
1280 */
1281 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001282 LoaderTask oldTask = mLoaderTask;
1283 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001284 oldTask.stopLocked();
1285 }
Reena Lee93f824a2011-09-23 17:20:28 -07001286 }
1287
Adam Cohen1a85c582014-09-30 09:48:49 -07001288 public boolean isCurrentCallbacks(Callbacks callbacks) {
1289 return (mCallbacks != null && mCallbacks.get() == callbacks);
1290 }
1291
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001292 public void startLoader(int synchronousBindPage) {
1293 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001294 }
1295
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001296 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001297 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1298 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001299 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001300 // Don't bother to start the thread if we know it's not going to do anything
1301 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001302 final Callbacks oldCallbacks = mCallbacks.get();
1303 // Clear any pending bind-runnables from the synchronized load process.
1304 runOnMainThread(new Runnable() {
1305 public void run() {
1306 oldCallbacks.clearPendingBinds();
1307 }
1308 });
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001311 stopLoaderLocked();
Sunny Goyal93f878c2016-03-30 17:31:24 -07001312 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags, synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001313 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001314 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001315 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1316 } else {
1317 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1318 sWorker.post(mLoaderTask);
1319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 }
1322 }
1323
Joe Onorato36115782010-06-17 13:28:48 -04001324 public void stopLoader() {
1325 synchronized (mLock) {
1326 if (mLoaderTask != null) {
1327 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 }
1329 }
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001332 /**
1333 * Loads the workspace screen ids in an ordered list.
1334 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001335 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001336 final ContentResolver contentResolver = context.getContentResolver();
1337 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001338
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001339 // Get screens ordered by rank.
1340 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1341 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1342 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001343 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001344 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001345 while (sc.moveToNext()) {
1346 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001347 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001348 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001349 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001350 }
1351 }
1352 } finally {
1353 sc.close();
1354 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001355 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001356 }
1357
Joe Onorato36115782010-06-17 13:28:48 -04001358 /**
1359 * Runnable for the thread that loads the contents of the launcher:
1360 * - workspace icons
1361 * - widgets
1362 * - all apps icons
1363 */
1364 private class LoaderTask implements Runnable {
1365 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001366 private int mPageToBindFirst;
1367
Adam Cohen091440a2015-03-18 14:16:05 -07001368 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001369 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001370 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001371 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372
Sunny Goyal93f878c2016-03-30 17:31:24 -07001373 LoaderTask(Context context, int flags, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001374 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001375 mFlags = flags;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001376 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001379 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001380 mIsLoadingAndBindingWorkspace = true;
1381
Joe Onorato36115782010-06-17 13:28:48 -04001382 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001383 if (DEBUG_LOADERS) {
1384 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001386
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001387 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001388 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001389 synchronized (LoaderTask.this) {
1390 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001391 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001392 }
1393 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
1396
Joe Onorato36115782010-06-17 13:28:48 -04001397 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001398 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001399 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001400
Joe Onorato36115782010-06-17 13:28:48 -04001401 private void waitForIdle() {
1402 // Wait until the either we're stopped or the other threads are done.
1403 // This way we don't start loading all apps until the workspace has settled
1404 // down.
1405 synchronized (LoaderTask.this) {
1406 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001407
Joe Onorato36115782010-06-17 13:28:48 -04001408 mHandler.postIdle(new Runnable() {
1409 public void run() {
1410 synchronized (LoaderTask.this) {
1411 mLoadAndBindStepFinished = true;
1412 if (DEBUG_LOADERS) {
1413 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001414 }
Joe Onorato36115782010-06-17 13:28:48 -04001415 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001416 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001417 }
Joe Onorato36115782010-06-17 13:28:48 -04001418 });
1419
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001420 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001421 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001422 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1423 // wait no longer than 1sec at a time
1424 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001425 } catch (InterruptedException ex) {
1426 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001427 }
1428 }
Joe Onorato36115782010-06-17 13:28:48 -04001429 if (DEBUG_LOADERS) {
1430 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001431 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001432 + "ms for previous step to finish binding");
1433 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001434 }
Joe Onorato36115782010-06-17 13:28:48 -04001435 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001436
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001437 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001438 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 // Ensure that we have a valid page index to load synchronously
1440 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1441 "valid page index");
1442 }
1443 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1444 // Ensure that we don't try and bind a specified page when the pages have not been
1445 // loaded already (we should load everything asynchronously in that case)
1446 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1447 }
1448 synchronized (mLock) {
1449 if (mIsLoaderTaskRunning) {
1450 // Ensure that we are never running the background loading at this point since
1451 // we also touch the background collections
1452 throw new RuntimeException("Error! Background loading is already running");
1453 }
1454 }
1455
1456 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1457 // data structures, we can't allow any other thread to touch that data, but because
1458 // this call is synchronous, we can get away with not locking).
1459
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001460 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001461 // operations from the previous activity. We need to ensure that all queued operations
1462 // are executed before any synchronous binding work is done.
1463 mHandler.flush();
1464
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 // Divide the set of loaded items into those that we are binding synchronously, and
1466 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001467 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1469 // arise from that.
1470 onlyBindAllApps();
1471 }
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001475 if (mStopped) {
1476 return;
1477 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001478 mIsLoaderTaskRunning = true;
1479 }
Joe Onorato36115782010-06-17 13:28:48 -04001480 // Optimize for end-user experience: if the Launcher is up and // running with the
1481 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1482 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001483 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001484 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001485 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001486
Joe Onorato36115782010-06-17 13:28:48 -04001487 if (mStopped) {
1488 break keep_running;
1489 }
1490
Joe Onorato36115782010-06-17 13:28:48 -04001491 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001492
1493 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001494 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1495 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497
Joe Onorato36115782010-06-17 13:28:48 -04001498 // Clear out this reference, otherwise we end up holding it until all of the
1499 // callback runnables are done.
1500 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 synchronized (mLock) {
1503 // If we are still the last one to be scheduled, remove ourselves.
1504 if (mLoaderTask == this) {
1505 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001508 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510 }
1511
1512 public void stopLocked() {
1513 synchronized (LoaderTask.this) {
1514 mStopped = true;
1515 this.notify();
1516 }
1517 }
1518
1519 /**
1520 * Gets the callbacks object. If we've been stopped, or if the launcher object
1521 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1522 * object that was around when the deferred message was scheduled, and if there's
1523 * a new Callbacks object around then also return null. This will save us from
1524 * calling onto it with data that will be ignored.
1525 */
1526 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1527 synchronized (mLock) {
1528 if (mStopped) {
1529 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531
1532 if (mCallbacks == null) {
1533 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535
1536 final Callbacks callbacks = mCallbacks.get();
1537 if (callbacks != oldCallbacks) {
1538 return null;
1539 }
1540 if (callbacks == null) {
1541 Log.w(TAG, "no mCallbacks");
1542 return null;
1543 }
1544
1545 return callbacks;
1546 }
1547 }
1548
1549 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001550 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1551 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001552 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001553 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001554 final int countX = profile.numColumns;
1555 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001558 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001559 // Return early if we detect that an item is under the hotseat button
1560 if (mCallbacks == null ||
1561 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001562 Log.e(TAG, "Error loading shortcut into hotseat " + item
1563 + " into position (" + item.screenId + ":" + item.cellX + ","
1564 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001565 return false;
1566 }
1567
Dan Sandler295ae182013-12-10 16:05:47 -05001568 final ItemInfo[][] hotseatItems =
1569 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1570
Adam Cohen2e6da152015-05-06 11:42:25 -07001571 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001572 Log.e(TAG, "Error loading shortcut " + item
1573 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001574 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001575 + ")");
1576 return false;
1577 }
1578
Dan Sandler295ae182013-12-10 16:05:47 -05001579 if (hotseatItems != null) {
1580 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 Log.e(TAG, "Error loading shortcut into hotseat " + item
1582 + " into position (" + item.screenId + ":" + item.cellX + ","
1583 + item.cellY + ") occupied by "
1584 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1585 [(int) item.screenId][0]);
1586 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001587 } else {
1588 hotseatItems[(int) item.screenId][0] = item;
1589 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001591 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001592 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 items[(int) item.screenId][0] = item;
1594 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001595 return true;
1596 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001597 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1598 if (!workspaceScreens.contains((Long) item.screenId)) {
1599 // The item has an invalid screen id.
1600 return false;
1601 }
1602 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001604 return true;
1605 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001606
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001608 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 occupied.put(item.screenId, items);
1610 }
1611
Dan Sandler295ae182013-12-10 16:05:47 -05001612 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001613 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1614 item.cellX < 0 || item.cellY < 0 ||
1615 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1616 Log.e(TAG, "Error loading shortcut " + item
1617 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1618 + item.cellX + "," + item.cellY
1619 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1620 return false;
1621 }
1622
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001623 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001624 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1625 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001626 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001627 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001628 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001629 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001630 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001631 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001632 return false;
1633 }
1634 }
1635 }
1636 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1637 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001638 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001639 }
1640 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001641
Joe Onorato36115782010-06-17 13:28:48 -04001642 return true;
1643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644
Winson Chungba9c37f2013-08-30 14:11:37 -07001645 /** Clears all the sBg data structures */
1646 private void clearSBgDataStructures() {
1647 synchronized (sBgLock) {
1648 sBgWorkspaceItems.clear();
1649 sBgAppWidgets.clear();
1650 sBgFolders.clear();
1651 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001652 sBgWorkspaceScreens.clear();
1653 }
1654 }
1655
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001656 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001657 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658
Joe Onorato36115782010-06-17 13:28:48 -04001659 final Context context = mContext;
1660 final ContentResolver contentResolver = context.getContentResolver();
1661 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001662 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001663 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001664 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001665
Winson Chung892c74d2013-08-22 16:15:50 -07001666 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001667 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001668 int countX = profile.numColumns;
1669 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001670
Sunny Goyalf076eae2016-01-11 12:25:10 -08001671 if (GridSizeMigrationTask.ENABLED &&
1672 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1673 // Migration failed. Clear workspace.
1674 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001675 }
1676
Dan Sandlerd5024042014-01-09 15:01:33 -05001677 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001678 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001679 LauncherSettings.Settings.call(contentResolver,
1680 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001681 }
1682
1683 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1684 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001685 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001686 LauncherSettings.Settings.call(contentResolver,
1687 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 } else {
1689 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001690 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001691 LauncherSettings.Settings.call(contentResolver,
1692 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001693 }
Adam Cohene25af792013-06-06 23:08:25 -07001694
Winson Chung2abf94d2012-07-18 18:16:38 -07001695 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001696 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001697 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001698 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001699 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001700
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001701 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1702 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001703 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001704 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001705 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001706
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 // +1 for the hotseat (it can be larger than the workspace)
1708 // Load workspace in reverse order to ensure that latest items are loaded first (and
1709 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001710 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001711 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 try {
1714 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1715 final int intentIndex = c.getColumnIndexOrThrow
1716 (LauncherSettings.Favorites.INTENT);
1717 final int titleIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 final int containerIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.CONTAINER);
1721 final int itemTypeIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.ITEM_TYPE);
1723 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001725 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 final int screenIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.SCREEN);
1729 final int cellXIndex = c.getColumnIndexOrThrow
1730 (LauncherSettings.Favorites.CELLX);
1731 final int cellYIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.CELLY);
1733 final int spanXIndex = c.getColumnIndexOrThrow
1734 (LauncherSettings.Favorites.SPANX);
1735 final int spanYIndex = c.getColumnIndexOrThrow(
1736 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001737 final int rankIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001739 final int restoredIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001741 final int profileIdIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001743 final int optionsIndex = c.getColumnIndexOrThrow(
1744 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001745 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746
Sunny Goyal7f834d22015-04-21 10:10:23 -07001747 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001748 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001749 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001750 long serialNo = mUserManager.getSerialNumberForUser(user);
1751 allUsers.put(serialNo, user);
1752 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001753 }
1754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 ShortcutInfo info;
1756 String intentDescription;
1757 LauncherAppWidgetInfo appWidgetInfo;
1758 int container;
1759 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001760 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001762 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 while (!mStopped && c.moveToNext()) {
1765 try {
1766 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001767 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001768 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001769 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 switch (itemType) {
1772 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1773 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001774 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001776 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001777 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001778 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001779 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001780 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001781 if (user == null) {
1782 // User has been deleted remove the item.
1783 itemsToRemove.add(id);
1784 continue;
1785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 try {
1787 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001788 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001789 if (cn != null && cn.getPackageName() != null) {
1790 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1791 cn.getPackageName(), user);
1792 boolean validComponent = validPkg &&
1793 launcherApps.isActivityEnabledForProfile(cn, user);
1794
1795 if (validComponent) {
1796 if (restored) {
1797 // no special handling necessary for this item
1798 restoredRows.add(id);
1799 restored = false;
1800 }
Kenny Guy44cba692016-01-21 19:50:02 +00001801 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1802 cn.getPackageName(), user);
1803 if (isSuspended) {
1804 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1805 }
Kenny Guyff05f432016-01-22 17:48:29 +00001806 if (quietMode.get(serialNumber)) {
1807 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1808 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001809 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001810 intent = null;
1811 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1812 // We allow auto install apps to have their intent
1813 // updated after an install.
1814 intent = manager.getLaunchIntentForPackage(
1815 cn.getPackageName());
1816 if (intent != null) {
1817 ContentValues values = new ContentValues();
1818 values.put(LauncherSettings.Favorites.INTENT,
1819 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001820 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001821 }
1822 }
1823
1824 if (intent == null) {
1825 // The app is installed but the component is no
1826 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001827 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001828 itemsToRemove.add(id);
1829 continue;
1830 } else {
1831 // no special handling necessary for this item
1832 restoredRows.add(id);
1833 restored = false;
1834 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001835 } else if (restored) {
1836 // Package is not yet available but might be
1837 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001838 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001839
1840 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1841 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001842 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001843 // App restore has started. Update the flag
1844 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1845 ContentValues values = new ContentValues();
1846 values.put(LauncherSettings.Favorites.RESTORED,
1847 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001848 updateItem(id, values);
1849 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1850 // This is a common app. Try to replace this.
1851 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1852 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1853 if (parser.findDefaultApp()) {
1854 // Default app found. Replace it.
1855 intent = parser.parsedIntent;
1856 cn = intent.getComponent();
1857 ContentValues values = parser.parsedValues;
1858 values.put(LauncherSettings.Favorites.RESTORED, 0);
1859 updateItem(id, values);
1860 restored = false;
1861 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001862
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001863 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001864 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001865 itemsToRemove.add(id);
1866 continue;
1867 }
Sunny Goyal94485362014-09-18 16:13:58 -07001868 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001869 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001870 itemsToRemove.add(id);
1871 continue;
1872 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001873 } else if (launcherApps.isAppEnabled(
1874 manager, cn.getPackageName(),
1875 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1876 // Package is present but not available.
1877 allowMissingTarget = true;
1878 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1879 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001880 // SdCard is not ready yet. Package might get available,
1881 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001882 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001883 HashSet<String> pkgs = sPendingPackages.get(user);
1884 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001885 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001886 sPendingPackages.put(user, pkgs);
1887 }
1888 pkgs.add(cn.getPackageName());
1889 allowMissingTarget = true;
1890 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001891
1892 } else {
1893 // Do not wait for external media load anymore.
1894 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001895 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001896 itemsToRemove.add(id);
1897 continue;
Winson Chungee055712013-07-30 14:46:24 -07001898 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001899 } else if (cn == null) {
1900 // For shortcuts with no component, keep them as they are
1901 restoredRows.add(id);
1902 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001903 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001905 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001906 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 continue;
1908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909
Sunny Goyal34b65272015-03-11 16:56:52 -07001910 boolean useLowResIcon = container >= 0 &&
1911 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1912
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 if (itemReplaced) {
1914 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001915 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001916 cursorIconInfo.iconIndex, titleIndex,
1917 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001918 } else {
1919 // Don't replace items for other profiles.
1920 itemsToRemove.add(id);
1921 continue;
1922 }
1923 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001924 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001925 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001926 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001927 intent = getRestoredItemIntent(c, context, intent);
1928 } else {
1929 // Don't restore items for other profiles.
1930 itemsToRemove.add(id);
1931 continue;
1932 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001933 } else if (itemType ==
1934 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001935 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001936 cursorIconInfo.iconIndex, titleIndex,
1937 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001939 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001940
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 // App shortcuts that used to be automatically added to Launcher
1942 // didn't always have the correct intent flags set, so do that
1943 // here
1944 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001945 intent.getCategories() != null &&
1946 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001947 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 intent.addFlags(
1949 Intent.FLAG_ACTIVITY_NEW_TASK |
1950 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1951 }
Michael Jurka96879562012-03-22 05:54:33 -07001952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001954 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001955 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 info.cellX = c.getInt(cellXIndex);
1960 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001961 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001962 info.spanX = 1;
1963 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001964 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001965 if (info.promisedIntent != null) {
1966 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1967 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001968 info.isDisabled = disabledState;
1969 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1970 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1971 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001972
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001974 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001975 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 break;
1977 }
1978
Sunny Goyal756adbc2015-04-16 15:20:51 -07001979 if (restored) {
1980 ComponentName cn = info.getTargetComponent();
1981 if (cn != null) {
1982 Integer progress = installingPkgs.get(cn.getPackageName());
1983 if (progress != null) {
1984 info.setInstallProgress(progress);
1985 } else {
1986 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1987 }
1988 }
1989 }
1990
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 switch (container) {
1992 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1993 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1994 sBgWorkspaceItems.add(info);
1995 break;
1996 default:
1997 // Item is in a user folder
1998 FolderInfo folderInfo =
1999 findOrMakeFolder(sBgFolders, container);
2000 folderInfo.add(info);
2001 break;
2002 }
2003 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002004 } else {
2005 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 }
2007 break;
2008
2009 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2010 id = c.getLong(idIndex);
2011 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2012
Sunny Goyala508e4f2015-05-21 09:33:57 -07002013 // Do not trim the folder label, as is was set by the user.
2014 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 folderInfo.cellX = c.getInt(cellXIndex);
2019 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002020 folderInfo.spanX = 1;
2021 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002022 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023
Daniel Sandler8802e962010-05-26 16:28:16 -04002024 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002025 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002026 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002027 break;
2028 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002029
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2032 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2033 sBgWorkspaceItems.add(folderInfo);
2034 break;
Joe Onorato36115782010-06-17 13:28:48 -04002035 }
Joe Onorato17a89222011-02-08 17:26:11 -08002036
Chris Wrenf4d08112014-01-16 18:13:56 -05002037 if (restored) {
2038 // no special handling required for restored folders
2039 restoredRows.add(id);
2040 }
2041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2043 sBgFolders.put(folderInfo.id, folderInfo);
2044 break;
2045
2046 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002047 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002049 boolean customWidget = itemType ==
2050 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002053 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002054 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002055 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002056 user = allUsers.get(serialNumber);
2057 if (user == null) {
2058 itemsToRemove.add(id);
2059 continue;
2060 }
2061
Sunny Goyalff572272014-07-23 13:58:07 -07002062 final ComponentName component =
2063 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002064
Sunny Goyal651077b2014-06-30 14:15:31 -07002065 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002066 final boolean isIdValid = (restoreStatus &
2067 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002068 final boolean wasProviderReady = (restoreStatus &
2069 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002070
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002071 if (widgetProvidersMap == null) {
2072 widgetProvidersMap = AppWidgetManagerCompat
2073 .getInstance(mContext).getAllProvidersMap();
2074 }
2075 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2076 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002077 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002078 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002079
2080 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002081 if (!isSafeMode && !customWidget &&
2082 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002083 addDumpLog("Deleting widget that isn't installed anymore: "
2084 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 itemsToRemove.add(id);
2086 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002087 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002088 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2089 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002090
Sunny Goyal53f96722015-07-13 19:54:53 -07002091 // The provider is available. So the widget is either
2092 // available or not available. We do not need to track
2093 // any future restore updates.
2094 int status = restoreStatus &
2095 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002096 if (!wasProviderReady) {
2097 // If provider was not previously ready, update the
2098 // status and UI flag.
2099
2100 // Id would be valid only if the widget restore broadcast was received.
2101 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002102 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002103 } else {
2104 status &= ~LauncherAppWidgetInfo
2105 .FLAG_PROVIDER_NOT_READY;
2106 }
2107 }
2108 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002109 } else {
2110 Log.v(TAG, "Widget restore pending id=" + id
2111 + " appWidgetId=" + appWidgetId
2112 + " status =" + restoreStatus);
2113 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002114 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002115 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002116 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002117
2118 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2119 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002120 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002121 // App restore has started. Update the flag
2122 appWidgetInfo.restoreStatus |=
2123 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002124 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002125 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002126 itemsToRemove.add(id);
2127 continue;
2128 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002129
2130 appWidgetInfo.installProgress =
2131 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002132 }
Sunny Goyalff572272014-07-23 13:58:07 -07002133
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 appWidgetInfo.cellX = c.getInt(cellXIndex);
2137 appWidgetInfo.cellY = c.getInt(cellYIndex);
2138 appWidgetInfo.spanX = c.getInt(spanXIndex);
2139 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002140 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002141
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2143 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2144 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002145 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2146 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 continue;
2148 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002150 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002152 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002153 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 break;
2155 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002156
Adam Cohen59400422014-03-05 18:07:04 -08002157 if (!customWidget) {
2158 String providerName =
2159 appWidgetInfo.providerName.flattenToString();
2160 if (!providerName.equals(savedProvider) ||
2161 (appWidgetInfo.restoreStatus != restoreStatus)) {
2162 ContentValues values = new ContentValues();
2163 values.put(
2164 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2165 providerName);
2166 values.put(LauncherSettings.Favorites.RESTORED,
2167 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002168 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002169 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2172 sBgAppWidgets.add(appWidgetInfo);
2173 }
Joe Onorato36115782010-06-17 13:28:48 -04002174 break;
2175 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002177 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002178 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 }
2180 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002181 if (c != null) {
2182 c.close();
2183 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 }
2185
Winson Chungba9c37f2013-08-30 14:11:37 -07002186 // Break early if we've stopped loading
2187 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002188 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002189 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002190 }
2191
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002194 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2195 Utilities.createDbSelectionQuery(
2196 LauncherSettings.Favorites._ID, itemsToRemove), null);
2197 if (DEBUG_LOADERS) {
2198 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2199 LauncherSettings.Favorites._ID, itemsToRemove));
2200 }
2201
2202 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002203 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2204 .call(contentResolver,
2205 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2206 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2207 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002208 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2209 sBgFolders.remove(folderId);
2210 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002211 }
2212 }
2213
Sunny Goyal317698b2015-07-29 11:45:41 -07002214 // Sort all the folder items and make sure the first 3 items are high resolution.
2215 for (FolderInfo folder : sBgFolders) {
2216 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2217 int pos = 0;
2218 for (ShortcutInfo info : folder.contents) {
2219 if (info.usingLowResIcon) {
2220 info.updateIcon(mIconCache, false);
2221 }
2222 pos ++;
2223 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2224 break;
2225 }
2226 }
2227 }
2228
Chris Wrenf4d08112014-01-16 18:13:56 -05002229 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002230 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002231 ContentValues values = new ContentValues();
2232 values.put(LauncherSettings.Favorites.RESTORED, 0);
2233 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2234 Utilities.createDbSelectionQuery(
2235 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002236 }
2237
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002238 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2239 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002240 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002241 null, sWorker);
2242 }
2243
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002244 // Remove any empty screens
2245 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002246 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002247 long screenId = item.screenId;
2248 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2249 unusedScreens.contains(screenId)) {
2250 unusedScreens.remove(screenId);
2251 }
2252 }
2253
2254 // If there are any empty screens remove them, and update.
2255 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002256 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 }
2259
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 if (DEBUG_LOADERS) {
2261 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2262 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002264 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002266
Sunny Goyale2df0622015-04-24 11:27:00 -07002267 for (int i = 0; i < nScreens; i++) {
2268 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002269 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 line += " | ";
2271 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002272 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002273 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002274 if (x < screen.length && y < screen[x].length) {
2275 line += (screen[x][y] != null) ? "#" : ".";
2276 } else {
2277 line += "!";
2278 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 }
Joe Onorato36115782010-06-17 13:28:48 -04002280 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002282 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 }
Joe Onorato36115782010-06-17 13:28:48 -04002284 }
Adam Cohene25af792013-06-06 23:08:25 -07002285 }
2286
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002287 /**
2288 * Partially updates the item without any notification. Must be called on the worker thread.
2289 */
2290 private void updateItem(long itemId, ContentValues update) {
2291 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002292 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002293 update,
2294 BaseColumns._ID + "= ?",
2295 new String[]{Long.toString(itemId)});
2296 }
2297
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 /** Filters the set of items who are directly or indirectly (via another container) on the
2299 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002300 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 ArrayList<ItemInfo> allWorkspaceItems,
2302 ArrayList<ItemInfo> currentScreenItems,
2303 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002304 // Purge any null ItemInfos
2305 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2306 while (iter.hasNext()) {
2307 ItemInfo i = iter.next();
2308 if (i == null) {
2309 iter.remove();
2310 }
2311 }
2312
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 // Order the set of items by their containers first, this allows use to walk through the
2314 // list sequentially, build up a list of containers that are in the specified screen,
2315 // as well as all items in those containers.
2316 Set<Long> itemsOnScreen = new HashSet<Long>();
2317 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2318 @Override
2319 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002320 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 }
2322 });
2323 for (ItemInfo info : allWorkspaceItems) {
2324 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002325 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 currentScreenItems.add(info);
2327 itemsOnScreen.add(info.id);
2328 } else {
2329 otherScreenItems.add(info);
2330 }
2331 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2332 currentScreenItems.add(info);
2333 itemsOnScreen.add(info.id);
2334 } else {
2335 if (itemsOnScreen.contains(info.container)) {
2336 currentScreenItems.add(info);
2337 itemsOnScreen.add(info.id);
2338 } else {
2339 otherScreenItems.add(info);
2340 }
2341 }
2342 }
2343 }
2344
2345 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002346 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 ArrayList<LauncherAppWidgetInfo> appWidgets,
2348 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2349 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350
2351 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002352 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002354 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 currentScreenWidgets.add(widget);
2356 } else {
2357 otherScreenWidgets.add(widget);
2358 }
2359 }
2360 }
2361
2362 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002363 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002364 LongArrayMap<ItemInfo> itemsIdMap,
2365 LongArrayMap<FolderInfo> folders,
2366 LongArrayMap<FolderInfo> currentScreenFolders,
2367 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368
Sunny Goyale2df0622015-04-24 11:27:00 -07002369 int total = folders.size();
2370 for (int i = 0; i < total; i++) {
2371 long id = folders.keyAt(i);
2372 FolderInfo folder = folders.valueAt(i);
2373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002375 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 currentScreenFolders.put(id, folder);
2379 } else {
2380 otherScreenFolders.put(id, folder);
2381 }
2382 }
2383 }
2384
2385 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2386 * right) */
2387 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002388 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002389 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002390 final int screenCols = profile.numColumns;
2391 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002393 @Override
2394 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002395 if (lhs.container == rhs.container) {
2396 // Within containers, order by their spatial position in that container
2397 switch ((int) lhs.container) {
2398 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2399 long lr = (lhs.screenId * screenCellCount +
2400 lhs.cellY * screenCols + lhs.cellX);
2401 long rr = (rhs.screenId * screenCellCount +
2402 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002403 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002404 }
2405 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2406 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002407 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002408 }
2409 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002410 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002411 throw new RuntimeException("Unexpected container type when " +
2412 "sorting workspace items.");
2413 }
2414 return 0;
2415 }
2416 } else {
2417 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002418 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002419 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002420 }
2421 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002423
Adam Cohendcd297f2013-06-18 13:13:40 -07002424 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2425 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002426 final Runnable r = new Runnable() {
2427 @Override
2428 public void run() {
2429 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2430 if (callbacks != null) {
2431 callbacks.bindScreens(orderedScreens);
2432 }
2433 }
2434 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002435 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002436 }
2437
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2439 final ArrayList<ItemInfo> workspaceItems,
2440 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002441 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002442 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443
2444 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002445 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002447 final int start = i;
2448 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 final Runnable r = new Runnable() {
2450 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002453 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002454 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2455 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002456 }
2457 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002459 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002460 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461
2462 // Bind the folders
2463 if (!folders.isEmpty()) {
2464 final Runnable r = new Runnable() {
2465 public void run() {
2466 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2467 if (callbacks != null) {
2468 callbacks.bindFolders(folders);
2469 }
2470 }
2471 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002472 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 }
2474
2475 // Bind the widgets, one at a time
2476 N = appWidgets.size();
2477 for (int i = 0; i < N; i++) {
2478 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2479 final Runnable r = new Runnable() {
2480 public void run() {
2481 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2482 if (callbacks != null) {
2483 callbacks.bindAppWidget(widget);
2484 }
2485 }
2486 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002487 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 }
2489 }
2490
2491 /**
2492 * Binds all loaded data to actual views on the main thread.
2493 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002494 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 final long t = SystemClock.uptimeMillis();
2496 Runnable r;
2497
2498 // Don't use these two variables in any of the callback runnables.
2499 // Otherwise we hold a reference to them.
2500 final Callbacks oldCallbacks = mCallbacks.get();
2501 if (oldCallbacks == null) {
2502 // This launcher has exited and nobody bothered to tell us. Just bail.
2503 Log.w(TAG, "LoaderTask running with no launcher");
2504 return;
2505 }
2506
Winson Chung9b9fb962013-11-15 15:39:34 -08002507 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2509 ArrayList<LauncherAppWidgetInfo> appWidgets =
2510 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002511 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002512
2513 final LongArrayMap<FolderInfo> folders;
2514 final LongArrayMap<ItemInfo> itemsIdMap;
2515
Winson Chung2abf94d2012-07-18 18:16:38 -07002516 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 workspaceItems.addAll(sBgWorkspaceItems);
2518 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002519 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002520
2521 folders = sBgFolders.clone();
2522 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 }
2524
Derek Prothro7aff3992013-12-10 14:00:37 -05002525 final boolean isLoadingSynchronously =
2526 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002527 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002528 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002529 if (currScreen >= orderedScreenIds.size()) {
2530 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002531 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002533 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002534 final long currentScreenId = currentScreen < 0
2535 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002536
2537 // Load all the items that are on the current page first (and in the process, unbind
2538 // all the existing workspace items before we call startBinding() below.
2539 unbindWorkspaceItemsOnMainThread();
2540
2541 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2543 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2544 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2545 new ArrayList<LauncherAppWidgetInfo>();
2546 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2547 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002548 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2549 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550
Winson Chung9b9fb962013-11-15 15:39:34 -08002551 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002553 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002555 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 otherFolders);
2557 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2558 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2559
2560 // Tell the workspace that we're about to start binding items
2561 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002562 public void run() {
2563 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2564 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002565 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002567 }
2568 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002570 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571
Adam Cohendcd297f2013-06-18 13:13:40 -07002572 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2573
Sunny Goyal527c7d32015-08-28 15:19:36 -07002574 Executor mainExecutor = new DeferredMainThreadExecutor();
2575 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002577 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578
Sunny Goyal527c7d32015-08-28 15:19:36 -07002579 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2580 // remaining screens after first onDraw is called. This ensures that the first screen
2581 // is immediately visible (eg. during rotation)
2582 // In case of !isLoadingSynchronously, bind all pages one after other.
2583 final Executor deferredExecutor = isLoadingSynchronously ?
2584 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2585
2586 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2587 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002588
2589 // Tell the workspace that we're done binding items
2590 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002591 public void run() {
2592 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2593 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002594 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002595 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002596
Sunny Goyal639e9062015-08-19 19:17:06 -07002597 mIsLoadingAndBindingWorkspace = false;
2598
2599 // Run all the bind complete runnables after workspace is bound.
2600 if (!mBindCompleteRunnables.isEmpty()) {
2601 synchronized (mBindCompleteRunnables) {
2602 for (final Runnable r : mBindCompleteRunnables) {
2603 runOnWorkerThread(r);
2604 }
2605 mBindCompleteRunnables.clear();
2606 }
2607 }
2608
Winson Chung98e030b2012-05-07 16:01:11 -07002609 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002610 if (DEBUG_LOADERS) {
2611 Log.d(TAG, "bound workspace in "
2612 + (SystemClock.uptimeMillis()-t) + "ms");
2613 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002614
Joe Onorato36115782010-06-17 13:28:48 -04002615 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002616 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002617 deferredExecutor.execute(r);
2618
Winson Chung4a2afa32012-07-19 14:53:05 -07002619 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002620 r = new Runnable() {
2621 public void run() {
2622 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2623 if (callbacks != null) {
2624 // We are loading synchronously, which means, some of the pages will be
2625 // bound after first draw. Inform the callbacks that page binding is
2626 // not complete, and schedule the remaining pages.
2627 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2628 callbacks.onPageBoundSynchronously(currentScreen);
2629 }
2630 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2631 }
2632 }
2633 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002634 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002635 }
Joe Onorato36115782010-06-17 13:28:48 -04002636 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002637
Joe Onorato36115782010-06-17 13:28:48 -04002638 private void loadAndBindAllApps() {
2639 if (DEBUG_LOADERS) {
2640 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2641 }
2642 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002643 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002644 synchronized (LoaderTask.this) {
2645 if (mStopped) {
2646 return;
2647 }
2648 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002649 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002650 synchronized (LoaderTask.this) {
2651 if (mStopped) {
2652 return;
2653 }
2654 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002655 }
Joe Onorato36115782010-06-17 13:28:48 -04002656 } else {
2657 onlyBindAllApps();
2658 }
2659 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002660
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002661 private void updateIconCache() {
2662 // Ignore packages which have a promise icon.
2663 HashSet<String> packagesToIgnore = new HashSet<>();
2664 synchronized (sBgLock) {
2665 for (ItemInfo info : sBgItemsIdMap) {
2666 if (info instanceof ShortcutInfo) {
2667 ShortcutInfo si = (ShortcutInfo) info;
2668 if (si.isPromise() && si.getTargetComponent() != null) {
2669 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2670 }
2671 } else if (info instanceof LauncherAppWidgetInfo) {
2672 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2673 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2674 packagesToIgnore.add(lawi.providerName.getPackageName());
2675 }
2676 }
2677 }
2678 }
2679 mIconCache.updateDbIcons(packagesToIgnore);
2680 }
2681
Joe Onorato36115782010-06-17 13:28:48 -04002682 private void onlyBindAllApps() {
2683 final Callbacks oldCallbacks = mCallbacks.get();
2684 if (oldCallbacks == null) {
2685 // This launcher has exited and nobody bothered to tell us. Just bail.
2686 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2687 return;
2688 }
2689
2690 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002691 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002692 final ArrayList<AppInfo> list
2693 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002694 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002695 public void run() {
2696 final long t = SystemClock.uptimeMillis();
2697 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2698 if (callbacks != null) {
2699 callbacks.bindAllApplications(list);
2700 }
2701 if (DEBUG_LOADERS) {
2702 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002703 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002704 }
2705 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002706 };
2707 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002708 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002709 r.run();
2710 } else {
2711 mHandler.post(r);
2712 }
Joe Onorato36115782010-06-17 13:28:48 -04002713 }
2714
Winson Chung64359a52013-07-08 17:17:08 -07002715 private void loadAllApps() {
2716 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002717
Joe Onorato36115782010-06-17 13:28:48 -04002718 final Callbacks oldCallbacks = mCallbacks.get();
2719 if (oldCallbacks == null) {
2720 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002721 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002722 return;
2723 }
2724
Kenny Guyed131872014-04-30 03:02:21 +01002725 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2726
Winson Chung64359a52013-07-08 17:17:08 -07002727 // Clear the list of apps
2728 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002729 for (UserHandleCompat user : profiles) {
2730 // Query for the set of apps
2731 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002732 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002733 if (DEBUG_LOADERS) {
2734 Log.d(TAG, "getActivityList took "
2735 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2736 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2737 }
2738 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002739 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002740 if (apps == null || apps.isEmpty()) {
2741 return;
2742 }
Kenny Guyff05f432016-01-22 17:48:29 +00002743 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002744 // Create the ApplicationInfos
2745 for (int i = 0; i < apps.size(); i++) {
2746 LauncherActivityInfoCompat app = apps.get(i);
2747 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002748 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002749 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002750
Sunny Goyal756a28a2015-04-23 17:07:55 -07002751 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2752 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002753 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002754
2755 @Override
2756 public void run() {
2757 heuristic.processUserApps(apps);
2758 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002759 };
2760 runOnMainThread(new Runnable() {
2761
2762 @Override
2763 public void run() {
2764 // Check isLoadingWorkspace on the UI thread, as it is updated on
2765 // the UI thread.
2766 if (mIsLoadingAndBindingWorkspace) {
2767 synchronized (mBindCompleteRunnables) {
2768 mBindCompleteRunnables.add(r);
2769 }
2770 } else {
2771 runOnWorkerThread(r);
2772 }
2773 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002774 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002775 }
Winson Chung64359a52013-07-08 17:17:08 -07002776 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002777 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002778 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2779 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002780
2781 // Post callback on main thread
2782 mHandler.post(new Runnable() {
2783 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002784
Winson Chung64359a52013-07-08 17:17:08 -07002785 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002786 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002787 if (callbacks != null) {
2788 callbacks.bindAllApplications(added);
2789 if (DEBUG_LOADERS) {
2790 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002791 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002792 }
2793 } else {
2794 Log.i(TAG, "not binding apps: no Launcher activity");
2795 }
2796 }
2797 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002798 // Cleanup any data stored for a deleted user.
2799 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002800 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002801 Log.d(TAG, "Icons processed in "
2802 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002803 }
2804 }
2805
2806 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002807 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002808 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002809 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2810 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2811 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2812 }
Joe Onorato36115782010-06-17 13:28:48 -04002813 }
2814 }
2815
Sunny Goyal75b0f552015-05-20 21:57:06 -07002816 /**
2817 * Called when the icons for packages have been updated in the icon cache.
2818 */
2819 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2820 final Callbacks callbacks = getCallback();
2821 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2822 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2823
2824 // If any package icon has changed (app was updated while launcher was dead),
2825 // update the corresponding shortcuts.
2826 synchronized (sBgLock) {
2827 for (ItemInfo info : sBgItemsIdMap) {
2828 if (info instanceof ShortcutInfo && user.equals(info.user)
2829 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2830 ShortcutInfo si = (ShortcutInfo) info;
2831 ComponentName cn = si.getTargetComponent();
2832 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2833 si.updateIcon(mIconCache);
2834 updatedShortcuts.add(si);
2835 }
2836 }
2837 }
2838 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2839 }
2840
2841 if (!updatedShortcuts.isEmpty()) {
2842 final UserHandleCompat userFinal = user;
2843 mHandler.post(new Runnable() {
2844
2845 public void run() {
2846 Callbacks cb = getCallback();
2847 if (cb != null && callbacks == cb) {
2848 cb.bindShortcutsChanged(updatedShortcuts,
2849 new ArrayList<ShortcutInfo>(), userFinal);
2850 }
2851 }
2852 });
2853 }
2854
2855 if (!updatedApps.isEmpty()) {
2856 mHandler.post(new Runnable() {
2857
2858 public void run() {
2859 Callbacks cb = getCallback();
2860 if (cb != null && callbacks == cb) {
2861 cb.bindAppsUpdated(updatedApps);
2862 }
2863 }
2864 });
2865 }
2866 }
2867
Joe Onorato36115782010-06-17 13:28:48 -04002868 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002869 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002870 }
2871
Adam Cohen091440a2015-03-18 14:16:05 -07002872 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002873
2874 @Override
2875 public void onReceive(Context context, Intent intent) {
2876 synchronized (sBgLock) {
2877 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2878 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002879 final PackageManager manager = context.getPackageManager();
2880 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2881 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002882 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2883 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002884 packagesRemoved.clear();
2885 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002886 for (String pkg : entry.getValue()) {
2887 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002888 boolean packageOnSdcard = launcherApps.isAppEnabled(
2889 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2890 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002891 packagesUnavailable.add(pkg);
2892 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002893 packagesRemoved.add(pkg);
2894 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002895 }
2896 }
2897 if (!packagesRemoved.isEmpty()) {
2898 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2899 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2900 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002901 if (!packagesUnavailable.isEmpty()) {
2902 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2903 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2904 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002905 }
Sunny Goyal34942622014-08-29 17:20:55 -07002906 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002907 }
2908 }
2909 }
2910
Joe Onorato36115782010-06-17 13:28:48 -04002911 private class PackageUpdatedTask implements Runnable {
2912 int mOp;
2913 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002914 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002915
2916 public static final int OP_NONE = 0;
2917 public static final int OP_ADD = 1;
2918 public static final int OP_UPDATE = 2;
2919 public static final int OP_REMOVE = 3; // uninstlled
2920 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002921 public static final int OP_SUSPEND = 5; // package suspended
2922 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002923 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002924
Kenny Guyed131872014-04-30 03:02:21 +01002925 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002926 mOp = op;
2927 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002928 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002929 }
2930
2931 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002932 if (!mHasLoaderCompletedOnce) {
2933 // Loader has not yet run.
2934 return;
2935 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002936 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002937
2938 final String[] packages = mPackages;
2939 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002940 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002941 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002942 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002943 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002944 for (int i=0; i<N; i++) {
2945 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002946 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002947 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002948 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002949
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002950 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2951 if (heuristic != null) {
2952 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002953 }
Joe Onorato36115782010-06-17 13:28:48 -04002954 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002955 }
Joe Onorato36115782010-06-17 13:28:48 -04002956 case OP_UPDATE:
2957 for (int i=0; i<N; i++) {
2958 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002959 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002960 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002961 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002962 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002963 // Since package was just updated, the target must be available now.
2964 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002965 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002966 case OP_REMOVE: {
2967 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2968 if (heuristic != null) {
2969 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002970 }
Joe Onorato36115782010-06-17 13:28:48 -04002971 for (int i=0; i<N; i++) {
2972 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002973 mIconCache.removeIconsForPkg(packages[i], mUser);
2974 }
2975 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002976 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002977 case OP_UNAVAILABLE:
2978 for (int i=0; i<N; i++) {
2979 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2980 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002981 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002982 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002983 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002984 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002985 case OP_SUSPEND:
2986 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002987 flagOp = mOp == OP_SUSPEND ?
2988 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2989 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002990 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002991 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2992 break;
2993 case OP_USER_AVAILABILITY_CHANGE:
2994 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2995 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2996 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2997 // We want to update all packages for this user.
2998 pkgFilter = StringFilter.matchesAll();
2999 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003000 break;
Joe Onorato36115782010-06-17 13:28:48 -04003001 }
3002
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003003 ArrayList<AppInfo> added = null;
3004 ArrayList<AppInfo> modified = null;
3005 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003006
Adam Cohen487f7dd2012-06-28 18:12:10 -07003007 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003009 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003010 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003011 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003012 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003013 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003014 }
Winson Chung5d55f332012-07-16 20:45:03 -07003015 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003016 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003017 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003018 }
3019
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003021
Joe Onorato36115782010-06-17 13:28:48 -04003022 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003023 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003024 for (AppInfo ai : added) {
3025 addedOrUpdatedApps.put(ai.componentName, ai);
3026 }
Joe Onorato36115782010-06-17 13:28:48 -04003027 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003028
Joe Onorato36115782010-06-17 13:28:48 -04003029 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003031 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032 for (AppInfo ai : modified) {
3033 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003034 }
3035
Joe Onorato36115782010-06-17 13:28:48 -04003036 mHandler.post(new Runnable() {
3037 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003038 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003039 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003040 callbacks.bindAppsUpdated(modifiedFinal);
3041 }
3042 }
3043 });
3044 }
Winson Chung83892cc2013-05-01 16:53:33 -07003045
Sunny Goyal4390ace2014-10-13 11:33:11 -07003046 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003047 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003048 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3049 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3050 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3051
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003052 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003053 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003054 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3055 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003056 boolean infoUpdated = false;
3057 boolean shortcutUpdated = false;
3058
3059 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003060 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003061 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003062 Bitmap icon = Utilities.createIconBitmap(
3063 si.iconResource.packageName,
3064 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003065 if (icon != null) {
3066 si.setIcon(icon);
3067 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003069 }
3070 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003071
3072 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003073 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003074 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3075
3076 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3078 // Auto install icon
3079 PackageManager pm = context.getPackageManager();
3080 ResolveInfo matched = pm.resolveActivity(
3081 new Intent(Intent.ACTION_MAIN)
3082 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3083 PackageManager.MATCH_DEFAULT_ONLY);
3084 if (matched == null) {
3085 // Try to find the best match activity.
3086 Intent intent = pm.getLaunchIntentForPackage(
3087 cn.getPackageName());
3088 if (intent != null) {
3089 cn = intent.getComponent();
3090 appInfo = addedOrUpdatedApps.get(cn);
3091 }
3092
3093 if ((intent == null) || (appInfo == null)) {
3094 removedShortcuts.add(si);
3095 continue;
3096 }
3097 si.promisedIntent = intent;
3098 }
3099 }
3100
3101 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003102 if (appInfo != null) {
3103 si.flags = appInfo.flags;
3104 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105
Sunny Goyal756adbc2015-04-16 15:20:51 -07003106 si.intent = si.promisedIntent;
3107 si.promisedIntent = null;
3108 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109 infoUpdated = true;
3110 si.updateIcon(mIconCache);
3111 }
3112
3113 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3114 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3115 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003116 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003117 si.contentDescription = appInfo.contentDescription;
3118 infoUpdated = true;
3119 }
3120
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003121 int oldDisabledFlags = si.isDisabled;
3122 si.isDisabled = flagOp.apply(si.isDisabled);
3123 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 shortcutUpdated = true;
3125 }
3126 }
3127
3128 if (infoUpdated || shortcutUpdated) {
3129 updatedShortcuts.add(si);
3130 }
3131 if (infoUpdated) {
3132 updateItemInDatabase(context, si);
3133 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003134 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3136 if (mUser.equals(widgetInfo.user)
3137 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003138 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003139 widgetInfo.restoreStatus &=
3140 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3141 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003142
3143 // adding this flag ensures that launcher shows 'click to setup'
3144 // if the widget has a config activity. In case there is no config
3145 // activity, it will be marked as 'restored' during bind.
3146 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3147
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 widgets.add(widgetInfo);
3149 updateItemInDatabase(context, widgetInfo);
3150 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003151 }
3152 }
3153 }
3154
Sunny Goyal4390ace2014-10-13 11:33:11 -07003155 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003156 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 mHandler.post(new Runnable() {
3158
3159 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003160 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003161 if (callbacks == cb && cb != null) {
3162 callbacks.bindShortcutsChanged(
3163 updatedShortcuts, removedShortcuts, mUser);
3164 }
3165 }
3166 });
3167 if (!removedShortcuts.isEmpty()) {
3168 deleteItemsFromDatabase(context, removedShortcuts);
3169 }
3170 }
3171 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003172 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003173 mHandler.post(new Runnable() {
3174 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003175 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003176 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003178 }
3179 }
3180 });
3181 }
3182 }
3183
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003184 final HashSet<String> removedPackages = new HashSet<>();
3185 final HashSet<ComponentName> removedComponents = new HashSet<>();
3186 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003187 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003188 Collections.addAll(removedPackages, packages);
3189
3190 // No need to update the removedComponents as
3191 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003192 } else if (mOp == OP_UPDATE) {
3193 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003194 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003195 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003196 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003197 }
3198 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003199
3200 // Update removedComponents as some components can get removed during package update
3201 for (AppInfo info : removedApps) {
3202 removedComponents.add(info.componentName);
3203 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003204 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003205
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003206 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3207 for (String pn : removedPackages) {
3208 deletePackageFromDatabase(context, pn, mUser);
3209 }
3210 for (ComponentName cn : removedComponents) {
3211 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003212 }
3213
Winson Chungdf95eb12013-10-16 14:57:07 -07003214 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003215 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3216
Winson Chungdf95eb12013-10-16 14:57:07 -07003217 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003218 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003219 mHandler.post(new Runnable() {
3220 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003221 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003222 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003223 callbacks.bindWorkspaceComponentsRemoved(
3224 removedPackages, removedComponents, mUser);
3225 }
3226 }
3227 });
3228 }
3229
3230 if (!removedApps.isEmpty()) {
3231 // Remove corresponding apps from All-Apps
3232 final Callbacks callbacks = getCallback();
3233 mHandler.post(new Runnable() {
3234 public void run() {
3235 Callbacks cb = getCallback();
3236 if (callbacks == cb && cb != null) {
3237 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003238 }
3239 }
3240 });
Joe Onoratobe386092009-11-17 17:32:16 -08003241 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003242
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003243 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3244 // get widget update signals.
3245 if (!Utilities.ATLEAST_MARSHMALLOW &&
3246 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003247 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003248 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003249 public void run() {
3250 Callbacks cb = getCallback();
3251 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003252 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003253 }
3254 }
3255 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003256 }
3257 }
3258 }
3259
3260 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3261 mHandler.post(new Runnable() {
3262 @Override
3263 public void run() {
3264 Callbacks cb = getCallback();
3265 if (callbacks == cb && cb != null) {
3266 callbacks.bindWidgetsModel(model);
3267 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003268 }
3269 });
3270 }
3271
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003272 public void refreshAndBindWidgetsAndShortcuts(
3273 final Callbacks callbacks, final boolean bindFirst) {
3274 runOnWorkerThread(new Runnable() {
3275 @Override
3276 public void run() {
3277 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3278 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003279 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003280 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3281 bindWidgetsModel(callbacks, model);
3282 // update the Widget entries inside DB on the worker thread.
3283 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3284 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003285 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003286 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003287 }
3288
Adam Cohen091440a2015-03-18 14:16:05 -07003289 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003290 UserHandleCompat user) {
3291 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3292 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003293 }
Adam Cohen556f6132014-01-15 15:18:08 -08003294
Kenny Guyed131872014-04-30 03:02:21 +01003295 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3296 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003297 if (cn == null) {
3298 return false;
3299 }
Kenny Guyed131872014-04-30 03:02:21 +01003300 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3301 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003302 return false;
3303 }
Kenny Guyed131872014-04-30 03:02:21 +01003304 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003305 }
3306
Adam Cohena28b78e2014-05-20 17:03:04 -07003307 public static boolean isValidPackage(Context context, String packageName,
3308 UserHandleCompat user) {
3309 if (packageName == null) {
3310 return false;
3311 }
3312 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3313 return launcherApps.isPackageEnabledForProfile(packageName, user);
3314 }
3315
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003317 * Make an ShortcutInfo object for a restored application or shortcut item that points
3318 * to a package that is not yet installed on the system.
3319 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003320 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003321 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003322 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003323 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003324
3325 Bitmap icon = iconInfo.loadIcon(c, info, context);
3326 // the fallback icon
3327 if (icon == null) {
3328 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3329 } else {
3330 info.setIcon(icon);
3331 }
Sunny Goyal34942622014-08-29 17:20:55 -07003332
3333 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003334 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003335 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003336 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003337 }
Sunny Goyal34942622014-08-29 17:20:55 -07003338 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3339 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003340 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003341 }
Sunny Goyal34942622014-08-29 17:20:55 -07003342 } else {
3343 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3344 }
3345
Winson Chung82b016c2015-05-08 17:00:10 -07003346 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003347 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003348 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003349 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003350 return info;
3351 }
3352
3353 /**
3354 * Make an Intent object for a restored application or shortcut item that points
3355 * to the market page for the item.
3356 */
Adam Cohen091440a2015-03-18 14:16:05 -07003357 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003358 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003359 return getMarketIntent(componentName.getPackageName());
3360 }
3361
3362 static Intent getMarketIntent(String packageName) {
3363 return new Intent(Intent.ACTION_VIEW)
3364 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003365 .scheme("market")
3366 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003367 .appendQueryParameter("id", packageName)
3368 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003369 }
3370
3371 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003372 * Make an ShortcutInfo object for a shortcut that is an application.
3373 *
3374 * If c is not null, then it will be used to fill in missing data like the title and icon.
3375 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003376 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003377 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003378 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003379 if (user == null) {
3380 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003381 return null;
3382 }
3383
Kenny Guyed131872014-04-30 03:02:21 +01003384 ComponentName componentName = intent.getComponent();
3385 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003386 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003387 return null;
3388 }
3389
3390 Intent newIntent = new Intent(intent.getAction(), null);
3391 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3392 newIntent.setComponent(componentName);
3393 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003394 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003395 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3396 return null;
3397 }
3398
3399 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003400 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003401 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3402 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3403 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003404 }
3405
Joe Onorato56d82912010-03-07 14:32:10 -05003406 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003407 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003408 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003409 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003410
Joe Onorato56d82912010-03-07 14:32:10 -05003411 // fall back to the class name of the activity
3412 if (info.title == null) {
3413 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003414 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003415
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003417 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003418 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003419 if (lai != null) {
3420 info.flags = AppInfo.initFlags(lai);
3421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003424
Sunny Goyale2df0622015-04-24 11:27:00 -07003425 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003426 ItemInfoFilter f) {
3427 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3428 for (ItemInfo i : infos) {
3429 if (i instanceof ShortcutInfo) {
3430 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003431 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003432 if (cn != null && f.filterItem(null, info, cn)) {
3433 filtered.add(info);
3434 }
3435 } else if (i instanceof FolderInfo) {
3436 FolderInfo info = (FolderInfo) i;
3437 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003438 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003439 if (cn != null && f.filterItem(info, s, cn)) {
3440 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003441 }
3442 }
Winson Chung64359a52013-07-08 17:17:08 -07003443 } else if (i instanceof LauncherAppWidgetInfo) {
3444 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3445 ComponentName cn = info.providerName;
3446 if (cn != null && f.filterItem(null, info, cn)) {
3447 filtered.add(info);
3448 }
Winson Chung8a435102012-08-30 17:16:53 -07003449 }
3450 }
Winson Chung64359a52013-07-08 17:17:08 -07003451 return new ArrayList<ItemInfo>(filtered);
3452 }
3453
Adam Cohen091440a2015-03-18 14:16:05 -07003454 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003455 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003456 ItemInfoFilter filter = new ItemInfoFilter() {
3457 @Override
3458 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003459 if (info.user == null) {
3460 return cn.equals(cname);
3461 } else {
3462 return cn.equals(cname) && info.user.equals(user);
3463 }
Winson Chung64359a52013-07-08 17:17:08 -07003464 }
3465 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003466 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003467 }
3468
Sunny Goyal1a745e82014-10-02 15:58:31 -07003469 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003470 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003471 */
Adam Cohen091440a2015-03-18 14:16:05 -07003472 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003473 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003474 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003475 // Non-app shortcuts are only supported for current user.
3476 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003479 // TODO: If there's an explicit component and we can't install that, delete it.
3480
Winson Chung82b016c2015-05-08 17:00:10 -07003481 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003482
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003483 Bitmap icon = iconInfo.loadIcon(c, info, context);
3484 // the fallback icon
3485 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003486 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003487 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003488 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003489 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 return info;
3491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003492
Sunny Goyal2350bc92014-10-14 16:42:54 -07003493 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003494 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3495 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3496 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3497
Adam Cohend9198822011-11-22 16:42:47 -08003498 if (intent == null) {
3499 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3500 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3501 return null;
3502 }
3503
Joe Onorato0589f0f2010-02-08 13:44:00 -08003504 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003505 boolean customIcon = false;
3506 ShortcutIconResource iconResource = null;
3507
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003508 if (bitmap instanceof Bitmap) {
3509 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003510 customIcon = true;
3511 } else {
3512 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003513 if (extra instanceof ShortcutIconResource) {
3514 iconResource = (ShortcutIconResource) extra;
3515 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003516 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003517 }
3518 }
3519
Michael Jurkac9d95c52011-08-29 14:03:34 -07003520 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003521
Kenny Guyed131872014-04-30 03:02:21 +01003522 // Only support intents for current user for now. Intents sent from other
3523 // users wouldn't get here without intent forwarding anyway.
3524 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003525 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003526 icon = mIconCache.getDefaultIcon(info.user);
3527 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003528 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003529 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003530
Winson Chung82b016c2015-05-08 17:00:10 -07003531 info.title = Utilities.trim(name);
3532 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003533 info.intent = intent;
3534 info.customIcon = customIcon;
3535 info.iconResource = iconResource;
3536
3537 return info;
3538 }
3539
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003541 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 * or make a new one.
3543 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003544 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 // See if a placeholder was created for us already
3546 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003547 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003549 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 folders.put(id, folderInfo);
3551 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003552 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003553 }
3554
Joe Onoratobe386092009-11-17 17:32:16 -08003555
Sunny Goyal651077b2014-06-30 14:15:31 -07003556 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3557 return (provider != null) && (provider.provider != null)
3558 && (provider.provider.getPackageName() != null);
3559 }
3560
Joe Onoratobe386092009-11-17 17:32:16 -08003561 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003562 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003563 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3564 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3565 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3566 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003567 if (mLoaderTask != null) {
3568 mLoaderTask.dumpState();
3569 } else {
3570 Log.d(TAG, "mLoaderTask=null");
3571 }
Joe Onoratobe386092009-11-17 17:32:16 -08003572 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003573
3574 public Callbacks getCallback() {
3575 return mCallbacks != null ? mCallbacks.get() : null;
3576 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003577
3578 /**
3579 * @return {@link FolderInfo} if its already loaded.
3580 */
3581 public FolderInfo findFolderById(Long folderId) {
3582 synchronized (sBgLock) {
3583 return sBgFolders.get(folderId);
3584 }
3585 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003586
Sunny Goyal527c7d32015-08-28 15:19:36 -07003587 @Thunk class DeferredMainThreadExecutor implements Executor {
3588
3589 @Override
3590 public void execute(Runnable command) {
3591 runOnMainThread(command);
3592 }
3593 }
3594
Sunny Goyal756adbc2015-04-16 15:20:51 -07003595 /**
3596 * @return the looper for the worker thread which can be used to start background tasks.
3597 */
3598 public static Looper getWorkerLooper() {
3599 return sWorkerThread.getLooper();
3600 }
Sunny Goyala1365452015-10-01 15:46:24 -07003601
3602 @Thunk static final void addDumpLog(String log) {
3603 Launcher.addDumpLog(TAG, log);
3604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605}